このコードは、2 次元配列を使用して 3 匹のサルが 1 週間にわたって食べた食物を取得し、平均、1 日の最小摂取量、および 1 日の最大摂取量を見つけることになっています。最小関数は、1 匹のサルが 1 日に食べた最低量を出力し、サルの数、食べたポンド数、および日数を含むことになっています。最高機能も同様。しかし、正しく計算されていません。関数が間違っていたかどうかはわかりません。私がそれを実行すると、「1日目に食べた最低量はサル1でした」と出力され、次に2、3、4などが出力されます.coutをループの外に出そうとしましたが、カウントは初期化されていません
#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;
}
}
}