gpt4 book ai didi

c++ - 如何在 C++ 中对二维数组进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:57:11 25 4
gpt4 key购买 nike

<分区>

我有 5 个学生的姓名和他们 3 个科目的成绩,我计算了他们在 3 个类(class)中的平均分。但是然后我需要用他们的数据(姓名、3 门类(class)的成绩和平均分)从平均分最高的到最低的对它们进行排序。谁能告诉我我的代码有什么问题?这是我的代码...

# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
void read(string names[],double grades[][4],int n);
void average(double grades[][4], int n);
void sort(string names[],double grades[][4], int n);
int main()
{
string names[5]; double grades[5][4];
cout<<fixed<<showpoint<<setprecision (2);
cout<<" please enter the names and grades(math,phy,cpp) for 5 students\n";
read(names,grades,5);
average(grades,5);
sort(names,grades,5);
for(int rows=0; rows<5; rows++)
{
cout<<names[rows]<<"\t";
for( int col=0; col<4; col++)
{
cout<<grades[rows][col]<<"\t";
}
cout<<endl;
}







system("pause");
return 0;
}
void read(string names[],double grades[][4],int n)
{
for(int rows=0; rows<n; rows++)
{
cin>>names[rows];
for( int col=0; col<3; col++)
{
cin>>grades[rows][col];
}
}
system("cls");
}
void average(double grades[][4], int n)
{
double sum;
for(int rows=0; rows<n; rows++)
{
sum=0.0;
for(int col=0; col<3;col++)
{
sum=sum+grades[rows][col];
}
grades[rows][3]=sum/3;
}
}

void sort(string names[],double grades[][4], int n)
{
double temp;int end=n;string swap_names;


for(int rows=0; rows<end; rows++)
{
if(grades[rows][3]> grades[rows+1][3])
{
for(int col=0; col<4;col++)
{
temp=grades[rows+1][col];
grades[rows+1][col]=grades[rows][col];
grades[rows][col]=temp;
}
swap_names=names[rows+1];
names[rows+1]=names[rows];
names[rows]=swap_names;
}
}


}

谢谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com