Arrays & Functions help

trouble finding the least and greatest amount

Page 1 of 1

1 Replies - 8978 Views - Last Post: 10 April 2009 - 10:49 AM Rate Topic: -----

#1 Captain__stabin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 06-March 09

Arrays & Functions help

Posted 10 April 2009 - 09:04 AM

Assignment:
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 two-dimensional 3 x 7 array, where each row represents a monkey and each column represents a different day of the week. The program should first have the user input the data for each monkey. Then it should create a report (screen and file) 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 (out of all the monkeys)
•The most amount of food eaten during the week by any one monkey (out of all the monkeys)
Do not accept negative numbers for the amount of food eaten.

********** I have the average correct, but im having some trouble in the function finding the least amount and most amount of food eating during the week by any one monkey. I'll gladly appreciate any help guys. Thanks.
Here's what I've have so far :

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

// Constants
const int MONKEYS = 3;
const int DAYS = 7;

//function prototypes
double familyAverage(const double mf[MONKEYS][DAYS]);
double littleMeal(const double mf[MONKEYS][DAYS]);
double bigMeal(const double mf[MONKEYS][DAYS]);


int main(int argc, char *argv[])
{ 
   
   
	
	double monkey_table [MONKEYS][DAYS]={0};
	int x, y;	   
	
				  
   cout << showpoint << fixed << setprecision(2) << endl;
   
   
   cout << "\t\t\t\tMonkey business " << endl;
   cout << endl;
   
  

   for (x = 0; x < MONKEYS; x++)
   {
	  for (y = 0; y < DAYS; y++)
	  {
		do
		{	 
			cout << " \tEnter the number of pounds eaten for";	 
			cout << " Monkey # " << x+1 << " ";
			cout << " Day " << y+1 << ": "; 
			cin >> monkey_table [x][y]; 
			
			
			

		 if(monkey_table [x][y] < 0)
			cout << "\t Please enter a positive number for pounds of food." << endl; 

		 }
		 while(monkey_table [x][y] < 0);
	  
	  }
	  cout << endl;

   }
	
	cout << "\t************************************************************" << endl;
	cout << " \t\tThe average amount of food eaten per day" << endl;
	cout << " \t\tby the whole family of monkeys "; 
	cout << " is " << familyAverage(monkey_table)<< " lbs" << endl;
	cout << endl;
   
	cout << " \t\tThe minimum amount of food eaten during" << endl; 
	cout << "\t\tthe week by any one monkey is " << littleMeal( monkey_table ) << " lbs" << endl;
	cout << endl;
	
	cout << "\t\tThe most amount of food eaten during the" << endl;
	cout << "\t\tweek by any one monkey is " << bigMeal( monkey_table ) << " lbs" << endl;
	cout << endl;
	
	
	
	system("PAUSE");
	return EXIT_SUCCESS;
}


double familyAverage(const double mf[MONKEYS][DAYS])
{
	double total = 0;

	for (int x = 0; x < MONKEYS; x++)
			for (int y = 0; y < DAYS; y++)
				total += mf[x][y];
			total= (total / DAYS) / 3;
			
	return total;
}


double littleMeal(const double mf[MONKEYS][DAYS])
{
	double small= INT_MAX;

	for (int x = 0; x < MONKEYS; x++)
			for (int y = 0; y < DAYS; y++)
				if (mf[x][y] < small)
					small= mf[x][y];
					
	return small;
}


double bigMeal(const double mf[MONKEYS][DAYS])
{
	double big= 0;

	for (int x = 0; x < MONKEYS; x++)
			for (int y = 0; y < DAYS; y++)
				if (mf[x][y] > big)
					big= mf[x][y];
					
	return big;
}


This post has been edited by Captain__stabin: 10 April 2009 - 09:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays & Functions help

#2 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: Arrays & Functions help

Posted 10 April 2009 - 10:49 AM

It's good to see that you have posted your question to the correct forum, i.e. Homework. However, I have already answered in this post:
Answer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1