# Problem with arrays.

• April 5th, 2014, 02:31 AM
ecwolf
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; }```
• April 5th, 2014, 03:11 AM
VictorN
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?
• April 5th, 2014, 06:42 AM
GCDEF
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.
• April 5th, 2014, 08:19 AM
2kaud
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.