用c语言解决数组

发布网友 发布时间:2022-04-24 04:21

我来回答

6个回答

热心网友 时间:2023-04-24 15:15

// Students.cpp : 定义控制台应用程序的入口点。
//

#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <time.h>

//每个学生 的成绩
typedef struct _STU_SCORE
{
unsigned int uiScoreOne;
unsigned int uiScoreTwo;
unsigned int uiScoreThree;
}STU_SCORE, *LPSTU_SCORE;

void CalcScores(STU_SCORE* ssScore);//计算每门总分
void GetMaxMinScore(STU_SCORE* ssScore);//每门功课的最高最低分。
void GetAvgScore(STU_SCORE* ssScore);//每门功课平均成绩和每个学生的平均成绩

//先输入学生成绩
void ScanfStuScore(STU_SCORE* ssScore)
{
for (int i = 0; i < 10; ++i)
{
srand((unsigned)time( NULL));
ssScore[i].uiScoreOne = rand()%100 + i * 5;
ssScore[i].uiScoreTwo = rand()%100 + i * 6;
ssScore[i].uiScoreThree = rand()%100 + i * 7;
}
}

//计算每门总分
void CalcScores(STU_SCORE* ssScore)
{
unsigned int uiOne = 0;
unsigned int uiTwo = 0;
unsigned int uiThree = 0;
for (int i = 0; i < 10; ++i)
{
uiOne += ssScore[i].uiScoreOne;
uiTwo += ssScore[i].uiScoreTwo;
uiThree += ssScore[i].uiScoreTwo;
}
printf("第一门成绩总分为%u\n第二门成绩总分为%u\n第三门成绩总分为%u\n",
uiOne, uiTwo, uiThree);
}

void CalcMaxMin(unsigned int uiScore[], unsigned int& uiMin, unsigned int& uiMax)
{
uiMin = uiMax = uiScore[0];
for (int i = 1; i < 10; ++i)
{
if (uiMin > uiScore[i])
{
uiMin = uiScore[i];
}
if (uiMax < uiScore[i])
{
uiMax = uiScore[i];
}
}
}
void CalcMaxMinScore(STU_SCORE* ssScore, unsigned int& uiMin, unsigned int& uiMax, unsigned int uiGrade)
{
switch (uiGrade)
{
case 1:
{
unsigned int uiOneScore[10] = {0};
for (int i = 0; i < 10; ++i)
{
uiOneScore[i] = ssScore[i].uiScoreOne;
}
CalcMaxMin(uiOneScore, uiMin, uiMax);
}
break;
case 2:
{
unsigned int uiTwoScore[10] = {0};
for (int i = 0; i < 10; ++i)
{
uiTwoScore[i] = ssScore[i].uiScoreTwo;
}
CalcMaxMin(uiTwoScore, uiMin, uiMax);
}
break;
case 3:
{
unsigned int uiThreeScore[10] = {0};
for (int i = 0; i < 10; ++i)
{
uiThreeScore[i] = ssScore[i].uiScoreThree;
}
CalcMaxMin(uiThreeScore, uiMin, uiMax);
}
break;
default:
break;
}
}
void GetMaxMinScore(STU_SCORE* ssScore)//每门功课的最高最低分。
{
unsigned int uiOneMin = 0, uiOneMax = 0;
unsigned int uiTwoMin = 0, uiTwoMax = 0;
unsigned int uiThreeMin = 0, uiThreeMax = 0;
//
CalcMaxMinScore(ssScore, uiOneMin, uiOneMax, 1);
CalcMaxMinScore(ssScore, uiTwoMin, uiTwoMax, 2);
CalcMaxMinScore(ssScore, uiThreeMin, uiThreeMax, 3);

printf("第一门功课的最高分为%u, 最低分为%u\n", uiOneMax, uiOneMin);
printf("第二门功课的最高分为%u, 最低分为%u\n", uiTwoMax, uiTwoMin);
printf("第三门功课的最高分为%u, 最低分为%u\n", uiThreeMax, uiThreeMin);

}

//每门功课平均成绩和每个学生的平均成绩
void GetAvgScore(STU_SCORE* ssScore)
{
unsigned int uiAvgOne = 0, uiAvgTwo = 0, uiAvgThree = 0;
for (int i = 0; i < 10; ++i)
{
uiAvgOne += ssScore[i].uiScoreOne;
uiAvgTwo += ssScore[i].uiScoreTwo;
uiAvgThree += ssScore[i].uiScoreThree;
}
printf("第一门功课的平均分为%u,第2门功课的平均分为%u,第3门功课的平均分为%u\n\n",
uiAvgOne /= 10,
uiAvgTwo /= 10,
uiAvgThree /= 10);

for (int i = 0; i < 10; ++i)
{
int iAvg = 0;
iAvg += ssScore[i].uiScoreOne;
iAvg += ssScore[i].uiScoreTwo;
iAvg += ssScore[i].uiScoreThree;
printf("第%d个学生的平均分为%u\n", i, iAvg / 3);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
STU_SCORE ssScore[10] = {0};
ScanfStuScore(ssScore);//输入成绩,这里先自动生成一些

CalcScores(ssScore);//计算每门总分
printf("----------------------------------------\n");

GetMaxMinScore(ssScore);

printf("----------------------------------------\n");
GetAvgScore(ssScore);

system("pause");
return 0;
}

热心网友 时间:2023-04-24 15:16

完整的
#include<stdio.h>

void TotalScoreOfEachSubject(float ob[][4])
{
inti;
floatsum = 0;

printf("The total score is:\n");
for(i = 0; i < 10; i++)
sum += ob[i][0] + ob[i][1] + ob[i][2];
printf("%6.1f\n", sum);
}
void MaxAndMinOfEachSubject(float ob[][4])
{
inti, j;
floatmax, min;

for(i = 0; i < 3; i++)
{
min = max = ob[0][i];

for(j = 1; j < 10; j++)
{
if(max < ob[j][i])
max = ob[j][i];
if(min > ob[j][i])
min = ob[j][i];
}
if(!i)
printf("Chinese max and min:\n");
else if(i == 1)
printf("Math max and min:\n");
else
printf("English max and min:\n");
printf("%6.1f %6.1f\n", max, min);
}

}
void AveOfEachSubject(float ob[][4])
{
inti, j;
floatsum;

for(i = 0; i < 3; i++)
{
sum = 0;
for(j = 0; j < 10; j++)
sum += ob[j][i];
if(!i)
printf("Chinese ave:\n");
else if(i == 1)
printf("Math ave:\n");
else
printf("English ave:\n");
printf("%6.1f\n", sum / 10);
}
}
void AveOfEachStudent(float ob[][4])
{
inti;

for(i = 0; i < 10; i++)
ob[i][3] = (ob[i][0] + ob[i][1] + ob[i][2]) / 3;
printf("Each student ave:\n");
for(i = 0; i < 10; i++)
printf("Num %d: %6.1f\n", i + 1, ob[i][3]);
}
int main()
{
inti;
floatob[10][4];

for(i = 0; i < 10; i++)
{
printf("The %d :\n", i + 1);
printf("Chinese score:");
scanf("%f", &ob[i][0]);
printf("Math score:");
scanf("%f", &ob[i][1]);
printf("English score:");
scanf("%f", &ob[i][2]);
}

TotalScoreOfEachSubject(ob);
MaxAndMinOfEachSubject(ob);
AveOfEachSubject(ob);
AveOfEachStudent(ob);
return 0;
}

热心网友 时间:2023-04-24 15:16

伪代码
double stu[10][3]={....};
int i,j;
求每门总分:
double sum(double array[][])
{
double sum[3]={0,0,0};
for(i=0;i<3;i++)
{
for(j=0;j<10;j++)
{
sum[i]+=array[j][i];
}
}
return sum[3];
}
统计最高最低分:
最高分:
double max(double array[][])
{
double max[3];
for(i=0;i<3;i++)
{
for(j=0;j<9;j++)
{
if(array[i][j]>=array[i][j+1])
max[i]=array[i][j];
else max[i]==array[i][j+1];
}
}
return max[3];
}

现写的,大概就这样,剩下的照着写吧。有点仓促,希望能帮到你。

热心网友 时间:2023-04-24 15:17

用二维数组a[10][3]存储

热心网友 时间:2023-04-24 15:17

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct student

{

char name[10];

float s1;
float s2;
float s3;

}stu[10],temp;

void main()

{

int i,j;

char ch,t[6];

for(i=0;i<10;i++)

{

printf("please input the data of student %d\n",i+1);

gets(stu[i].name);

gets(t);

stu[i].s1=atoi(t);

gets(t);

stu[i].s2=atoi(t);
gets(t);

stu[i].s3=atoi(t);

}

}

热心网友 时间:2023-04-24 15:18

这个太简单了噻~!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com