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:
/*
***************************************
This program will display the histogram
of student grades for an assignment
***************************************/
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <conio.h>
usingnamespace std;
constint SIZE = 100;
int main()
{
//Create pointer
int* p;
//Variable declaration
int max = 0;
int inputVal;
p = newint[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:
/*
***************************************
This program will display the histogram
of student grades for an assignment
***************************************/
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <conio.h>
usingnamespace std;
constint SIZE = 100;
int main()
{
//Create pointer
int *p;
//Variable declaration
int max = 0;
int inputVal;
p = newint[SIZE];
vector<int> 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?
***************************************
This program will display the histogram
of student grades for an assignment
***************************************/
#include <iostream>
#include <vector>
#include <conio.h>
usingnamespace std;
int main()
{
vector<int> 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 = newint[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;
}
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.