What is my assignment's output?

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?
1. As you said -1 means the end of the array. The problem with just using cutoffs[] = {90, 80, 70, 60}; Is that the size of the array may be 10 but only 4 values are filled and you don't want to try to read all 10 values when you only need the first 4 so instead you can use -1 and have your code setup so when you reach a -1 that it doesn't process it.

2. What you doing basically is comparing the arrays cutoffs and grades and using dist to hold for your results.

3. In the loop, you are checking if the grade that you pulled from the grade array is greater than or equal to the current cutoff you are testing. Suggestion: I commonly found it easier to ignore the code snippets that my teachers gave me, I found it better to name the variables myself so I could remember them and know what they do. Also if you build the code yourself you have a better understanding of it.

4.Not sure on this, I only use a break in switch statments. My suggestion ignore this tiny code snippet and start working on the larger program.
Thanks for your response! Do you understand what the program is even supposed to do? Is there even an output? My current understanding is that it takes x number of grades and puts each grade in another array, almost like it's organizing and storing the grades in a table. Is this what's going on?

I appreciate your suggestions, by the way!
What I believe it wants is something like you have an array of the grades that different students got on a test and it wants you to find out how many of those grades were between 100 and 90, then 90 and 80, and so on. Though it wants you to find out the ranges it wants (100 to 90) by pulling the values from the cutoff array. Then store the number of scores that were between the cutoffs in dist. It's really a pretty weird the way it wants it done. I'm guessing since you are using arrays you don't know vectors yet which would make this a little easier.
Last edited on
Hmm.. that makes some more sense. No we have not learned vectors yet!

Would the user enter the grades through a looped input? Also, the instructions say "the outer loop will go through all the grades." How would go about doing this? I think it may be some kind of for loop?

So far here is what I have (By the way, once I get a better idea of what I'm doing, I'll rebuild my professor's snippets into my own as you suggested. For now I'm just trying to see things go.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int grade_dist(int grades[], int cutoffs[], int dist[]);

int main()
{
    cout << "Enter your grades:" << endl;


    // Still figuring out how to use a loop for inputted values


    return 0;
}

int grade_dist(int grades[], int cutoffs[], int dist[])
{
    //cutoffs[] = {90,80,70,60,-1};

    for(int i = 0; cutoffs[i] >= 0; i++)
    {
        if(grade >= cutoff[i])
            {
                dist[i]++;
                break;
            }
    }

    return /* Return what? I think this function may be a void one.*/ ;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

void grade_dist(int grades[], int cutoffs[], int dist[]);

int main()
{

	int grade;

	cout << "Enter your grades:" << endl;


	//ya you will have a for loop here to get the grade inputted, either you will need to ask the user first how many grades they will input or
	//tell them when their done entering grades to enter -1 to signal the end, also make sure they dont enter more grades then you have room in the arry


	return 0;
}

void grade_dist(int grades[], int cutoffs[], int dist[])
{
	//cutoffs[] = {90,80,70,60,-1};
	
	//you are going to have another for loop around your current one that will get a grade out of your grades array,
	//make sure you dont use i for this loop too, use a different variable

	for (int i = 0; cutoffs[i] >= 0; i++)
	{
		if (grade >= cutoffs[i])// made cutoff cutoffs
		{
			dist[i]++;
			break;
		}
	}

}
Last edited on
Awesome! Thank you so much! I finally have somewhere to go! :)
Topic archived. No new replies allowed.