获取2D数组中的平均数,最小数和最大数

时间:2012-05-04 16:21:01

标签: c++ arrays multidimensional-array

我在获取2D数组元素的最小值,最大值和平均值时遇到问题。

我有一个包含学生和成绩的2D数组。

我用兰特生成成绩。例如,当我输入2,2时,它打印出我

Courses : 01   02   Average   Min   Max
    ID   
    01     8   50     29
    02    74   59     29

,我的平均功能取第一个平均值而不是其他平均值。

这是我的代码;

int A[30][30];

int findAverage(int noOfStudents ,int noOfGrades ){

    float sum,average;




    for (int i = 0 ; i < noOfGrades ; i++) {
        for (int j = 0; j<noOfStudents; j++) {

        sum += A[i][j];

        }
        average = sum / noOfGrades;
       // cout << "   " << format(average);
        sum = 0;
        return format(average);

    }

以及我如何使用它

int main() {

    int noOfCourses , noOfStudents;
    cin >> noOfCourses >> noOfStudents;
    cout << "Courses : " ;

    for (int i = 0; i < noOfCourses; i++) {
        if (i+1 >= 10) {

            cout << i+1 << "   ";
        }else{
            cout <<"0" << i+1 << "   ";

        }
    }

    cout << "Average   Min   Max";

    for(int i=0; i<noOfStudents; i++) {         //This loops on the rows.        

        for(int j=0; j<noOfCourses; j++) {     //This loops on the columns
            A[i][j] = genGrade(); 
        }
    }

    cout << "\n    ID   " << endl;

    for(int i=0; i<noOfStudents; i++) {  //This loops on the rows.

        if (i+1 >= 10) {

            cout <<"    " << i+1 << "   ";
        }else{
            cout <<"    0" << i+1 << "   ";

        }

        //cout <<"    0" << i+1 << "   ";

        for(int j=0; j<noOfCourses; j++) {  //This loops on the columns


            if (A[i][j] >= 10 && A[i][j] <=99) {

                cout <<" " << A[i][j]  << "  ";
            }
            if(A[i][j] < 10) {

                cout <<"  " << A[i][j] << "  ";
            }
            if (A[i][j] == 100) {

                cout << A[i][j] << "  ";
            }
        }
        cout <<"   "<<findAverage(noOfStudents,noOfCourses);
        cout << endl;
    }
}

我做错了什么?另外我怎么能得到每个阵列的最小值,最大值?

3 个答案:

答案 0 :(得分:1)

对于初学者,你是从你的循环中返回的:

for (int i = 0 ; i < noOfGrades ; i++) {
  for (int j = 0; j<noOfStudents; j++) {
    ...
  }
  ...
  return ...;
}

你能看到外循环只执行一次吗?

答案 1 :(得分:1)

我强烈建议使用容器来执行此任务。例如,您可以执行以下操作

typedef std::vector<float> grades;
std::vector<grades> student_grades;

//populate 

for(const grades& gr : student_grades) {
    float min, max, avg;
    std::tie(min, max)=std::minmax(gr.begin(), gr.end());
    avg=std::accumulate(gr.begin(), gr.end(), 0.0) / gr.size());
    std::cout << "Min" << min << " Max: " << max << " Avg: " << avg << std::endl;
}

http://en.cppreference.com/w/cpp/algorithm/minmax

http://en.cppreference.com/w/cpp/algorithm/accumulate

http://en.cppreference.com/w/cpp/utility/tuple/tie

答案 2 :(得分:0)

问题在于,在findAverage函数中,循环中的循环中有一个返回语句。如果您想要一个非常简单的修复,请在findAverage函数中添加另一个参数,以指示计算平均值的行。

int findAverage(int course ,int noOfGrades ){

    float sum,average;


    for (int j = 0; j<noOfStudents; j++) {

        sum += A[course][j];

    }
    average = sum / noOfGrades;
    return format(average);
 }

并像这样称呼它

cout <<"   "<<findAverage(i,noOfCourses);