实习主题 实验题目 实验时间 学生姓名 实验地点 C语言集中上机 C程序设计 2016 学年 2 学期 2 周(2016年3月8日—日) 胡渝苹 2506 学号 设备号 2015211006 A55 班级 指导教师 03011503 周玉敏 Test1
1. A.题目
编写一个函数pi,其功能是根据以下近似公式求π值:(π*π)/6=1+1/(2*2)+1/(3*3)+?+1/(n*n)。并写一个主函数,调用该函数计算π的值,n在主函数中从键盘输入。要求程序存入test11.c。
B.设计思路
先对pi函数进行声明,再找出pi的表示方法,最后对函数进行定义
C.调试情况
将所有的单精度装换为双精度
D.运行结果
E.附录
#include
double pi(int n); int n; double y;
scanf(\ y=pi(n);
printf(\}
double pi(int n) {
double m=1; inti;
for(i=2;i<=n;i++) { m=m+1.0/(i*i); }
m=sqrt(m*6); return(m); }
2. A.题目
下列程序是将一个数组中的值按逆序重新存放并输出。请输入并完善程序。要求程序存入test12.c。
#include
{ intarr[10],i,j,temp; printf(“Input value of array:\\n”); for(i=0;i<10;i++) scanf(“%d”, (1) ); for(i=0,j=9;i<=j;i++,j--) temp=arr[i],arr[i]= (2) ,arr[j]=temp; for(i=0;i<10;i++) printf(“M”, (3) ); }
B.设计思路
第一个数和最后一个数对调
C.调试情况
输入数组也要用&
D.运行结果
E.附录
#include
{ intarr[10],i,j,temp; printf(\ for(i=0;i<10;i++) scanf(\ for(i=0,j=9;i<=j;i++,j--) temp=arr[i],arr[i]=arr[j],arr[j]=temp; for(i=0;i<10;i++) printf(\}
3 A. 题目
B. 在下列程序中,函数my_cmp()的功能是比较字符串s和t的大小,当s等于t时返回0,
否则返回s和t的第一个不同字符ASCII码差值,当s #include * intmy_cmp(char s,char t) { while(*s==*t) * { if(*s) return(0); ++s,++t; } * my_cmp=*s-*t; } void main() { char *s1,*s2; * s1=getchar(); s2=getchar(); if(my_cmp(s1,s2)==0) printf(“EQ”); else if(my_cmp(s1,s2)>0) printf(“s1>s2”); else printf(“s2>s1”); } B.设计思路 用指针指向数组 C.调试情况 正确 D.运行结果 E.附录 #include charmy_cmp(char *s,char *t) { while(*s==*t) { if(*s=='\\0') return(0); ++s,++t; } return(*s-*t); } void main() { char str1[100],str2[100]; char *s1=str1,*s2=str2; *s1=getchar(); *s2=getchar();; if(my_cmp(s1,s2)==0) printf(\ else if(my_cmp(s1,s2)>0) printf(\ else printf(\} 4 A.题目 编写一个程序用来统计学生成绩。其功能包括输入学生姓名和成绩,按成绩从高到低排列打印输出,对前80%的学生定为合格(pass),后20%的学生定为不合格(fail)。要求程序存入test14.c。 B.设计思路 #include