C ++无法弄清楚数组和函数

时间:2012-11-08 01:23:22

标签: c++ visual-c++

  

可能重复:
  Stuck on C++ functions and arrays

坚持这个问题:我无法让它运行,我不能让数组函数返回到main。谢谢你的帮助。

需要使用二维数组来存储一周七天中每只3只猴子食用的食物磅数。

创建一个函数,以获得每周猴子每周吃掉的磅数。 创建第二个函数以确定通过数组传递以计算所有资金的总量,然后在一天内吃掉平均值。

创建第三个功能,以确定哪只猴子吃的食物量最少,以及哪一天。还输出那天猴子吃的数量。 创建第四个功能,以确定哪一只猴子在一天内吃了最多的食物。输出猴子数,吃掉的磅数和工作日。

#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(){
    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;
}

1 个答案:

答案 0 :(得分:0)

您只是声明了这些功能,而您根本没有调用任何功能。使用下面的示例并相应地更改您的程序。

示例:

int main()
{
    int i_array={0,1,2,3,4,5};
    function(i_array);           //  Call a function
    printf("%d",i_array[0]);    // will print 100 not 0
}

void function(int []  i_array)
{
    i_array[0]=100;
}