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:

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
54
55
56
/*

***************************************
This program will display the histogram
of student grades for an assignment
***************************************/

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <conio.h>

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:

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
54
55
56
57
58
59
60
61
62
/*

***************************************
This program will display the histogram
of student grades for an assignment
***************************************/

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <conio.h>

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<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?
Without testing, using new features of C++ (except the range-based for statement) and without standard algorithm

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
54
55
56
57
***************************************
This program will display the histogram
of student grades for an assignment
***************************************/

#include <iostream>
#include <vector>
#include <conio.h>

using namespace 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 = 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
@vlad from moscow

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

1
2
3
4
5
	//Display output
	for( int j = 0; j < max; j++)
	{
		if (  p[j] != 0 )  cout << "Number of " << j << "'s: " << p[j] << endl;
	}
My original code had a typo. Instead of

1
2
	//Create pointer
	int p = new int[max];


there shall be

1
2
	//Create pointer
	int *p = new int[max];


With the update it should be compiled and executed.

As for this loop

1
2
3
4
5
	//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.