-
April 5th, 2014, 02:31 AM
#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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|