### 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:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` #include #include #include 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:
 ``12345678910`` ``````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.