2011-02-28 130 views
0

我正在做一個任務,我遇到了一些問題,所以請幫助,你好人:D 我需要創建一個允許學生成績輸入的函數。在這個功能中,我只需要輸入6-10(通過成績)。然後,我需要製作一個計算學生最低年級的函數。最後,需要制定一個計算每個學生平均成績的函數。 PS:也許我的想法是錯誤的,或者你會選擇不同的或更好的,請說出來,我想學習。提前致謝。二維數組,調用函數問題

這是我的代碼和錯誤:

#include <iostream> 
using namespace std; 

int input (int [][4], int); 
int average (int [][4], int); 
int min (int [][4], int); 
int main() 
{ 
    const int wdth = 5; 
    int matrix[4][4]; 
    input (matrix [4][4], wdth); 
    average (matrix [4][4], wdth); 
    min (matrix [4][4], wdth); 
return 0; 
} 
int input (int matrix[][4], int wdth) 
{ 
    for (int i = 0; i < wdth; i ++) 
    { 
     cout<<"Enter grades for "<<i+1<<" student:"<<endl; 
     for (int j = 0; j < wdth; j ++) 
     { 
      cin>>matrix[i][j]; 
      if ((matrix[i][j] < 6) || (matrix[i][j] > 10)) 
      { 
       cout<<"INVALID INPUT!"<<endl; 
       return 0; 
      } 
      //cout<<setw(5); 
      cout<<matrix[i][j]; 
     } 
     cout<<endl; 
    } 
    return 0; 
} 
int average (int matrix[][4], int wdth) 
{ 
    int sum = 0; 
    int avrg = 0; 
    for (int i = 0; i < wdth; i ++) 
    { 
     cout<<"Calculating average for "<<i+1<<" student: "<<endl; 
     for (int j = 0; j < wdth; j ++) 
     { 
      sum = sum + matrix[i][j]; 
     } 
    } 
    avrg = sum/5; 
    return 0; 
} 
int min (int matrix[][4], int wdth) 
{ 
    int temp = 0; 
    int MIN = 10; 
    for (int i = 0; i < wdth; i ++) 
    { 
     cout<<"Calculating lowest grade for "<<i+1<<" student: "<<endl; 
     for (int j = 0; j < wdth; j ++) 
     { 
      temp = matrix[i][j]; 
      if (temp < MIN) 
      { 
       MIN = temp; 
      } 
     } 
    cout<<MIN; 
    } 
    return 0; 
} 

ERRORS:

cpp(11) : error C2664: 'input' : cannot convert parameter 1 from 'int' to 'int [][4]' 
     Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 

cpp(12) : error C2664: 'average' : cannot convert parameter 1 from 'int' to 'int [][4]' 
     Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 

回答

5

input (matrix [4][4], wdth);這裏matrix [4][4]是的矩陣,即一個int的元件。你需要做input(matrix,wdth);

3
input (matrix [4][4], wdth); 
average (matrix [4][4], wdth); 
min (matrix [4][4], wdth); 

試試吧。 :)

input (matrix, wdth); 
average (matrix, wdth); 
min (matrix, wdth); 

隨着matrix[4][4]您通過第4行的第4個元素。 ;)