Begin by writing a function named countScores to accept the list of scores and return an array of one hundred and one counts, one for each possible score on a one-hundred point exam (0 to 100).
Be sure to initialize all one hundred and one counters to zero when the function begins. Use a pair of nested loops; the outer loop counts the number of students, while the inner loop looks at each score for a particular student.
writeHistogram should use a pair of nested loops also; the outer loop counts the possible scores, while the inner loop counts the number of occurrences for each score, printing an asterisk for each.
So far my program prints the possible scores and prints an asterisk next to each score. I am having difficulty making my code print the required number of asterisks dependent on the frequency of entered numbers next to each score.
This is my current code but I have manipulated this for several days only to get this or infinite loops. I am grateful for any and all help, suggestions, and advise
This is the output I want
02 100 *
03 99
04 98 ****
05 97 **
06 96 ***
07 95 *
08 ...
09 This is the output I get
10 100 *
11 99 *
12 98 *
13 97 *
14 96 *
15 95 *
16 ...
17 The output is supposed to produce a histogram showing the frequency of occurrence of each score in the set of scores read from the keyboard.I changed the initialization as suggested but the main problem is still there.
01 #include <iostream>
02 #include <iomanip>
03 #include <cctype>
04 #include <math.h>
05
06
07
08 using namespace std;
09 using std::cout;
10 using std::cin;
11
12
13 const int numberOfTests = 4;
14
15
16 void countScore (int studentScore[][numberOfTests], int numberOfStundents, int counts[101]);
17 void writeHistogram (int counts[101]);
18
19 int main ()
20 {
21 int studentScore[30][numberOfTests];
22 int counts[101];
23 int numberOfStudents;
24
25 countScores(studentScore, numberOfStudents, counts);
26 cout << endl;
27
28 writeHistogram(counts);
29
30 return 0;
31 }
32
33
34
35
36 void countScors (int studentScore[][numberOfTests], int numberOfStudents, int counts[101])
37 {
38 numberOfStudents = 0;
39 cout << "Enter the test scores (-1 to end): ";
40 for (int i =0; i <= numberOfTests-1; i++)
41 {
42 cin >> studentScore[numberOfStudents][i];
43 }
44 while (studentScore[numberOfStudents][0] != -1)
45 {
46 numberOfStudents++;
47 cout << "Enter the test scores (-1 to end): ";
48 for (int i = 0; i <= numberOfTests-1; i++)
49 {
50 cin >> studentScore[numberOfStudents][i];
51 }
52 }
53 counts[0]=0;
54 //...
55 counts[101]=0;
56 for (int i = 0; i < numberOfStudents; i++)
57 {
58 for (int j = 0; j < studentScore[numberOfStudents][i]; j++)
59 return;
60 }
61 }
62
63 void writeHistogram (int counts[101])
64 {
65 int studentScore[30][numberOfTests];
66 int numberOfStudents = 0;
67
68 for (int i = 101; i > 0; i--)
69 {
70 cout << i-1 << ":";
71 for (int j = 0; j <= studentScore[numberOfStudents][0]; j++)
72 {
73 cout << " *";
74 }
75 cout << endl;
76 }
77 return;
78 }
In void writeHistogram (int counts[101])
studentScore is empty, and it also needs to be only 1 dimensional... in fact you could just as well use counts. Do you see why and how, and also why at line 71 you should use < in place of <= when dealing with counts?
I tried changing the < at line 71 and using counts in the void writeHistogram function
it now prints the numbers 1 through 100 with 4 lines of asterisks for each number...