current position:Home>Sort with average score, preferably bubble method

Sort with average score, preferably bubble method

2022-02-03 01:03:30 CSDN Q & A

It is known that 5 Of a student 4 Results of courses , Ask to find out the average score of each student , Then sort the scores of students from high to low ( The students with the highest scores are in the first row , The students with the lowest grades are on the last line .)

Refer to the answer 1:

Refer to the answer 2:

Refer to the answer 3:

Define a two-dimensional array score[5][4], Then enter the student's grade . Calculate the sum of rows , Divide 4 Get an average score

#include <stdio.h>typedef struct _student{    int s[4];    float avg;}student;int main(){    student stu[5],temp;    int i,j;    for(i=0;i<5;i++)    {        stu[i].avg = 0;        for(j=0;j<4;j++)        {            scanf("%d",&stu[i].s[j]);            stu[i].avg += stu[i].s[j];        }        stu[i].avg /= 4;    }    for(i=0;i<4;i++)        for(j=0;j<4-i;j++)        {            if(stu[j].avg < stu[j+1].avg)            {                temp = stu[j];                stu[j] = stu[j+1];                stu[j+1] = temp;            }        }    for(i=0;i<5;i++)    {        for(j=0;j<4;j++)            printf("%d ",stu[i].s[j];        printf("%.1f\n",stu[i].avg);    }    return 0;}

Refer to the answer 4:

Refer to the answer 5:
#include<iostream>using namespace std;void main(){int i,j,sum,t;int a[5][5]={
   78,89,99,76}}; //5*5 Will be enough printf("\n");for (i=0;i<5;i++){ sum=0;for(j=0;j<4;j++) // Before adding 4 Achievements sum+=a[i][j];a[i][4]=sum/4;}// Average Division 4! There is a subscript of 4 Within the element of ;for(int k=0;k<5;k++)//  Added a layer of circulation   See foaming for(i=0;i<5-k;i++)if(a[i][4]<a[i+1][4])// The condition is moved out of the loop for(j=0;j<5;j++){t=a[i][j];a[i][j]=a[i+1][j];a[i+1][j]=t;}for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%8d",a[i][j]);printf("\n");}}

Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended