954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help: Passing 2D array in function, Highest, Lowest, Average

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;
}
erozero
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Check lines 64, 77, and 98.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Oh. I was careless...
Thanks :)

erozero
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: