Hello,
I was given the assignment below, but I have no idea what the program is even supposed to do. I wrote some more questions below the assignment. Thanks in advance!
-- ASSIGNMENT --
Write a function named grade_dist that takes three int arrays as parameters. The parameters are named: grades, cutoffs, and dist. The grades array is a list of grades with each value between 0 and 100 inclusive. The end of the list is indicated by a value of -1. The cutoffs are a list of cutoffs for latter grades also with values between 0 and 100 inclusive, and the end of the list is indicated by -1. These are inputs to the function. The third array, dist, is the output. It counts how many grades are in the range defined by the cutoffs, so it represents a grade distribution.
If cutoffs[] = {90, 80, 70, 60, -1}; then dist will have 5 elements, dist[0] will be how many grades were >= 90, dist[1] will be the number of grades < 90 but >= 80, dist[2] will be the number of grades < 80 but >= 70, dist[3] will be the number of grades < 70 but >= 60, and dist[4] will be the number of grades < 60. This can be done with nested loops. The outer loop will go through all the grades. The inner loop might look like:
1 2 3 4 5 6 7 8
|
for(i = 0; cutoffs[i] >= 0; i++)
{
if(grade >= cutoff[i])
{
dist[i]++;
break;
}
}
|
This loop will be inside another loop that sets grade equal to each value in the grades array. The break is used to prevent a grade from counting in more than one category. Can you find another way to do this without using break? Also, you will need some additional code after this inner loop. If the loop finishes with cutoffs[i] == -1, then the grade did not get counted because it falls into the < 60 category, so you need to account for this.
------------
1. What does the -1 element in the cutoffs[] array mean? I know that it represents the end of the list, but can't it just be cutoffs[] = {90, 80, 70, 60};?
2. What is the relationship between cutoffs[] and dist? I can see how the number of the element of cutoffs[] goes inside dist[] but the bigger picture is not clicking.
3. What is exactly happening inside the if-statement if(grade >= cutoff[i])? Again, I can't really see the bigger picture here.
4. If I remember correctly, break; "exits" the loop. Do you have any suggestions to avoid using break as the assignment asks?