Hi, my assignment asked me to make a 3 row, 7 column array that asked the user to input amount of food for 3 monkeys. 7 represents the number of days. It should tell the user the average, highest and lowest amount for this..

Here is what I've got so far... but it seems to crash when I run it... Can anyone help me identify the problem?

#include <iostream>
#include <iomanip>
using namespace std;

//Global Variable
const int COLS = 7;
//Function prototypes
double getAverage (double pounds[][COLS], int rows);
double getHighest(double pounds[][COLS], int rows);
double getLowest (double pounds[][COLS], int rows);

int main()
{
	const int NO_MONKEY = 3;
	const int NO_DAYS = 7;
	char day[7][11] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
	double food[NO_MONKEY][NO_DAYS], average, lowest, highest;

	//Input amount of food
	for(int monkey = 0; monkey < NO_MONKEY; monkey++)
	{
		cout << "Enter pounds of food for Monkey " << monkey + 1 << ": \n";
		for(int i = 0; i < NO_DAYS; i++)
		{
			cout << setw(11) << left << day[i] << ": ";
			cin >> food[monkey][i];

			//Input validation
			while(food[monkey][i] < 0)
			{
				cout << "Invalid Entry! Do not enter negative value for amount of food!\n\a";
				cout << setw(11) << left << day[i] << ": ";
				cin >> food[monkey][i];
			}
		}
		cout << endl;
	}

	//Call functions
	highest = getHighest(food, NO_MONKEY);
	average = getAverage(food, NO_MONKEY);
	lowest = getLowest(food, NO_MONKEY);

	//Display report
	cout << "This is the report: \n";
	cout << "Least amount of food eaten by any monkey           : " << lowest << " pounds\n";
	cout << "Greatest amount of food eaten by any monkey        : " << highest << " pounds\n";
	cout << "Average amount of food eaten per day by all monkeys: " << average << " pounds\n";

return 0;
}

double getHighest(double pounds[][COLS], int rows)
{
	double highest;

	//Initialization for lowest
	highest = pounds[0][0];

	//Check other values for lowest
	for(int i = 0; i < rows; i++)
	{
		for(int z = 0 ; z < COLS; i++)
			pounds[i][z] > highest? highest = pounds[i][z]: highest = highest;
	}

		return highest;
}

double getAverage(double pounds[][COLS], int rows)
{
	double averageFood, total = 0;

	for(int i = 0; i < rows; i++)
	{
		for(int z = 0 ; z < COLS; i++)
			total += pounds[i][z];
	}

	//Calculate average
	averageFood = total / 7.0;

	return averageFood;
}


double getLowest(double pounds[][COLS], int rows)
{
	double lowest;

	//Initialization for lowest
	lowest = pounds[0][0];

	//Check other values for lowest
	for(int i = 0; i < rows; i++)
	{
		for(int z = 0 ; z < COLS; i++)
			pounds[i][z] < lowest? lowest = pounds[i][z]: lowest = lowest;
	}

		return lowest;
}

Recommended Answers

All 2 Replies

Check lines 64, 77, and 98.

Oh. I was careless...
Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.