Vertical Histogram Question

Hi, I am new here and in my first year of Uni. One of my modules is C++ and I am having a little trouble doing a small task. I really need a reply ASAP so thanks in advance.

Anyway, here is problem. I want to create a vertical histogram in my code. I already made it go vertical but not the way I want it.

Example:
I want it like this:

Range1 Range2 Range3 Range4

And asterisks under each one, depending on the user input. (My code is below and doing it on here doesn't make it come out correctly)

But what I've managed to do is this:

Range1
*
*
*

Range2
*
*

Range3
*
*
*
*

Range4
*
*

Which I don't want. Any ideas?

I want everything else to stay pretty much the same since I can only use some features such as Arrays and really basic functions.

Here is my code: (Worked fine last time I used it and I am doing it on Visual Studio 2010 (at uni) and 2013 (on my laptop)).
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream> //Start of code

using namespace std;


	int MarkValueInput; //Mark entered by user

	//Counter variables for ranges in While Loop
	int counterlow; //Counter for low range
	int countermidlow; //Counter for mid-low range
	int countermidhigh; //Counter for mid-high range
	int counterhigh; //Counter for high range

	//Counter variables in For Loop to print stars
	int starcounterlow; //Star Counter for low range
	int starcountermidlow; //Star Counter for mid-low range
	int starcountermidhigh; //Star Counter for mid-high range
	int starcounterhigh; //Star Counter for high range

	//Counter for each student starting at 1
	int students = 1; 

	//total students
	int totalstudents;

	//Counters for extras part 1
	double avgmark; //Average marks
	int num_pass_students; //Number of students passed
	int num_fail_students; //Number of students failed
	int highmark; //Highest mark
	int lowmark; //Lowest mark
	double mark_sum; //Total marks

int main () {
	
	mark_sum = 0; //Total marks initialised as "0"
	highmark = 0; //Highest mark initialised as "0" to make it easier to find the highest mark
	lowmark = 100; //Lowest mark initialised as "100" to make it easier to find the lowest mark

	cout << "Enter the student results below:" << endl; 
	cout << "Please enter mark between 0 - 100 for student " << students << endl;
	cout << "To stop, enter value above the maximum of 100" << endl;
	cin >> MarkValueInput; //Input mark for student

	while (MarkValueInput <=100){ //While Loop begins
	
		mark_sum = mark_sum + MarkValueInput; //Value inputed is added on Total marks and is done each time mark in inputed
		if (MarkValueInput > highmark) 
		highmark = MarkValueInput; //If mark inputted is greater than highest mark then it becomes the new highest mark and it is done with each input
		if (MarkValueInput < lowmark)
		lowmark = MarkValueInput; //If mark inputted is smaller than lowest mark then it becomes the new lowest mark and it is done with each input

		if ((MarkValueInput >=0) && (MarkValueInput <=29)) { //IF block 1 begins
		students++; //Increment of students
		counterlow++; //Increment of counter
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;
	
		} //IF block 1 ends

		else if ((MarkValueInput >=30) && (MarkValueInput <=39)) { //IF block 2 begins
		students++;
		countermidlow++;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;
	
		} //IF block 2 ends
	
		else if ((MarkValueInput >=40) && (MarkValueInput <=69)) { //IF block 3 begins
		students++;
		countermidhigh++;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;
	
		} //IF block 3 ends
	
		else if ((MarkValueInput >=70) && (MarkValueInput <=100)) { //IF block 4 begins
		students++;
		counterhigh++;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;

		} //IF block 4 ends
	
		else { //If value is not greater than 100 and is smaller than 0, then the value will not be counted in any of the ranges
		cout << "WARNING: NEGATIVE VALUE ENTERED!" << endl;
		cout << "Input value between 0 - 100" << endl;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;

		} //ELSE block ends

	} //End of While Loop

	cout << endl << endl; //For Loop begins
	cout << "0-29     " << endl;
	for (starcounterlow = 1; starcounterlow <= counterlow; starcounterlow++) {cout << "* " << endl;} //Number of asterisks needed for low range which is equal to counterlow

	cout  << endl << endl;
	cout << "30-39    " << endl;
	for (starcountermidlow = 1; starcountermidlow <= countermidlow; starcountermidlow++) {cout << "* " << endl;} //Number of asterisks needed for mid-low range which is equal to countermidlow

	cout << endl << endl;
	cout << "40-69    " << endl;
	for (starcountermidhigh = 1; starcountermidhigh <= countermidhigh; starcountermidhigh++) {cout << "* " << endl;} //Number of asterisks needed for mid-high range which is equal to countermidhigh

	cout << endl << endl;
	cout << "70-100   " << endl;
	for (starcounterhigh = 1; starcounterhigh <= counterhigh; starcounterhigh++) {cout << "* " << endl;} //Number of asterisks needed for high range which is equal to counterhigh

	cout << endl;
	totalstudents = counterlow + countermidlow + countermidhigh + counterhigh; //Total students is equal to the sum of the range counters.
	cout << endl << endl;
	cout << "Total student(s) counted:" << totalstudents << endl; //End of For Loop
	cout << endl;

	cout << "Highest Mark = " << highmark << endl; //Output for highest mark
	cout << endl;

	cout << "Lowest Mark = " << lowmark << endl; //Output for lowest mark
	cout << endl;

	avgmark = mark_sum / totalstudents; //Average mark is found by total marks divided by total students
	cout << "Average Mark = " << avgmark << endl << endl; //Output for average mark

	num_pass_students = countermidhigh + counterhigh; //Number of students passed is found by summing mid-high and high counters
	num_fail_students = counterlow + countermidlow; //Number of students failed is found by summing mid-low and low counters
	cout << "Number of passed student(s) = " << num_pass_students << endl << endl; //Number of students passed it outputted here
	cout << "Number of failed student(s) = " << num_fail_students << endl << endl; //Number of students failed it outputted here

	return 0;

} //End of code 
Last edited on
Declare all your variables inside function (main in your case). Very bad habit to use global variables
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
#include<iostream>
using namespace std;

int main()
{
  int low=5,midlow=7,midhigh=3,high=4;
  int max=low;
  if (midlow>low) max=midlow;
  if (midhigh>low) max=midhigh;
  if (high>low) max=high;
  
  cout<<"0-29    30-39   40-69   70-100"<<endl;
  for (int i=1;i<=max;i++)
  {
    if (i<=low)
      cout<<"   *    ";
    else
      cout<<"        ";
    if (i<=midlow)
      cout<<"   *    ";
    else
      cout<<"        ";
    if (i<=midhigh)
      cout<<"   *    ";
    else
      cout<<"        ";
    if (i<=high)
      cout<<"   *    ";
    else
      cout<<"        ";
    cout<<endl;
  }
}


1
2
3
4
5
6
7
8
0-29    30-39   40-69   70-100
   *       *       *       *    
   *       *       *       *    
   *       *       *       *    
   *       *               *    
   *       *                    
           *                    
           *                    
Thanks for the reply Ats15.
Declare all your variables inside function (main in your case). Very bad habit to use global variables


I can't really declare the variables for those counters since I don't know what they will be. This is supposed to be user input. (At least that's what the sheet says). Is it still possible to do this without declaring them myself?

Thanks.
I can't really declare the variables for those counters since I don't know what they will be. This is supposed to be user input. (At least that's what the sheet says). Is it still possible to do this without declaring them myself?


Of course you can declare them inside main. As long as you declare them before you use them, then what's the problem?

Global variables are a bad idea for many reasons. Get out of the habit of using them as fast as you can.
I kinda of have to use Global variables. My professor said that's what the Examiner wants and the guying marking it is an ass. He failed some students in the module last year because of they didn't use Global variables.

Anyway, I took what was told to me earlier and did this instead: (And it seems to be working - I tested this about 20ish times before I concluded that it did what I needed it to).

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream> //Start of code

using namespace std;


	int MarkValueInput; //Mark entered by user

	//Counter variables for ranges in While Loop
	int counterlow; //Counter for low range
	int countermidlow; //Counter for mid-low range
	int countermidhigh; //Counter for mid-high range
	int counterhigh; //Counter for high range

	//Counter variables in For Loop to print stars
	int starcounterlow; //Star Counter for low range
	int starcountermidlow; //Star Counter for mid-low range
	int starcountermidhigh; //Star Counter for mid-high range
	int starcounterhigh; //Star Counter for high range

	//Counter for each student starting at 1
	int students = 1; 

	//total students
	int totalstudents;

	//Counters for extras part 1
	double avgmark; //Average marks
	int num_pass_students; //Number of students passed
	int num_fail_students; //Number of students failed
	int highmark; //Highest mark
	int lowmark; //Lowest mark
	double mark_sum; //Total marks

int main () {
	
	mark_sum = 0; //Total marks initialised as "0"
	highmark = 0; //Highest mark initialised as "0" to make it easier to find the highest mark
	lowmark = 100; //Lowest mark initialised as "100" to make it easier to find the lowest mark

	cout << "Enter the student results below:" << endl; 
	cout << "Please enter mark between 0 - 100 for student " << students << endl;
	cout << "To stop, enter value above the maximum of 100" << endl;
	cin >> MarkValueInput; //Input mark for student

	while (MarkValueInput <=100){ //While Loop begins
	
		mark_sum = mark_sum + MarkValueInput; //Value inputted is added on Total marks and is done each time mark in inputted
		if (MarkValueInput > highmark) 
		highmark = MarkValueInput; //If mark inputted is greater than highest mark then it becomes the new highest mark and it is done with each input
		if (MarkValueInput < lowmark)
		lowmark = MarkValueInput; //If mark inputted is smaller than lowest mark then it becomes the new lowest mark and it is done with each input

		if ((MarkValueInput >=0) && (MarkValueInput <=29)) { //IF block 1 begins
		students++; //Increment of students
		counterlow++; //Increment of counter
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;
	
		} //IF block 1 ends

		else if ((MarkValueInput >=30) && (MarkValueInput <=39)) { //IF block 2 begins
		students++;
		countermidlow++;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;
	
		} //IF block 2 ends
	
		else if ((MarkValueInput >=40) && (MarkValueInput <=69)) { //IF block 3 begins
		students++;
		countermidhigh++;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;
	
		} //IF block 3 ends
	
		else if ((MarkValueInput >=70) && (MarkValueInput <=100)) { //IF block 4 begins
		students++;
		counterhigh++;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;

		} //IF block 4 ends
	
		else { //If value is not greater than 100 and is smaller than 0, then the value will not be counted in any of the ranges
		cout << "WARNING: NEGATIVE VALUE ENTERED!" << endl;
		cout << "Input value between 0 - 100" << endl;
		cout << "Please enter mark for student " << students << endl;
		cin >> MarkValueInput;

		} //ELSE block ends

	} //End of While Loop

	cout << endl << endl; //For Loop begins
	cout << "0-29     " << endl;
	for (starcounterlow = 1; starcounterlow <= counterlow; starcounterlow++) {cout << "* ";} //Number of asterisks needed for low range which is equal to counterlow

	cout  << endl << endl;
	cout << "30-39    " << endl;
	for (starcountermidlow = 1; starcountermidlow <= countermidlow; starcountermidlow++) {cout << "* ";} //Number of asterisks needed for mid-low range which is equal to countermidlow

	cout << endl << endl;
	cout << "40-69    " << endl;
	for (starcountermidhigh = 1; starcountermidhigh <= countermidhigh; starcountermidhigh++) {cout << "* ";} //Number of asterisks needed for mid-high range which is equal to countermidhigh

	cout << endl << endl;
	cout << "70-100   " << endl;
	for (starcounterhigh = 1; starcounterhigh <= counterhigh; starcounterhigh++) {cout << "* ";} //Number of asterisks needed for high range which is equal to counterhigh

	cout << endl;
	totalstudents = counterlow + countermidlow + countermidhigh + counterhigh; //Total students is equal to the sum of the range counters.
	cout << endl << endl;
	cout << "Total student(s) counted:" << totalstudents << endl; //End of For Loop
	cout << endl;

	cout << "Highest Mark = " << highmark << endl; //Output for highest mark
	cout << endl;

	cout << "Lowest Mark = " << lowmark << endl; //Output for lowest mark
	cout << endl;

	avgmark = mark_sum / totalstudents; //Average mark is found by total marks divided by total students
	cout << "Average Mark = " << avgmark << endl << endl; //Output for average mark

	num_pass_students = countermidhigh + counterhigh; //Number of students passed is found by summing mid-high and high counters
	num_fail_students = counterlow + countermidlow; //Number of students failed is found by summing mid-low and low counters
	cout << "Number of passed student(s) = " << num_pass_students << endl << endl; //Number of students passed is outputted here
	cout << "Number of failed student(s) = " << num_fail_students << endl << endl; //Number of students failed is outputted here

	int max = counterlow;
	if (countermidlow > counterlow) max = countermidlow;
	if (countermidhigh > counterlow) max = countermidhigh;
	if (counterhigh > counterlow) max = counterhigh;

	cout << "  0-29    30-39   40-69   70-100  " << endl;
	for (int i = 1; i <= max; i++)
	{
		if (i <= counterlow)
			cout << "   *    ";
		else
			cout << "        ";
		if (i <= countermidlow)
			cout << "   *    ";
		else
			cout << "        ";
		if (i <= countermidhigh)
			cout << "   *    ";
		else
			cout << "        ";
		if (i <= counterhigh)
			cout << "   *    ";
		else
			cout << "        ";
		cout << endl;
	}
} //End of code 


I am going to put comments in as well so my professor knows what I am doing. I might also have to put in a reference so would you mind if I linked this page if that occurs? Credit would go to those who answered the question of course. I just get a slightly lower mark.

EDIT: This is the end result: http://imgur.com/1SLbzsl

The histogram at the bottom is what I wanted.

Thanks guys. Much appreciated.
Last edited on
Topic archived. No new replies allowed.