一维数组和函数综合编程练习 联系客服

发布时间 : 星期日 文章一维数组和函数综合编程练习更新完毕开始阅读fcd789c59ec3d5bbfd0a74d0

实验七:一维数组和函数综合编程练习

一:实验目的: 1.掌握定义函数的方法;

2.掌握函数实参与形参的对应关系,以及值传递的方式; 3.掌握函数的嵌套调用和递归调用的方法;

4.掌握全局变量和局部变量、动态变量、静态变量的概念和使用方法; 5.掌握一维数组的定义、赋值和输入输出的方法; 6掌握一维数组的有关算法(特别是排序算法); 二:实验内容及要求:

学生成绩统计

从键盘输入一个班(全班最多不超过30人)学生某门课的成绩,当输入成绩为负值时,输入结束,分别实现下列功能:

(1)统计不及格人数并打印不及格学生名单;

(2)统计成绩在全班平均分及平均分之上的学生人数,并打印这些学生的名单; (3)统计各分数段的学生人数及所占的百分比。

【思考题】 在编程实现对数据的统计任务时,需要注意什么问题?

三:程序源代码: #include #define ARR_SIZE 30

int ReadScore(long num[], float score[]); int GetFail(long num[], float score[], int n); float GetAver(float score[], int n);

int GetAboveAver(long num[], float score[], int n); void GetDetail(float score[], int n);

main() {

int n, fail, aboveAver; float score[ARR_SIZE]; long num[ARR_SIZE];

printf(\ n = ReadScore(num, score);

printf(\

fail = GetFail(num, score, n);

printf(\

aboveAver = GetAboveAver(num, score, n);

printf(\

GetDetail(score, n); }

int ReadScore(long num[], float score[]) {

int i=0;

scanf(\ while(score[i]>=0) { i++;

scanf(\ } return i; }

int GetFail(long num[], float score[], int n) {

int i,j=0;

for(i=0;i

return j; }

float GetAver(float score[], int n) {

int i,j;

float sum=0,aver; for(i=0;i

sum=sum+score[i]; }

aver=sum/n; return aver;

}

int GetAboveAver(long num[], float score[], int n) {

int i,j=0;

for(i=0;i

if(GetAver(score,n)<=score[i]) { printf(\ j++; } }

return j; }

void GetDetail(float score[], int n) {

int i,Acount=0,Bcount=0,Ccount=0,Dcount=0,Ecount=0; for(i=0;i

switch((int)score[i]/10) { case 10: case 9: Acount++; break; case 8: Bcount++; break; case 7: Ccount++; break; case 6: Dcount++; break; case 5: case 4: case 3: case 2: case 1: case 0: Ecount++; break; }

}

printf(\ printf(\ printf(\ printf(\ printf(\}

四:运行结果、分析与讨论:

Please enter num and score until score<0: 09610110 98 这次实验主总得来说是有些难度,特别是对数组的定义与赋值还有函09610111 78 数的嵌套调用和递归调用。在程序设计上,依照题目的提示,我对自09610112 96 定义的函数进行程序内容的扩充。但是在给 09610113 75 int ReadScore(long num[], float score[]) 09610114 65 进行编程时出问题。一开始,我是这样编程的: 09610115 69 int ReadScore(long num[], float score[]) 09610116 79 { 09610117 87 int i=0; 09610118 86 do{ 09610119 78 scanf(\09610120 45 i++; 09610121 58 }while(sun[i]>=0); 09610122 -8 return i; Total students:12 } 9610120 45 但是运行程序的时候,计算机只让我输入了7次就自动停止9610121 58 了输入,而且在按照此程序运行时允许的7次输入次数中,Fail students = 2 我数入了-7但是计算机并未执行停止输入的程序,直到我输9610110 98 入7次数据为止,而将该语句快改成: 9610111 78 int ReadScore(long num[], float score[]) 9610112 96 { 9610116 79 int i=0; 9610117 87 scanf(\9610118 86 while(score[i]>=0) 9610119 78 { Above aver students = 7 i++;

90-100: 2 16.67% scanf(\80-89: 2 16.67% } 70-79: 4 33.33% return i; 60-69: 2 16.67% } Failed: 2 16.67% 请按任意键继续. . .

程序正常运行,而且在允许次数内输入-7,输入程序终止。相比较两者,发现两者的while(sun[i]>=0)

判断语句有所不同。对于出现错误的第一个语句快,sum[i]中的i是原来i的增量,

while(sun[i]>=0)语句中sum[i]其实没有任何的值,因而程序运行错误。而后者正确的语句内,while(sun[i]>=0)语句中sum[i]有正确的定义,有准确的值,所以程序正常运行。不过这只是我个人的看法,希望老师能够跟我好好讲下这个问题。与此同时,我还有个疑问,先前我们定义了 float score[ARR_SIZE];

long num[ARR_SIZE];

那么在输入score[i]和num[i]时就不必考虑他们输入的次数,是不是系统就直接默认了i

五:实验心得与体会:

这次实验总得来说还是挺有难度的。不过,通过这次实验后,我对数组和函数的嵌套调用和递归调用有了更深一步的认识。虽然自己在解决这种嵌套式结构还很不熟练,但是都过不断练习与分析可以提高自己的水平。此外,通过这次试验,发现自己对于数组的认识还是很欠缺的,很多方面自己很难理解甚至是混淆。所以,不断研究和努力是最为关键的!