Hi my program is supposed ask for an input of grades and then output the number of times those grades occur. The number of times that occurs is shown by tally markers, I am using "*" as the tally marker. My current code outputs this:
Enter number of grades:
4
Enter grades:
1
5
10
10
10
Histogram:
0
1 *
2
3
4
5 *
.
.
10 **
As you can see the number of grades I am asking for is 4, but it requires 5 inputs and doesn't seem to count the 5th. Also it prints out the numbers in between instead of just the scores. What I am trying to achieve is
Enter number of grades:
4
Enter grades:
1
5
10
10
Histogram:
1 *
5 *
10 **
#include<iostream>
#include<vector>
#include<iomanip>
#include<algorithm>
using namespace std;
int main(){
int n;
int value = 0;
vector<int> grades;
cout<<"Enter number of grades:"<<endl;
cin >> n;
cout <<"Enter grades: "<<endl;
cin >> value;
for (int i=0; i<n; i++) // believe this condition is wrong, causing the input extra number. I've tried multiple variations and can not figure it out.
{
grades.push_back(value);
cin >> value;
}
int max = 0;
for(int i=0; i<grades.size(); i++)
{
if(grades[i] > max)
max = grades[i];
}
int* array= new int[max+1];
for(int i=0; i<grades.size(); i++)
{
array[grades[i]]++;
}
cout<<"The histogram is:"<<endl;
for(int i=0; i<=max; i++){
if(array[i]!=0)
char stuff;
std::string stuff(array[i], '*'); //pretty sure this is causing all the numbers to be printed, but I don't know another way to print the asterisk
cout<<i <<" "<<stuff<<std::endl;
}
return 0;
}
Why is it printing all the numbers, when the line to print the asterisk is included? When I take out the line for printing the asterisk, it no longer prints all the numbers, but of course it also does not print the asterisk.