Need help with a grade processor assignment using arrays..

Pages: 1234
kemort,
It looks like the use of strings would have made this assignment easier. I think the next assignment we learn about them though. Figures.

I am getting a compiler error because I dont have c++ 11?
closed account (48T7M4Gy)
What error? I am running this program on latest Xcode so maybe u need '='

char letter[] = {'A','B','C','D','E','F'};
int group[5] = {0};

etc, instead of no '='
Last edited on
closed account (48T7M4Gy)
@Dmtg93

Here's the lot, there are a couple of quirky bits - strings and how you work out the score generation, but they are monor.

I have to go.

Good luck with it :)

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void getNumberOfScores( int& );
int generateOneScore();
void generateScores( int[], int );
void displayScores(int[], int );
int findLowest(int[], int );
int findHighest(int[], int );
double calcAverage (int[], int, bool);
int countAboveAverage (int[], int, double);
char findLetterGrade(double);
void displayDuplicateScores( int[], int );
void displayHistogram( int[], int );

const int MIN_A = 90;
const int MIN_B = 80;
const int MIN_C = 70;
const int MIN_D = 60;

int main()
{
    cout
    << "Welcome to the Grade Processor Application\n"
    << "This app will generate the number of scores desired by the user\n"
    << "and then perform and display several functions on this data.\n\n";
    
    int no_of_scores = 0;
    
    getNumberOfScores(no_of_scores);
    
    int* scores  = new int[no_of_scores];
    srand(time_t(0));
    generateScores(scores, no_of_scores);
    displayScores(scores, no_of_scores);
    
    double average = calcAverage(scores, no_of_scores, false);
    cout
    << "               Average of all scores: " << average << '\n'
    << " Number of scores above this average: "
    << countAboveAverage(scores, no_of_scores, average) << '\n'
    << "         Letter grade for all scores: "
    << findLetterGrade(average) << '\n'
    << '\n';
    
    average = calcAverage(scores, no_of_scores, true);
    cout
    << "Average of all scores without lowest: " << average << '\n'
    << " Number of scores above this average: "
    << countAboveAverage(scores, no_of_scores, average) << '\n'
    << "  Letter grade after dropping lowest: "
    << findLetterGrade(average) << '\n'
    << '\n';
    
    cout
    << "               Minimum of all scores: "
    << findLowest(scores, no_of_scores) << '\n'
    << "               Maximum of all scores: "
    << findHighest(scores, no_of_scores) << '\n'
    << '\n';
    
    cout
    << "Displaying histogram for " << no_of_scores << " scores:\n";
    displayHistogram(scores, no_of_scores);
    
    cout << "Score occurrence counts > 1:\n";
    displayDuplicateScores(scores, no_of_scores);
    cout << '\n';
    
    cout << "Thanks for using the Grade Processor application.\n";
    
    delete[] scores;
    
    return 0;
}

void getNumberOfScores( int& number)
{
    cout << "How many scores do you want to process: ";
    cin >> number;
}

int generateOneScore()
{
    return rand() % 65 + 35; // <--
}

void generateScores( int list[], int n )
{
    for(int i = 0; i < n; ++i)
    {
        list[i] = generateOneScore();
    }
    return;
}

void displayScores(int list[], int n)
{
    cout << "All scores:\n";
    
    int col_count = 0;
    
    for(int i = 0; i < n; i++)
    {
        cout << setw(6) << list[i];
        ++col_count;
        
        if(col_count % 10 == 0)
            cout << '\n';
    }
    cout << '\n';
    
    return;
}


int findLowest(int list[], int n)
{
    int smallest = list[0]; // <---
    for (int i = 1; i < n; i++) // <--
    {
        if (list[i] < smallest)
            smallest = list[i];
    }
    return smallest;
}


int findHighest(int list[], int n)
{
    int largest = list[0];
    for (int i = 1; i < n; i++)
    {
        if (list[i] > largest)
            largest = list[i];
    }
    return largest;
}


double calcAverage (int list[], int n, bool drop)
{
    double total = 0;
    for(int i = 0; i < n; ++i)
        total += list[i];
    
    if (drop)
    {
        total -= findLowest(list,n);
        n--;
    }
    
    return  1.0*total/n;
}

int countAboveAverage (int list[], int n, double average)
{
    int count = 0;
    for(int i = 0; i < n; ++i)
    {
        if(list[i] > average)
            count++;
    }
    
    return count;
}

char findLetterGrade(double grade)
{
    char letter_grade;
    
    if(grade >= MIN_A)
        letter_grade = 'A';
    else if(grade >= MIN_B)
        letter_grade = 'B';
    else if(grade >= MIN_C)
        letter_grade = 'C';
    else if(grade >= MIN_D)
        letter_grade = 'D';
    else
        letter_grade = 'F';
    
    return letter_grade;
}

void displayDuplicateScores( int list[], int n )
{
    int duplicates[101]{0};
    
    for( int i = 0; i < n; i++)
        ++duplicates[ list[i] ];
    
    int col_count = 0;
    for (int i = 100; i >= 0; --i)
    {
        if (duplicates[i] > 1)
        {
            col_count++;
            cout << setw(6) << i << setw(2) << duplicates[i];
            
            if(col_count % 10 == 0)
                cout << '\n';
        }
    }
    cout << '\n';
}

void displayHistogram( int list[], int n )
{
    string letter = {"ABCDEF"};
    int group[5] = {0};
    char grade;
    
    for(int i = 0; i < n; ++i)
    {
        grade = findLetterGrade(list[i]);
        
        switch(grade)
        {
            case 'A':
                ++group[0];
                break;
            case 'B':
                ++group[1];
                break;
            case 'C':
                ++group[2];
                break;
            case 'D':
                ++group[3];
                break;
            case 'F':
                ++group[4];
                break;
            default:
                cout << "Error in histogram processing\n";
                break;
        }
    }
    
    string histogram = "";
    for (int i = 0; i < 5; ++i)
    {
        histogram.assign(group[i], '*');
        cout << letter[i] << ": " << histogram << '\n';
    }
    cout << '\n';
}


Welcome to the Grade Processor Application
This app will generate the number of scores desired by the user
and then perform and display several functions on this data.

How many scores do you want to process: 10
All scores:
    85    76    85    77    71    36    95    87    53    96

               Average of all scores: 76.1
 Number of scores above this average: 6
         Letter grade for all scores: C

Average of all scores without lowest: 80.5556
 Number of scores above this average: 5
  Letter grade after dropping lowest: B

               Minimum of all scores: 36
               Maximum of all scores: 96

Displaying histogram for 10 scores:
A: **
B: ***
C: ***
D: 
E: **

Score occurrence counts > 1:
    85 2

Thanks for using the Grade Processor application.
Program ended with exit code: 0
Added the = sign, works perfect now.Thanks!

Do you happen to know how to fix my calcAverage? I was thinking of using an else statement, but like Andy was saying it should work without it im just not sure what the error the address of 'double calcAverage(int*, int, bool)' will always evaluate as 'true means. When I run the program and use
1
2
3
4
cout << "Average of all scores:  " << endl;
	calcAverage(list, userNum,drop1);
	cout << calcAverage<<endl;
	cout<<"\n";

It outputs a 1 instead of the average of the numbers in the array.
This is how far I am in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int userNum = 0;
	getNumberOfScores(userNum);
	cout << "How many scores do you want to process: " << userNum << endl;

	generateScores(list, userNum);

	cout << "All Scores:" << endl;
	displayScores(list, userNum);
	cout << "\n";

	cout << "Average of all scores:  " << endl;
	calcAverage(list, userNum,drop1);
	cout << calcAverage<<endl;
	cout<<"\n";
closed account (48T7M4Gy)
Welcome to the Grade Processor Application
This app will generate the number of scores desired by the user
and then perform and display several functions on this data.

How many scores do you want to process: 6
All scores:
    85    76    85    77    71    36
               Average of all scores: 71.6667
 Number of scores above this average: 4
         Letter grade for all scores: C

Average of all scores without lowest: 78.8
 Number of scores above this average: 2
  Letter grade after dropping lowest: C

               Minimum of all scores: 36
               Maximum of all scores: 85

Displaying histogram for 6 scores:
A: 
B: **
C: ***
D: 
E: *
F: *

Score occurrence counts > 1:
    85 2

Thanks for using the Grade Processor application.
Program ended with exit code: 0
closed account (48T7M4Gy)
Unnecessary duplicate deleted ...
Last edited on
closed account (48T7M4Gy)
Duplicate deleted ...
Last edited on
closed account (48T7M4Gy)
BTW I made a blooper on line 248 - 5 should be 6 and I'm surprised, shocked and horrified that u didn't pic that up. ;)
Last edited on
Thank you kemort, appreciate it :)
Also I cant use the string class, is there another way to make use of this
1
2
 
int* scores = new int[no_of_scores];

It's only displaying the minimum values and not the max
Hey,
I'm making progress here, here's what I did for the histogram, it seems to be working well. I'm sure there are better ways to do so by not using strings, but hey, it works for now.

make variables for numAs, numBs...
Set in a for loop set your grade = to your array
if your grade >= numXs, increase numX
else if grade >= numYs. incrase num Y

then in a for statement set an asterisk to output an * iterating numXs amount of times. Do this for all A-F.
Last edited on
Hello Dmtg93,

Since "calcAverage" returns a double line 2 is an empty call to the function. Normally a function like this would be written as "average = calcAverage(list, n, drop)". The only time you would use a function call that returns a value with out setting the return value equal to something would be in a cout statement.

It does not matter what yo call the third parameter as long as it is defined as a bool.

Every if statement does not always requires an else. The way this one works is if drop is true then subtract one from sum and subtract one from "n". Otherwise the for loop already has all the numbers in list and if "drop" is false nothing more needs to be done.

I am having a problem understanding this:
1
2
error : invalid types 'int[int]' for array subscript
  if (aGrades[lc] <= 100 && aGrades[lc] >= 90) aCounters[0]++;


Not seeing how you wrote or used the whole for loop.These two lines are hard to understand because the error could be on a line that is not shown.

This:
1
2
3
4
cout << "Average of all scores:  " << endl;
calcAverage(list, userNum, drop1);
cout << calcAverage << endl;
cout << "\n";

should look like this:
1
2
3
4
cout << "Average of all scores:  " << endl;
// drop1 needs to be set to true or false before the function call on the next line.
cout << calcAverage(list, userNum, drop1) << endl;
cout << "\n";


In main you have:
1
2
getNumberOfScores(userNum);
cout << "How many scores do you want to process: " << userNum << endl;

Usually the prompt goes before the input. In this case before the function call. Or put the prompt in the function where it makes more sense.

Just had a thought about cout << calcAverage << endl;. I do not think you defined a variable called "calcAverage" and this is not a complete function call, so this might be confusing the compiler.

Hope that helps,

Andy
Last edited on
closed account (48T7M4Gy)
Also I cant use the string class, is there another way to make use of this
int* scores = new int[no_of_scores];


I think you mean you can't use pointers IIRC from an earlier post.


So what you do is make an array big enough at compile time to cover the estimated maximum number of scores and then enter the actual number as you are already doing.

ie int scores[500]{0}; allows for 500 scores at the start and 'off ya go'. As long as you program doesn't ask for more then you won't crash.
Last edited on
@Dmtg93, you asked what lines 18 and 26 in http://www.cplusplus.com/forum/beginner/218769/3/#msg1009081 mean?

I have read from your comments that you are not allowed to use pointers. If you have not used pointers, then dynamic memory allocation will not be familiar either. I commented what the lines are doing.

Details of it is here http://www.cplusplus.com/doc/tutorial/dynamic/

Also I cant use the string class, is there another way to make use of this

Yes, there is another way of doing it as earlier pointed by Andy [http://www.cplusplus.com/forum/beginner/218769/3/#msg1009093]

This will mean that you have to define the array size at declaration. Since the array size must be constant(except you want to dynamically allocate it (with new())), you have to do something like this
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
const int MAX_SCORE_SIZE = 100; 
int main()
{
    
    // some welcome message
    std::cout << std::setprecision(2) << std::fixed; // decimals fixed to 2. 

	int numOfScores = 0;
	
     getNumberOfScores(numOfScores);
	
	// confirm that you got the right info
	//std::cout << "\nYou entered " << numOfScores << std::endl;

	int list[MAX_SCORE_SIZE] = { 0 }; // creates an array that can take MAX_SCORE_SIZE 
							 // of integers. All is initialized to zero
	
	generateScores(list, numOfScores); 
       // fill list upto numOfScore. NB numOfScores <= MAX_SCORE_SIZE 

	displayScores(list, numOfScores);
	
	// do the rest
	
	return 0;
}
Last edited on
closed account (48T7M4Gy)
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
38
39
40
41
42
43
44
45
46
47
// WITHOUT STRINGS
void displayHistogram( int list[], int n )
{
    // string letter = {"ABCDEF"};
    char letter[]{'A','B','C','D','E','F'};
    
    int group[5] = {0};
    char grade;
    
    for(int i = 0; i < n; ++i)
    {
        grade = findLetterGrade(list[i]);
        
        switch(grade)
        {
            case 'A':
                ++group[0];
                break;
            case 'B':
                ++group[1];
                break;
            case 'C':
                ++group[2];
                break;
            case 'D':
                ++group[3];
                break;
            case 'F':
                ++group[4];
                break;
            default:
                cout << "Error in histogram processing\n";
                break;
        }
    }
    
    string histogram = "";
    for (int i = 0; i < 6; ++i)
    {
        //histogram.assign(group[i], '*');
        cout << letter[i] << ": ";// << histogram << '\n';
        for(int j = 0; j < group[i]; ++j)
            cout << '*';
        cout << '\n';
    }
    cout << '\n';
}
I have an amateurish one here
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
void displayHistogram(int list[], int n)
{
	int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0;
	for (int i = 0; i < n; i++)
	{
		char grade = findLetterGrade(static_cast<double>(list[i]));

		switch (grade)	
		{
		case 'A':
			aCount++;
			break;
		case 'B':
			bCount++;
			break;
		case 'C':
			cCount++;
			break;
		case 'D':
			dCount++;
			break;
		case 'F':
			fCount++;
		default:
			break;
		}

	}

	std::cout << "\nThe score histogram\n";
	std::cout << "\nA: ";
	for (int i = 0; i < aCount; i++)
	{
		std::cout << "*";
	}
	std::cout << "\nB: ";
	for (int i = 0; i < bCount; i++)
	{
		std::cout << "*";
	}
	std::cout << "\nC: ";
	for (int i = 0; i < cCount; i++)
	{
		std::cout << "*";
	}

	std::cout << "\nD: ";
	for (int i = 0; i < dCount; i++)
	{
		std::cout << "*";
	}

	std::cout << "\nF: ";
	for (int i = 0; i < fCount; i++)
	{
		std::cout << "*";
	}
}
//
char findLetterGrade(double grade)
{
	char c = 'F';
	bool gradeA = (grade >= MIN_A && grade <= MAX_VAL);
	bool gradeB = (grade >= MIN_B && grade <= MAX_B);
	bool gradeC = (grade >= MIN_C && grade <= MAX_C);
	bool gradeD = (grade >= MIN_D && grade <= MAX_D);
	bool gradeF = grade < MIN_D;

	if (gradeA) c = 'A';
	else if (gradeB) c = 'B';
	else if (gradeC) c = 'C';
	else if (gradeD) c = 'D';
	else
	{
		if (gradeF) c = 'F';
	}

	return c;
}
Hello Dmtg93,

If you are still having problems with the "histogram" function refer back to my message http://www.cplusplus.com/forum/beginner/218769/3/#msg1009138 Also some of the latest previous messages have good ideas that you could make use of.

kemort was nice enough to give you an idea of a complete program http://www.cplusplus.com/forum/beginner/218769/4/#msg1009166 Pay attention to his "displayDuplicateScores" function, it is a little different than what I did, but the same concept. It can easily be incorporated into what you are doing and the way you are writing.

Somewhere I thought I read that the output of your program should match the output of your OP. I could be wrong here, but it would look better if you could come close.

Hope that helps,

Andy
kemort,
I updated it to look like this at the beginning, it's still is only displaying lowest score?
 
int scores[500]{0};


1
2
3
4
cout << "               Minimum of all scores: "
			<< findLowest(scores, no_of_scores) << '\n'
			<< "               Maximum of all scores: "
			<< findHighest(scores, no_of_scores) << '\n' << '\n';


Thank you everyone for your help!
Last edited on
closed account (48T7M4Gy)
Time to show us all your code. I'm surprised int scores[500] {0} worked this time without the '='
Topic archived. No new replies allowed.
Pages: 1234