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: 3686528 • 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);

roommate-scaffold.c

roommate.in

Explanation / Answer

#include <stdio.h>

#define MAX 100

int tooManyFriends(int data[], int length, int friendLimit, int numDays);

int main() {

int i, loop, n;

// Read in the schedule data.
FILE* ifp = fopen("schedule.in", "r");
fscanf(ifp, "%d", &n);

// Process each case
for (loop=1; loop<=n; loop++) {

// Read in all the data for the case.
int totaldays, maxfriends, period, data[MAX];
fscanf(ifp, "%d%d%d", &totaldays, &maxfriends, &period);
for (i=0; i<totaldays; i++)
fscanf(ifp, "%d", &data[i]);

// Output accordingly.
if (tooManyFriends(data, totaldays, maxfriends, period))
printf("Case : RULE BROKEN!!! ");
else
printf("Case : You followed the agreement. ");
}

fclose(ifp);
return 0;
}

// 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 totalFriends = 0;
int i;
for(i=0; i<length-numDays; i++){
      
       totalFriends = 0;
       int j=i;
       for(j=i; j< i+numDays; j++){
           totalFriends = totalFriends + data[j];
       }
      
       if(totalFriends > friendLimit){
           //printf("total: %d ", totalFriends);
           return 1;
       }
   }
   return 0;
}

/*

Output:

Case : You followed the agreement.
Case : You followed the agreement.
Case : You followed the agreement.
Case : You followed the agreement.
Case : You followed the agreement.
Case : RULE BROKEN!!!
Case : You followed the agreement.
Case : RULE BROKEN!!!
Case : RULE BROKEN!!!
Case : RULE BROKEN!!!

*/

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