# Histogram multidimensional array

• 11-01-2010
coolguy
Histogram multidimensional array
Hello I am having trouble initializing all one hundred and one counters to zero when the countscores function begins.

The last function is supposed to use an outer loop that counts the possible scores, while the inner loop counts the number of occurrences of scores entered and prints an asterisk for each.

So far my program prints the possible scores and prints an asterisk next to each score. I am having difficulty making my code print the required number of asterisks dependent on the frequency of entered numbers next to each score.

This is my current code but I have manipulated this for several days only to get this some other variation or infinite loops. I am grateful for any and all help, suggestions, and advise.

This is the output I want
100 *
99
98 ****
97 **
96 ***
95 *
...

This is the output I get
100 *
99 *
98 *
97 *
96 *
95 *
...

Code:

``` #include <iostream> #include <iomanip> #include <cctype> #include <math.h> using namespace std; using std::cout; using std::cin; const int numberOfTests = 4; void countScores (int studentScore[][numberOfTests], int numberOfStundents, int counts[101]); void writeHistogram (int counts[101]); int main () { int studentScore[30][numberOfTests] = {}; int counts[101] = {}; int numberOfStudents = 0; countScores(studentScore, numberOfStudents, counts); cout << endl; writeHistogram(counts); return 0; } void countScores (int studentScore[][numberOfTests], int numberOfStudents, int counts[101]) {    numberOfStudents = 0; cout << "Enter the test scores (-1 to end): "; for (int i =0; i <= numberOfTests-1; i++) { cin >> studentScore[numberOfStudents][i]; } while (studentScore[numberOfStudents][0] != -1) { numberOfStudents++; cout << "Enter the test scores (-1 to end): "; for (int i = 0; i <= numberOfTests-1; i++) { cin >> studentScore[numberOfStudents][i]; } } for (int i = 0; i < numberOfStudents; i++) {    for (int j = 0; j < studentScore[numberOfStudents][i]; j++) return; } } void writeHistogram (int counts[101]) { for (int i = 101; i > 0; i--) { for (int j = 0; j <= counts[j]; j++) { cout << i-1 << ":"; cout << " *"; } cout << endl; } return; }```
• 11-01-2010
jimblumberg
In the following code:

Code:

```void countScores (int studentScore[][numberOfTests], int numberOfStudents, int counts[101]) {         numberOfStudents = 0;         cout << "Enter the test scores (-1 to end): ";         for (int i =0; i <= numberOfTests-1; i++)         {                 cin >> studentScore[numberOfStudents][i];         }         while (studentScore[numberOfStudents][0] != -1)         {                 numberOfStudents++;                 cout << "Enter the test scores (-1 to end): ";                 for (int i = 0; i <= numberOfTests-1; i++)                 {                         cin >> studentScore[numberOfStudents][i];                 }         }         for (int i = 0; i < numberOfStudents; i++)         {                 for (int j = 0; j < studentScore[numberOfStudents][i]; j++)                         return;         } }```
Where are you using counts[]??

Is this not the value you need for:

Code:

`writeHistogram(counts);`
You also need to pick an indentation style and then use it.

Jim
• 11-01-2010
coolguy
The function parameters were given
I am suppsed toinitialize all one hundred and one counters to zero when the function begins.
which is what I previously tried to do with
the counts[] in that function but i am guessing this
is wrong or i was doing it wrong
• 11-01-2010
jimblumberg
In
Code:

`void countScores (int studentScore[][numberOfTests], int numberOfStudents, int counts[101])`
Do you need to count something?? Your function name seems to indicate that.

Maybe scores?? Here again only looking at your function name.

Maybe store that something into counts?? Here I am looking at
Code:

`writeHistogram(counts);`
Which is where you are trying to print your "Histogram" but it contains 0 in every member of counts because you never stored anything else in it.

Jim