
正文
c语言上机实验七函数答案 c语言实验六函数答案
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
c语言程序设计苏小红版第七章课后实验答案
不知道你说的是不是这一次实验
2.2.7 实验7:二维数组和函数综合编程练习
成绩排名次
某班期末考试科目为数学(MT)、英语(EN)和物理(PH),有最多不超过30人参加考试。考试后要求:
(1)计算每个学生的总分和平均分;
(2)按总分成绩由高到低排出成绩的名次;
(3)打印出名次表,表格内包括学生编号、各科分数、总分和平均分;
(4)任意输入一个学号,能够查找出该学生在班级中的排名及其考试分数。
【思考题】 请读者思考如下问题。
① 如果增加一个要求:要求按照学生的学号由小到大对学号、成绩等信息进行排序,那么程序如何修改呢?
② 如果要求程序运行后先打印出一个菜单,提示用户选择:成绩录入、成绩排序、成绩查找,在选择某项功能后执行相应的操作,那么程序如何修改呢?
答案
#include stdio.h
#define STU 30
#define COURSE 3
void Input(long num[],int score[][COURSE],int n);
void GetSumAver(int score[][COURSE],int n,int sum[],float aver[]);
void Sort(long num[],int score[][COURSE],int n,int sum[],float aver[]);
void Print(long num[],int score[][COURSE],int n,int sum[],float aver[]);
int Search(long num[], int n, long x);
main()
{
int n, score[STU][COURSE], sum[STU], pos;
long num[STU], x;
float aver[STU];
printf("Please enter the total number of the students(n=30):");
scanf("%d", n); /*输入参加考试的学生人数*/
printf("Enter No. and score as: MT EN PH\n");
Input(num, score, n); /*输入学生成绩*/
GetSumAver(score, n, sum, aver); /*计算总分和平均分*/
printf("Before sort:\n");
Print(num, score, n, sum, aver);
Sort(num, score, n, sum, aver); /*排名次*/
printf("After sort:\n");
Print(num, score, n, sum, aver);
printf("Please enter searching number:");
scanf("%ld", x); /*以长整型格式输入待查找学生的学号*/
pos = Search(num, n, x); /*名次查询*/
if (pos != -1)
{
printf("position:\t NO \t MT \t EN \t PH \t SUM \t AVER\n");
printf("%8d\t%4ld\t%4d\t%4d\t%4d\t%5d\t%5.0f\n",
pos+1,num[pos], score[pos][0],score[pos][1],
score[pos][2], sum[pos],aver[pos]);
}
else
{
printf("Not found!\n");
}
}
/* 函数功能:输入某班学生期末考试三门课程成绩
函数参数:长整型数组num,存放学生学号
整型数组score,存放学生成绩
整型变量n,存放学生人数
函数返回值:无
*/
void Input(long num[], int score[][COURSE], int n)
{
int i, j;
for (i=0; in; i++)
{
scanf("%ld", num[i]);
for (j=0; jCOURSE; j++)
{
scanf("%d", score[i][j]);
}
}
}
/* 函数功能:计算每个学生的总分和平均分
函数参数: 整型数组score,存放学生成绩
整型变量n,存放学生人数
整型数组sum,计算得到的每个学生的总分
实型数组aver,计算得到的每个学生的平均分
函数返回值:无
*/
void GetSumAver(int score[][COURSE], int n, int sum[], float aver[])
{
int i, j;
for (i=0; in; i++)
{
sum[i] = 0;
for (j=0; jCOURSE; j++)
{
sum[i] = sum[i] + score[i][j];
}
aver[i] = (float)sum[i] / COURSE;
}
}
/* 函数功能:按总分成绩由高到低排出成绩的名次
函数参数:长整型数组num,存放学生学号
整型数组score,存放学生成绩
整型变量n,存放学生人数
整型数组sum,存放每个学生的总分
实型数组aver,存放每个学生的平均分
函数返回值:无
*/
void Sort(long num[],int score[][COURSE], int n, int sum[], float aver[])
{
int i, j, k, m;
int temp1;
long temp2;
float temp3;
for (i=0; in-1; i++)
{
k = i;
for (j=i+1; jn; j++)
{
if (sum[j] sum[k]) k = j;
}
if (k != i)
{
temp1 = sum[k]; sum[k] = sum[i]; sum[i] = temp1;
temp2 = num[k]; num[k] = num[i]; num[i] = temp2;
temp3 = aver[k]; aver[k] = aver[i]; aver[i] = temp3;
for (m=0; mCOURSE; m++)
{
temp1 = score[k][m];
score[k][m] = score[i][m];
score[i][m] = temp1;
}
}
}
}
/* 函数功能: 打印名次表,表格内包括学生编号、各科分数、总分和平均分
函数参数: 长整型数组num,存放学生学号
整型数组score,存放学生成绩
整型变量n,存放学生人数
整型数组sum,存放每个学生的总分
实型数组aver,存放每个学生的平均分
函数返回值:无
*/
void Print(long num[], int score[][COURSE], int n,
int sum[], float aver[])
{
int i, j;
printf(" NO \t| MT \t EN \t PH \t SUM \t AVER\n");
printf("----------------------------------------------------\n");
for (i=0; in; i++)
{
printf("%ld\t| ", num[i]);
for (j=0; jCOURSE; j++)
{
printf("%4d\t", score[i][j]);
}
printf("%5d\t%5.0f\n", sum[i], aver[i]);
}
}
/* 函数功能:在学号数组中顺序查找学生的学号
函数参数:长整型数组num,存放学生学号
整型变量n,存放学生人数
长整型变量x,存放待查找学生的学号
函数返回值:找到时,返回学生学号在学号数组中的下标位置,否则返回值-1
*/
int Search(long num[], int n, long x)
{
int i;
for (i=0; in; i++)
{
if (num[i] == x) return(i);
}
return (-1);
}
相关问答
Q1: 大学程序设计C语言 实验七的题目
第一题:
有3个错。
1.strupr(name[i]) 改为:strcpy(name[i],strupr(name[i]));
2.if(name[i] name[j])改为:if(strcmp(name[i],name[j])0)
3.strcpy(name[i],str );改为:strcpy(str,name[i]);
第二题:
(1)s[i] != '\0'
(2){ j ++;}else
memcpy(s[i],s[i]+1,strlen(s)-i-1)
第三题:
s[i] = '9' s[i] = '0'
Q2: 急求!!C语言课后习题答案!2
以前上课的时候不想做,现在很想补下,很后悔没好好学习呢~希望楼主好好努力
第4道1):
#includestdio.h
void main(void)
{
int i,j,a[80],max,max_i;
printf("please input 10 num!\n");
for(i=0;i10;i++)
scanf("%d",a[i]);
printf("the 10 num is:\n");
for(i=0;i10;i++)
printf("%d\t",a[i]);
for(max=a[0],i=0;i10;i++)
{
if(maxa[i])
{
max=a[i];
max_i=i;
}
}
printf("the max num is:a[%d]=%d\n",max_i+1,max);
getch();
}
2)道:
#includestdio.h
void main(void)
{
int a[3][2],i,j,sum[3];
for(i=0;i3;i++)
{
printf("please input the score of the student %d\n",i+1);
scanf("%d%d",a[i][0],a[i][1]);
sum[i]=a[i][0]+a[i][1];
}
for(i=0;i3;i++)
{
printf("the score of the student %d is:%d\t%d\n",i+1,a[i][0],a[i][1]);
printf("the sum score of the student %d is:%d\n",i+1,sum[i]);
}
getch();
}
3)道:
#includestdio.h
#includestring.h
main()
{
int i;
char string1[80],string2[80];
printf("please input the string 1\n");
scanf("%s",string1);
printf("please input the string 2\n");
scanf("%s",string2);
if(strcmp(string1,string2)==0)
printf("the 2 string is the same\n");
else
printf("the 2 string is different\n");
getch();
}
第5道:
#includestdio.h
main()
{
float c,f;
printf("please input the F\n");
scanf("%f",f);
c=(f-32)*5/9;
printf("the convert result is:%.2f\n",c);
getch();
}
小问:
float convert(float a)
{
float b;
b=(a-32)*5/9;
return b;
}
main()
{
float c,f;
printf("please input the F\n");
scanf("%f",f);
c=convert(f);
printf("the convert result is %.2f\n",c);
getch();
}
第6道:
#includestdio.h
main()
{
int x,y,*p1,*p2;
printf("please input the 2 nums;\n");
scanf("%d%d",x,y);
swap1(x,y);
printf("the swap1 result is:%d\t%d\n",x,y);
p1=x;
p2=y;
swap2(p1,p2);
printf("the swap2 result is:%d\t%d\n",*p1,*p2);
getch();
}
swap1(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
swap2(int *p1,int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
6.2)
#include stdio.h
void main()
{
char s[80];/*定义一个数组*/
char c;
printf("which style you want to:\n");/*是\不是|*/
printf("capital ( c ) or uncapital(a):");
c=getchar();
if(c=='c')/*是==不是=*/
strcpy(s,"COMPUTER");
else
strcpy(s,"computer");
puts(s);
getch();
}
第七道
struct student
{
char num[8];
char name[80];
float score;
}stu[4];
main()
{
int i,max,max_i;
for(i=0;i4;i++)
{
printf("input the num of student %d\n",i+1);
scanf("%s",stu[i].num);
printf("input the name of student %d\n",i+1);
scanf("%s",stu[i].name);
printf("input the score of student %d\n",i+1);
scanf("%f",stu[i].score);
}
for(max=stu[0].score,i=0;i4;i++)
{
if(maxstu[i].score)
{
max=stu[i].score;
max_i=i;
}
}
printf("the highest score student is student%d\n",max_i+1);
printf("%s\t%s\t%.2f\n",stu[max_i].num,stu[max_i].name,stu[max_i].score);
getch();
}
第八道:
Java的行不,C++的不记得了。
public class Circle
{
private static double PI=3.14
private double radius;
public Circle(double radius)
{
this.radius=radius;
}
public double Area()
{
return PI*radius*radius;
}
public double Perimeter()
{
return 2*PI*radius;
}
}
关于c语言上机实验七函数答案和c语言实验六函数答案的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






