Histogram Hysteria
Feb 10, 2017 at 2:47am UTC
I am making a program where the user inputs grades. Once they input -1, a histogram is printed. However it works the first time in a category but then it goes up one.
For example:
Input grades. Enter -1 to stop. Then I will print a histogram.
5
4
3
100
-1
0-9 : *************
10-19 :
20-29 :
30-39 :
40-49 :
50-59 :
60-69 :
70-79 :
80-89 :
90-100 : **
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
#include <iostream>
#include <vector>
using namespace std;
int main() {
int input = 0;
int zeroToNine = 0;
int tenToNineteen = 0;
int twentyToTwentynine = 0;
int thirtyToThirtynine = 0;
int fourtyToFourtnine = 0;
int fiftyToFiftynine = 0;
int sixtyToSixtynine = 0;
int seventyToSeventynine = 0;
int eightyToEightynine = 0;
int ninetyToHundred = 0;
vector<int >Grades;
cout << "Input grades. Enter -1 to stop. Then I will print a histogram.\n" ;
while (input!=-1){
input = 0;
cin >> input;
Grades.push_back(input);
for (int i = 0; i<Grades.size();i++){
if (Grades[i]<10 && Grades[i]>-1){
zeroToNine++;
}
if (Grades[i]<20 && Grades[i]>9){
tenToNineteen++;
}
if (Grades[i]<30 && Grades[i]>19){
twentyToTwentynine++;
}
if (Grades[i]<40 && Grades[i]>29){
thirtyToThirtynine++;
}
if (Grades[i]<50 && Grades[i]>39){
fourtyToFourtnine++;
}
if (Grades[i]<60 && Grades[i]>49){
fiftyToFiftynine++;
}
if (Grades[i]<70 && Grades[i]>59){
sixtyToSixtynine++;
}
if (Grades[i]<80 && Grades[i]>69){
seventyToSeventynine++;
}
if (Grades[i]<90 && Grades[i]>79){
eightyToEightynine++;
}
if (Grades[i]<101 && Grades[i]>89){
ninetyToHundred++;
}
}
}
cout << "\n0-9 : " ;
for (int i=0;i<zeroToNine;i++){
cout << "*" ;
}
cout << "\n10-19 : " ;
for (int i=0;i<tenToNineteen;i++){
cout << "*" ;
}
cout << "\n20-29 : " ;
for (int i=0;i<twentyToTwentynine;i++){
cout << "*" ;
}
cout << "\n30-39 : " ;
for (int i=0;i<thirtyToThirtynine;i++){
cout << "*" ;
}
cout << "\n40-49 : " ;
for (int i=0;i<fourtyToFourtnine;i++){
cout << "*" ;
}
cout << "\n50-59 : " ;
for (int i=0;i<fiftyToFiftynine;i++){
cout << "*" ;
}
cout << "\n60-69 : " ;
for (int i=0;i<sixtyToSixtynine;i++){
cout << "*" ;
}
cout << "\n70-79 : " ;
for (int i=0;i<seventyToSeventynine;i++){
cout << "*" ;
}
cout << "\n80-89 : " ;
for (int i=0;i<eightyToEightynine;i++){
cout << "*" ;
}
cout << "\n90-100 : " ;
for (int i=0;i<ninetyToHundred;i++){
cout << "*" ;
}
}
So, I think it may be due to my for() loops, but I've been stuck on this a while and decided it was time to ask for help.
Last edited on Feb 10, 2017 at 2:58am UTC
Feb 10, 2017 at 3:15am UTC
You forgot to print new line after you print a line of '*'.
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
cout << "\n0-9 : " ;
for (int i=0;i<zeroToNine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n10-19 : " ;
for (int i=0;i<tenToNineteen;i++){
cout << "*" ;
}
cout << endl;
cout << "\n20-29 : " ;
for (int i=0;i<twentyToTwentynine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n30-39 : " ;
for (int i=0;i<thirtyToThirtynine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n40-49 : " ;
for (int i=0;i<fourtyToFourtnine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n50-59 : " ;
for (int i=0;i<fiftyToFiftynine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n60-69 : " ;
for (int i=0;i<sixtyToSixtynine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n70-79 : " ;
for (int i=0;i<seventyToSeventynine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n80-89 : " ;
for (int i=0;i<eightyToEightynine;i++){
cout << "*" ;
}
cout << endl;
cout << "\n90-100 : " ;
for (int i=0;i<ninetyToHundred;i++){
cout << "*" ;
}
cout << endl;
Line 23 : You are checking elements that have already been checked. One way to fix this :
for (int i = Grades.size() - 1; i < Grades.size();i++){
Last edited on Feb 10, 2017 at 3:16am UTC
Feb 10, 2017 at 3:25am UTC
Thanks Mantorr22. Simple fix :)
Jared
Topic archived. No new replies allowed.