Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem B: Roommate Trouble! You and your roommate were getting along, but just

ID: 3686826 • Letter: P

Question

Problem B: Roommate Trouble! You and your roommate were getting along, but just as you thought everything was fine, she started inviting random friends over to the room. In general, you have no problem with a few friends, but at some point it becomes too much. Luckily, by this point in the semester, you've learned how to deal with your problems maturely. You brought up a polite conversation with your roommate and she agreed to never bring more than 10 friends in any consecutive 7 days. To make sure she's keeping up with her end of the bargain, you start recording how many friends she brings each day. You've decided, however, that it's too pain-staking to look through each 7 day interval to see if your roommate has violated the policy the two of you agreed upon. Thus, you've decided to write a computer program to make the calculation for you. Luckily, your gracious, affable C programming teacher has written some partial code to help you (since she commiserates with your roommate woes). All you have to do is write the function specified in the prototype below. Since your tastes may change, you've decided to write the function so that it can deal with any maximum number of friends for any consecutive period. // Pre-condition: data has length elements. friendLimit, numDays are both // positive and numDays <= length; // Post-condition: Returns 1 if there is some consecutive streak of numDays // integers that adds up to more than friendLimit. Returns 0 // otherwise. int tooManyFriends(int data[], int length, int friendLimit, int numDays);

Explanation / Answer

Here is the code for you:

// Pre-condition: data has length elements. friendLimit, numDays are both
// positive and numDays <= length;
// Post-condition: Returns 1 if there is some consecutive streak of numDays
// integers that adds up to more than friendLimit. Returns 0
// otherwise.
int tooManyFriends(int data[], int length, int friendLimit, int numDays)
{
int sum = 0;
for(int i = 0; i < numDays; i++) //Counts the number of friends in the first numDays consecutive days.
sum += data[i];
for(; i < length; i++)       //For the remaining days from then, till the end.
{
if(sum > friendLimit)       //If the sum exceeds the friend limit.
return 1;                   //Return true.
sum += data[i];               //Add the current day friends.
sum -= data[i - numDays];    //Subtract the day numDays before visited friends.
}
return 0;                       //If at any point friend limit didn't crossed till the end, return 0.
}

If you need any refinements, just get back to me.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote