Average performance

Posted by the max on Sat, 02 Nov 2019 01:47:06 +0100

Average performance

Suppose that there are n (n < = 50) students in a class, and each of them takes m (m < = 5) courses. The average score of each student and the average score of each course are calculated, and the number of students whose scores of each subject are greater than or equal to the average is output.

Input

The input data has multiple test instances. The first line of each test instance includes two integers n and m, representing the number of students and courses respectively. Then there are n rows of data, each row including M integers (i.e. test scores).

Output

For each test instance, three rows of data are output, the first row contains n data, which represents the average score of n students, and the result retains two decimal places; the second row contains m data, which represents the average score of m courses, and the result retains two decimal places; the third row is an integer, which represents the number of students whose scores of each subject in the class are greater than or equal to the average score.
Each test instance is followed by a blank line.

Sample Input

2 2
5 10
10 20

Sample Output

7.50 15.00
7.50 15.00
1

C++:

Try to pass all with cin and cout, and finally fail. The following is the failure code
Of course, other systems may succeed.

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double sum1[50],sum2[5],stu[50][5];    //sum1 records the total scores of each student, and sum2 records the total scores of each course.
    int n,m;   //N and m represent the number of students and courses respectively, with n rows of data and m integers
    while(cin>>n>>m)
    {
        for(int i=0;i<n;i++)
            sum1[i] = 0.0;
        for(int i=0;i<m;i++)
            sum2[i] = 0.0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>stu[i][j];
                sum1[i] += stu[i][j];
                sum2[j] += stu[i][j];
            }
        }
        for(int i=0;i<n;i++)     //Output a row to represent the average score of each student
        {  
            if(i != n-1) 
                cout<<fixed<<setprecision(2)<<sum1[i]/m<<" ";  
            else  
                cout<<fixed<<setprecision(2)<<sum1[i]/m<<endl;  
        }  
        for(int j=0;j<m;j++)     //Output a row to represent the average score of each course
        {  
            if(j != m-1)  
                cout<<fixed<<setprecision(2)<<sum2[j]/n<<" ";  
            else  
                cout<<fixed<<setprecision(2)<<sum2[j]/n<<endl;  
        }
        
        int num=0;
        for(int i=0;i<n;i++)
        {
            int k=1;
            for(int j=0;j<m;j++)
            {
                if(stu[i][j] >= sum2[j]/n)
                    k=1;          //Only the final score of each subject is greater than or equal to the average score will make k=1.
                else
                {
                    k=0;
                    break;
                }   
            }
            if(k==1)
                num++;
        } 
        cout<<num<<endl;    //Output the number of students whose scores in all subjects in the class are greater than or equal to the average
    }
}

Correct code:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    double sum1[50],sum2[5],stu[50][5];    //sum1 records the total scores of each student, and sum2 records the total scores of each course.
    int n,m;   //N and m represent the number of students and courses respectively, with n rows of data and m integers
    while(cin>>n>>m)
    {
        for(int i=0;i<n;i++)
            sum1[i] = 0.0;
        for(int i=0;i<m;i++)
            sum2[i] = 0.0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>stu[i][j];
                sum1[i] += stu[i][j];
                sum2[j] += stu[i][j];
            }
        }
        for(int i=0;i<n;i++)     //Output a row to represent the average score of each student
        {  
            if(i != n-1) 
                printf("%.2lf ",sum1[i]/m);  
            else  
                printf("%.2lf\n",sum1[i]/m);  
        }  
        for(int j=0;j<m;j++)     //Output a row to represent the average score of each course
        {  
            if(j != m-1)  
                printf("%.2lf ",sum2[j]/n);   
            else  
                printf("%.2lf\n",sum2[j]/n);  
        }
        
        int num=0;
        for(int i=0;i<n;i++)
        {
            int k=1;
            for(int j=0;j<m;j++)
            {
                if(stu[i][j] >= sum2[j]/n)
                    k=1;          //Only the final score of each subject is greater than or equal to the average score will make k=1.
                else
                {
                    k=0;
                    break;
                }   
            }
            if(k==1)
                num++;
        } 
        cout<<num<<endl;
        cout<<endl;
    }
}