2012-11-08 41 views
-3

可能重複:
Stuck on C++ functions and arraysC++想不通陣列和功能

憋屈這個問題:我不能讓它跑,我被困在得到數組函數返回到main。謝謝你的幫助。

要求利用2維數組來存儲每週七天中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; 
} 
+0

開始添加函數調用到您的main(),在當前的狀態你的程序什麼也不做。可能你需要幫助如何調用一個函數? –

回答

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; 
}