0

The code is supposed to use a 2d array to take the food eaten by 3 monkeys over a week and find the average, lowest eaten on a single day, and highest on a single day. The least function is supposed to output the lowest eaten in one day by a single monkey and include the monkeys number, pounds eaten and the day number. Same with the highest function. But its not calculating correctly, i dont know if i did the function wrong. When i run it it prints out "The lowest amount eaten was monkey 1 on day 1 was 1" then prints out 2, 3 ,4 etc. I tried putting the cout outside the loop but then count isnt initialized

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

//Global Constants
const int NUM_MONKEYS = 3; // 3 rows
const int DAYS = 7; // 7 columns

//Prototypes
void poundsEaten(double[][DAYS],int, int);
void averageEaten(double [][DAYS], int, int);
void least(double [][DAYS], int, int);
void most(double [][DAYS], int, int);

int main()
{
//const int NUM_MONKEYS = 3;
//const int DAYS = 7;
double foodEaten[NUM_MONKEYS][DAYS]; //Array with 3 rows, 7 columns 

poundsEaten(foodEaten, NUM_MONKEYS, DAYS);
averageEaten(foodEaten, NUM_MONKEYS, DAYS);
least(foodEaten, NUM_MONKEYS, DAYS);
most(foodEaten, NUM_MONKEYS, DAYS);

system("pause");
return 0;
}

void poundsEaten(double monkey[][DAYS], int rows, int cols)
{
for(int index = 0; index < rows; index++)
{
    for(int count = 0; count < cols; count++)
    {
        cout << "Pounds of food eaten on day " << (count + 1);
        cout << " by monkey " << (index + 1) << ": ";
        cin >> monkey[index][count];

    }
} 
}

void averageEaten(double monkey[][DAYS], int rows, int cols)
{
for(int count = 0; count < cols; count++)
{
    double total = 0;
    double average;
    for(int index = 0; index < rows; index++)
    {
        total += monkey[index][count]; 

        average = total/rows;
    }
    cout << "The average food eaten on day " << (count + 1)
             <<" is " << average << endl;

}
}

void least(double monkey[][DAYS], int rows, int cols)
{
double lowest = monkey[NUM_MONKEYS][DAYS];
for(int index = 0; index < rows; index++)
{
    for(int count = 0; count < cols; count++)
    {
        if(monkey[index][count] > lowest)
            lowest = monkey[index][count];

        cout << "The lowest amount of food eaten was monkey number 
                       << " " << (index + 1)
               << " On day " << (count + 1) << " was " << lowest;
    }

}


}

void most(double monkey[][DAYS], int rows, int cols)
{
double highest = monkey[NUM_MONKEYS][DAYS];
for(int index = 0; index < rows; index++)
{
    for(int count = 0; count < cols; count++)
    {
        if(monkey[index][count] > highest)
            highest = monkey[index][count];

    cout << "The highest amount of food eaten was monkey number"
         << (index + 1) << " on day " << (count + 1)  << " was "  
                  << highest;
    }
}


}
4

1 回答 1

1

least功能:

if(monkey[index][count] > lowest)
    lowest = monkey[index][count];

在比较中,我确定您的意思是<而不是>.

您还应该保存索引并在循环之后打印,这适用于leastmost

于 2013-04-09T19:27:20.077 回答