Arrays of 6???

Generate a text-based histogram for a quiz given to a class of students. The quiz is graded on a scale from 0 to 5. Write a program that allows the user to enter grades for each student. As the grades are being entered, the program should count, using an array, the number of 0’s, the number of 1’s, then number of 2’s, the number of 3’s, the number of 4’s and the number of 5’s. The program should be capable of handling an arbitrary number of student grades.

You can do this by making an array of size 6, where each array element is initialized to zero. Whenever a zero is entered, increment array[0]. Whenever a one is entered, increment array[1], and so on, up to array[5].

Output the histogram count at the end. For example, if the input grades are 3, 0, 1, 3, 3, 5, 5, 4, 5, 4, then the program should output

1 grade(s) of 0
1 grade(s) of 1
0 grade(s) of 2
3 grade(s) of 3
2 grade(s) of 4
3 grade(s) of 5

I have no idea how to finish up this program. I have started it, but I do not know what else to put in it. I'm a noob when it comes to coding :/. Here's what I have so far:

using namespace std;

int main ()
{
int i;
string grades[6];
int zero,one,two,three,four,five;

for (i=0; i<6; i++)
{
cout<< "Enter students grades";
getline (cin, grades [i]);
}



cout <<"0 grades"<<zero[i]<<endl;
cout<<"1 grades"<<one[i]<<endl;
cout<<"2 grades"<<two[i]<<endl;
cout<<"3 grades"<<three[i]<<endl;
cout<<"4 grades"<<four[i]<<endl;
cout<<"5 grades"<<five[i]<<endl;

system ("pause");


return 0;

}










You should define an array of unsigned integers and fill it when you will enter grades.
first change string grades[6] to ints

int grades[6] = {0} //sets all to 0

secondly.. you don't need int zero, one, two, etc

just increment the array..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    char cont;
    int myinput;
    int grades[6] = {0};
    do{
       cin>>myinput;
       if(myinput <= 5) // makes sure its in range of the array
        grades[myinput]++; // increments the value at grades 0 - 5

        cout << "Enter another value? y or n";
        cin >> cont;
}while(cont != 'n'); //continue till n is entered

for(int i=0; i < 6; i++) // out put the array
    cout << grades[i] << " grade(s) of (" << i << ")" << endl;

}


editted due to some typo's in the code

Last edited on
Topic archived. No new replies allowed.