2013-04-28 195 views
0

我應該編寫一個程序來讀取並填充包含5個學生ID及其成績的數組,然後使用學生的ID打印平均值,最小值和最大值,一個例子可以是: 學生1:111 56 學生2:222 98 學生3:333 90 學生4:444 68 學生5:555 88 平均:80 等級#1:222 98 排名5 :111 56 我的程序給我弄亂了值(平均值爲328.4,等級#1:444 555,等級#5:555 88)以及調試錯誤(運行時檢查失敗#2 - 圍繞變量'一個'是腐敗者),這裏是計算二維數組的平均值,最小值和最大值

#include <iostream> 
using namespace std; 
void avg(double a[4][1]); 
void minMax(double a[4][1]); 
void main() { 
    double a[4][1]; 
    cout << "Enter 5 students' IDs and marks:\n"; 
    int studentNum = 1; 
    for (int r=0; r<5; r++) { 
     cout << "Student" << studentNum << ": "; 
     studentNum++; 
     for (int c=0; c<2; c++) 
      cin >> a[r][c]; } 
    avg(a); 
    minMax(a); } 
void avg(double a[4][1]){ 
    double sum=0.0; 
    for (int r=0; r<5; r++) { 
     for (int c=1; c<2; c++) // does not include the ID column 
      sum = sum + a[r][c]; } 
    double avg = sum/5; // number of students = 5 
    cout << "Average: " << avg << endl; } 
void minMax (double a[4][1]) { 
    double min = a[0][1]; 
    double max = a[0][1]; 
    int minID = a[0][0]; 
    int maxID = a[0][0]; 
    for (int r=0; r<5; r++) { 
     for (int c=1; c<2; c++) { 
      if (a[r][c] < min){ 
       min = a[r][c]; 
       minID = a[r][0]; } 
      if (a[r][c] > max){ 
       max = a[r][c]; 
       maxID = a[r][0]; } } } 
    cout << "Rank#1: " << maxID << " " << max << endl; 
    cout << "Rank#5: " << minID << " " << min << endl; } 

任何幫助將不勝感激,非常感謝你!

+1

刪除您的代碼,不這樣做。 – HeyYO 2013-04-28 10:41:24

回答

2

的錯誤是:

double a[4][1]; 

它應該是:下面的答案沒用

double a[5][2]; 
相關問題