0
#include <iostream>
using namespace std;

const int monkeys = 3;
const int weekdays = 7;
double monkeyWeek[monkeys][weekdays];
double largest;
double least;
double average;
int index;
int dayCount;
double amount;



double amountEaten(double[] [weekdays], int);
double mostEaten (double[] [weekdays],int);
double leastEaten (double[][weekdays], int);

int main(){
cout << "Ch 7-4 Monkey " << endl;
cout << "Created by Aaron Roberts" << endl;

double mostBananas (double[] [weekdays],int);
double leastBananas (double[][weekdays],int);
//double bananaAverage (double[][weekdays], int);



}


double amountEaten(double array[] [weekdays], int){
    cout << "Please enter the amount of food eaten per monkey per day." << endl;
double amount = array[0][0];
for (index = 0; index < monkeys; index++)
{
    for (dayCount = 0; dayCount < weekdays; dayCount++)
    {
    cout << endl <<"Please enter the amount of pounds eaten by     monkey"     
        <<(index +1)
            << endl << "for day " << (dayCount +1) << ": ";
        cin >> monkeyWeek[monkeys] [weekdays] ;
        if (monkeyWeek[monkeys] [weekdays] < 1)
            cout << endl <<"Must feed positive amount" << endl;
    }


}
}

double mostEaten( double array[] [weekdays], int size)
{
double largest = array[0][0];
for (int count = 0; count < size; count++)
{
    for (int col = 0; col < count; col++)
    {
        if (array[count][weekdays] > largest)
            largest = array[count][weekdays];
    }
}
return largest;
}

double leastEaten(double array[] [weekdays], int size)
{
double least = array[0][0];

for (int count = 0; count < size; count++)
{
for (int col = 0; col < size; col++);
{
if (array[count][weekdays] < least)
least = array[count][weekdays];
}
}
return least;
} 

该项目需要使用二维数组来存储 3 只猴子每周 7 天吃掉的食物磅数。

创建一个函数来获取一周中每一天每只猴子吃掉的磅数。创建第二个函数来确定通过数组来计算所有吃的钱的总数,然后是一天吃的平均值。(你们中的一些人将此解释为对所有值求和,然后除以值的数量。其他人将此解释为对每天的值求和并计算当天的平均值。因此,将有 7 行输出,而不仅仅是一条。 )

创建第三个函数来确定哪只猴子在哪一天吃的食物最少。还要输出猴子那天吃的量。创建第四个函数来确定哪只猴子一天吃的食物量最多。输出猴子数量、吃的磅数和工作日。

我是 C++ 的新手并且卡住了,真的不知道如何完成这个。感谢您的帮助,我真的很感激。

4

1 回答 1

1

你一直在做这样的事情:

for (int count = 0; count < size; count++)
{
    for (int col = 0; col < count; col++)
    {
        if (array[count][weekdays] > largest)
            largest = array[count][weekdays];
    }
}

看到您正在使用weekdays索引您的数组。但是那个索引是无效的。它可能有点工作,但总是会返回下一行的第一个元素(然后在最后一行有更明确的未定义行为)。

我很确定你打算使用col而不是weekdays这里。

正如 WhozCraig 在评论中指出的那样,您可能也需要遍历整个weekdays范围。这是一个稍微修复的循环:

for (int count = 0; count < size; count++)
{
    for (int col = 0; col < weekdays; col++)
    {
        if (array[count][col] > largest)
            largest = array[count][col];
    }
}

同样,对于您所做的其他任何地方...

于 2012-11-08T01:13:01.653 回答