Problem with arrays.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Problem with arrays.

  1. #1
    Join Date
    Apr 2014
    Posts
    1

    Problem with arrays.

    After compiling, I cannot understand why my getMost and getLeast functions are returning erroneous values. I've done everything I could think of to try and fix this but I'm completely lost at this point and could really use some help. Problem and code below.

    A local zoo wants to keep track of how many pounds of food each of its three monkeys eats
    each day during a typical week. Write a program that stores this information in a twodimensional
    3 * 7 array, where each row represents a different monkey and each column
    represents a different day of the week. The program should rst have the user input the data
    for each monkey. Then it should create a report that includes the following information:

    * Average amount of food eaten per day by the whole family of monkeys.
    * The least amount of food eaten during the week by any one monkey.
    * The greatest amount of food eaten during the week by any one monkey.

    Input Validation: Do not accept negative numbers for pounds of food eaten.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Constants.
    const int NUM_MONKEYS = 3;
    const int NUM_DAYS = 7;
    
    // Function prototypes.
    void getData(int[][NUM_DAYS], int);
    int getAverage(int[][NUM_DAYS], int);
    int getMost(int[][NUM_DAYS], int);
    int getLeast(int[][NUM_DAYS], int);
    
    int main()
    {
    	// Variables to hold average eaten per day, lowest eaten per week
    	// by anyone monkey, and highest eaten per week by any one monkey.
    	int average,
    		lowest,
    		highest;
    
    	// Array to hold data.
    	int foodPerWeek[NUM_MONKEYS][NUM_DAYS];
    
    	// Get the amount eaten per day for all 3 monkeys.
    	getData(foodPerWeek, NUM_MONKEYS);
    
    	// Get the average eaten per day for entire family of monkeys.
    	average = getAverage(foodPerWeek, NUM_MONKEYS);
    
    	// Get the least amount of food eaten during the week by
    	// anyone one monkey.
    	lowest = getLeast(foodPerWeek, NUM_MONKEYS);
    
    	// Get the most amount of food eaten during the week by anyone
    	// one monkey.
    	highest = getMost(foodPerWeek, NUM_MONKEYS);
    
    	// Print to console the (1) average eaten per day by entire
    	// family of monkeys, (2) least amount of food eaten during 
    	// the week by any one monkey, and (3) most amount of food
    	// eaten per week by any one monkey.
    	cout << endl << "Average per day: " << average << endl;
    	cout << "Least per week: " << lowest << endl;
    	cout << "Most per week: " << highest << endl;
    
    	return 0;
    }
    
    // Function getData.
    void getData(int data[][NUM_DAYS], int rows)
    {
    	for (int x=0; x<rows; x++)
    	{
    		cout << "Enter amount eaten per day for Monkey #" << (x+1) << endl;
    		for (int y=0; y<NUM_DAYS; y++)
    		{
    			cout << "Day " << (y+1) << ": ";
    			cin >> data[x][y];
    		}
    	}
    }
    
    // Function getAverage.
    int getAverage(int data[][NUM_DAYS], int rows)
    {
    	int total = 0,
    		average;
    
    	for (int x=0; x<rows; x++)
    	{
    		for (int y=0; y<NUM_DAYS; y++)
    			total += data[x][y];
    	}
    
    	average = (total / NUM_DAYS);
    
    	return average;
    }
    
    // Function getLeast.
    int getLeast(int data[][NUM_DAYS], int rows)
    {
    	int monkey[NUM_MONKEYS];
    	int least = monkey[0];
    
    	for (int x=0; x<rows; x++)
    	{
    		for (int y=0; y<NUM_DAYS; y++)
    		{
    			monkey[x] += data[x][y];
    		}
    
    		if (monkey[x] <= least)
    			least = monkey[x];
    	}
    
    	return least;
    }
    
    // Function getMost.
    int getMost(int data[][NUM_DAYS], int rows)
    {
    	int monkey[NUM_MONKEYS];
    	int most = monkey[0];
    
    	for (int x=0; x<rows; x++)
    	{
    		for (int y=0; y<NUM_DAYS; y++)
    		{
    			monkey[x] += data[x][y];
    		}
    
    		if (monkey[x] >= most)
    			most = monkey[x];
    	}
    
    	return most;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,354

    Re: Problem with arrays.

    Did you try to debug your code (step-by-step) to understand what, where and why goes wrong or not expected?
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Problem with arrays.

    I compiled your code and got a warning from the compiler on this line
    int least = monkey[0];
    that you were using an unitialized variable. That would be a good place to start looking, but as Victor said, you'll need to learn to use the debugger.

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,327

    Re: Problem with arrays.

    Code:
    int monkey[NUM_MONKEYS];
    You are using monkey without initialising its elements. Try
    Code:
    int monkey[NUM_MONKEYS] = {0};
    which initialises all its elements to 0. If a variable is defined and used before its value has been set then the value of the variable is undefined as it's whatever happens to be in memory at the location used for the variable.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center