Thread: Histogram multidimensional array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    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;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    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

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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
    Last edited by jimblumberg; 11-01-2010 at 07:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM