0

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.

Barry
  • 286,269
  • 29
  • 621
  • 977
Sam
  • 31
  • 1
  • 6
  • 1
    Hint: Think about the `cin`s and `push_back`s that happen when `n == 1`. What are you doing? What *should* you be doing? – Barry May 26 '15 at 03:14

4 Answers4

2

The logic in these lines is incorrect. Let's say n is 1.

cin >> value; // Read the first number

for (int i=0; i<n; i++)
{
    grades.push_back(value); // Push the first number
    cin >> value;            // Unnecessary input, which doesn't get used.
}

You need:

for (int i=0; i<n; i++)
{
   cin >> value;
   grades.push_back(value);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Problems:

  1. Illogical reading code. You read the first grade outside of the for-loop, but then unconditionally read a grade inside the for-loop after pushing the previous grade onto the vector. You need to read one grade and push it in every iteration; there should be no reads before or after the loop.
  2. Not initializing the new int array to zeroes. This can be done with an initializer, meaning a pair of parentheses. See Operator new initializes memory to zero.
  3. Omitting the braces around the final if-block inside the histogram-printing loop. You need the braces, because nothing should happen inside that loop unless the count is non-zero for the current iteration.
  4. Unused variable declaration char stuff;. Also, if you fix #3, then this would become a redeclaration and would cause a compiler error.

Fixed code:

#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>

using namespace std;

int main() {

    int n;
    int value;
    vector<int> grades;

    cout << "Enter number of grades:" << endl;
    cin >> n;

    cout << "Enter grades: " << endl;
    for (int i = 0; i < n; ++i) {
        cin >> value;
        grades.push_back(value);
    } // end for

    int max = 0;
    for (int i = 0; i < grades.size(); ++i)
        if (grades[i] > max)
            max = grades[i];

    int* count = new int[max+1]();

    for (int i = 0; i < grades.size(); ++i)
        ++count[grades[i]];

    cout << "The histogram is:" << endl;
    for (int i = 0; i <= max; ++i) {
        if (count[i] != 0) {
            std::string stuff(count[i],'*');
            cout << i << " " << stuff << std::endl;
        } // end if
    } // end for

    return 0;

} // end main()
Community
  • 1
  • 1
bgoldst
  • 34,190
  • 6
  • 38
  • 64
0
cin >> value;    // input:1

for (int i=0; i<n; i++) 
{
    grades.push_back(value);
    cin >> value;// input:5,10,10,10, this is why you need to enter extra number(10),
                 // but the last number is never used
}
zicai
  • 3
  • 2
-1

Add = sign in the for condition i.e 1<=n May this solve your issue.

for (int i=0; i<=n; i++)