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

This program uses functions with pass-by-reference parameters. The Problem State

ID: 3680567 • Letter: T

Question

This program uses functions with pass-by-reference parameters.

The Problem Statement

You are very meticulous with your scheduling. Whenever you have a potential activity, you prefer to schedule it on the day with the fewest activities. You have already made a list of the number of activities for each day in the rest of the semester.

Program Setup

A scaffold of the solution has been created for you:

Do not modify the code that is already present. Instead, fill in the function at the end of the program.

This a function will take in a list of the number of activities for each day. The function should also take in a starting day and an ending day to create a range. The function should then return the day number with the fewest activities in that range.

For example, if the input array looked like this:

index

0

1

2

3

4

5

6

7

8

9

10

11

# activities

8

3

6

7

9

5

3

8

6

7

4

5

and the start day of the request was 2 and the end day of the request was 9, the answer would be 6, since on day 6 there are only 3 activities and this is less than any of the other days.

Since you like getting things out of the way as early as possible, if there are multiple days that are the "least busy" in the given interval, return the day that is earliest. For example, if you were given the query day 0 to day 10 with the array above, your function should return 1, since day 1 is the first day with only 3 activities planned.

Function Prototype

You must use this prototype to receive credit for the assignment.

// Pre-condition: low <= high and are both valid indexes to data.

// Post-condition: Returns the lowest index in [low, high] storing

//                 the minimum of array[low]…array[high].

int findMinIndex(int data[], int low, int high);

SCHEDULE-SCAFFOLD.C INCLUDED BELOW

index

0

1

2

3

4

5

6

7

8

9

10

11

# activities

8

3

6

7

9

5

3

8

6

7

4

5

Explanation / Answer

#include <stdio.h>

#define MAX 100

int findMinIndex(int data[], int low, int high);

int main() {
int i, loop, n, queries, data[MAX];

// Read in the schedule data.
FILE* ifp = fopen("schedule.in", "r");
fscanf(ifp, "%d", &n);
for (i=0; i<n; i++)
fscanf(ifp, "%d", &data[i]);

// Process each query.
fscanf(ifp, "%d", &queries);
for (loop=0; loop<queries; loop++) {
int low, high;
fscanf(ifp, "%d%d", &low, &high);
printf("%d ", findMinIndex(data, 0, 4));
}

return 0;
}

// Pre-condition: low <= high and are both valid indexes to data.
// Post-condition: Returns the lowest index in [low, high] storing
// the minimum of array[low]…array[high].
int findMinIndex(int data[], int low, int high) {
int min=data[low];
int index = low;
if (low <= high){
for (int i = low+1 ; i <= high ; i++){
if (min > data[i]){
min = data[i];
index=i;
}
}
return index;
}
else{
return -1;
}
}

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