### Pointers, Dynamic Arrays & Vectors

I get so close, and then it seems my brain shuts down ... I need to write a program that outputs a histogram of student grades for an assignment. The program should input each student's grade as an integer and store the grade in a vector. Grades should be entered until the user enters -1 for a grade. The program should then scan through the vector and compute the histogram. In computing the histogram, the minimum value for a grade is 0, but your program should determine the maximum value entered by the user. Use a dynamic array to store the histogram. Output the histogram to the console. For example, if the input is:

20

30

4

20

30

30

-1

Then the output should be:

Number of 4’s: 1

Number of 20’s: 2

Number of 30’s: 3

I can't quite get my output to look like that:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````/* *************************************** This program will display the histogram of student grades for an assignment ***************************************/ #include #include #include #include using namespace std; const int SIZE = 100; int main() { //Create pointer int* p; //Variable declaration int max = 0; int inputVal; p = new int[SIZE]; for(int i = 0; i < SIZE; i++) { p[i] = 0; } //Prompt the user to enter an input value cout << "Enter grades of the student (-1 to QUIT) \n"; do { //Read the input value from the keyboard cin >> inputVal; //Compare input value with Max value if(inputVal > max) //assign input value to maximum value max = inputVal; if(inputVal >= 0) //Determines number of times grade is entered by the user p[inputVal]++; }while (inputVal != -1); //Display output for(int j = 0; j <= max; j++) cout << "Number of " << j << "'s: " << p[j] << endl; getch(); return 0; }//End Main ``````

 ```Enter grades of the student (-1 to QUIT) 20 30 4 20 30 30 -1 Number of 0's: 0 Number of 1's: 0 Number of 2's: 0 Number of 3's: 0 Number of 4's: 1 Number of 5's: 0 Number of 6's: 0 Number of 7's: 0 Number of 8's: 0 Number of 9's: 0 Number of 10's: 0 Number of 11's: 0 Number of 12's: 0 Number of 13's: 0 Number of 14's: 0 Number of 15's: 0 Number of 16's: 0 Number of 17's: 0 Number of 18's: 0 Number of 19's: 0 Number of 20's: 2 Number of 21's: 0 Number of 22's: 0 Number of 23's: 0 Number of 24's: 0 Number of 25's: 0 Number of 26's: 0 Number of 27's: 0 Number of 28's: 0 Number of 29's: 0 Number of 30's: 3 ```

I realize vectors are MIA, but I do have a failed attempt of using them which seemed at least just as far off from desired result, if not more off:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````/* *************************************** This program will display the histogram of student grades for an assignment ***************************************/ #include #include #include #include using namespace std; const int SIZE = 100; int main() { //Create pointer int *p; //Variable declaration int max = 0; int inputVal; p = new int[SIZE]; vector values; for(int i = 0; i < SIZE; i++) { p[i] = 0; } //Prompt the user to enter an input value cout << "Enter grades of the student (-1 to QUIT) \n"; do { //Read the input value from the keyboard cin >> inputVal; values.push_back(inputVal); //Compare input value with Max value if(inputVal > max) //assign input value to maximum value max = inputVal; if(inputVal >= 0) //Determines number of times grade is entered by the user p[inputVal]++; }while (inputVal != -1); //Display output for(int j = 0; j < values.size(); j++) cout << "Number of " << values[j] << "'s: " << p[j] << endl; delete p; getch(); return 0; }//End Main ``````

I feel I'm very close ... I probably just need to get something to eat, and get back to this, but if you can polish this code up for me, I'd surely appreciate it?
Without testing, using new features of C++ (except the range-based for statement) and without standard algorithm

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````*************************************** This program will display the histogram of student grades for an assignment ***************************************/ #include #include #include using namespace std; int main() { vector values; int inputVal; //Prompt the user to enter an input value cout << "Enter grades of the student (-1 to QUIT) \n"; while ( cin >> inputVal && inputVal != -1 ) { values.push_back(inputVal); } int max = 0; for ( int x : values ) { if ( max < x ) max = x; } //Create pointer int *p = new int[max]; for ( int i = 0; i < max; i++ ) { p[i] = 0; } for ( int x : values ) { if ( x >= 0 ) ++p[x]; } //Display output for( int j = 0; j < max; j++) { if ( p[j] != 0 ) cout << "Number of " << j << "'s: " << p[j] << endl; } delete []p; getch(); return 0; } ``````
Last edited on

That looks good, but I'm sure I'm overlooking something with the array in this for loop:

 ``12345`` `````` //Display output for( int j = 0; j < max; j++) { if ( p[j] != 0 ) cout << "Number of " << j << "'s: " << p[j] << endl; }``````

 ``12`` `````` //Create pointer int p = new int[max];``````

there shall be

 ``12`` `````` //Create pointer int *p = new int[max];``````

With the update it should be compiled and executed.

As for this loop

 ``12345`` `````` //Display output for( int j = 0; j < max; j++) { if ( p[j] != 0 ) cout << "Number of " << j << "'s: " << p[j] << endl; }``````

when we simply traverse the dynamic array to see what values of the vector were registered in the array ( p[j] != 0 ) and display them. If for example for some j p[j] == 0 then it means that the vector had no such value among its elements.
Last edited on
Topic archived. No new replies allowed.