Histogram Help!

I need help with, i would also appreciate a little explanation so i can comprehend whats going on.

A histogram showing the number of students who received scores in the following ranges: 0 thru 5, 6 thru 10, 11 thru 15, 16 thru 20, 21 thru 25, and 25 thru 30. The histogram should look something like this:

Frequency
---------
Score Obtained By 5 10 15 20 25
----- ----------- ----|----|----|----|----|

0...5 1 *

6...10 7 *******

11...15 10 **********

16...20 3 ***

21...25 2 **

26...30 2 **


my current code:
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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
 
 
 
int main()
{
        const int ARRAY_SIZE = 31;
		const int CLASS_SIZE = 25;
        char answerkey[ARRAY_SIZE];
        char studentanswers[ARRAY_SIZE];
        char student_name[64];
		int count = 0;
		int i = 0;
		int sum = 0;
		int mean = 0 ;
        ifstream inputFile;
 
        inputFile.open("xfile.txt");
			if( !inputFile.good() )
			{
					cout << "Error opening file" << endl;
					return EXIT_FAILURE;
			}
 
	
		inputFile.getline(answerkey, ARRAY_SIZE);
        cout << answerkey<< endl;
       
		for(i = 0; i < CLASS_SIZE; i++)
	{
		int score = 0;
        inputFile.getline(student_name, 64);
	    cout << student_name << endl;

		inputFile.getline(studentanswers, ARRAY_SIZE);
        cout << studentanswers << endl;
			for( count = 0; count < ARRAY_SIZE; count++)
				if(answerkey[count] == studentanswers[count])
					score = score + 1;
				else
					score = score;
		sum = score + sum;
		cout << score << endl;          
	}
		cout << endl <<"The class mean is " << (mean = sum / 25 ) << endl; 
return 0;
       
}
Last edited on
You should store the mean as a double since it is an average, it will most likely contain a decimal.

to make a histogram you have to have an increment variable that will be set to the amount of students that received that score.

You then take that variable and use it within a for loop to print the asterisks.

I would use a while loop like this:
1
2
3
4
5
6
7
8
9
10
while (count <= numberOfStudents)
{
   if ((score >= 0) && score<= 5))
          accum1++;
   else if (( .....))
          accum2++;
     //repeat this logic for amout of scores necessary

count++;
}
Topic archived. No new replies allowed.