1、西南交通大学 通信工程系高级语言程序设计课后习题编程题答案(部分)第六章:循环结构程序设计.#includeint main()int a=1,b=2,n;double sum=0;for(n=0;nint main()double s=0,a=1,t;int n=1;dot=1/a;s=s+t;a=a+n;n+;while(t=0.000001);printf(“%lf“,s);return 0;.#includeint main()double t,sum=1;double i=1;doi=i*i;t=1/i;sum=sum+t;i+;while(t=0.00001);printf(“%l
2、f“,sum);return 0;.#includevoid main()int n,i,t;for(n=2;nvoid main()double sum=0,x=0,n=0,av=0;printf(“请输入成绩:“);dosum=sum+x;scanf(“%lf“,n+;while(x!=-99);av=sum/(n-1);printf(“平均成绩为%lfn“,av);.#includevoid main()int m,n,i,sum=0;printf(“请输入整数m和n:“);scanf(“%d%d“,for(i=2;ivoid main()int man,woman,child;for(
3、man=0;man=0.#includevoid main()int s;for(s=0;s+)if(s%2=1)printf(“阶梯总共有:%d“,s);第七章:数组.#include#include#includevoid main()int a100,sum=0;int i,n=0;double av;srand(time(0);for(i=0;ivoid main()int a100;int i,j=0;for(i=1;i#include#includevoid main()int a100;int i,x;srand(time(0);for(i=0;i#include#include
4、const int N=10;void main()int aNN;int i,j,sum=0;srand(time(0);for(i=0;i#include#includeconst int N=4;void main()int aNN,bNN,cNN;int i,j;srand(time(0);for(i=0;ivoid main()char str1100;int i,n=0;printf(“请输入一串字符:“);gets(str1);for(i=0;str1i!=0;i+)n+;printf(“%d“,n);./*生成两个字符串str1和str2,比较两个字符串的大小*/#includ
5、econst int N=100;void main()char str1N,str2N;int i;printf(“请输入两个字符串:“);gets(str1);gets(str2);for(i=0;istr2i)printf(“字符串str1大于字符串str2“);break;if(str1i#includevoid main()char str100,s;int i,n;printf(“请输入字符串:“);gets(str);n=strlen(str);printf(“原字符串为:“);puts(str);for(i=0;i#define PI 3.14double V(double r
6、)/定义V函数计算圆球体积double v;v=PI*r*r*r*4/3;/注意若把(4/3)放到计算式的头部则运行结果会发生变化,错误return(v);double V2(double r,double h)/定义V2函数计算圆柱体积double v2;v2=PI*r*r*h;return(v2);double output(double v)/输出函数printf(“%lfn“,v);return 0;void main()double r,h;double v;double v2;scanf_s(“%lf“,scanf_s(“%lf“,v=V(r); /函数调用v2=V2(r,h);printf(“圆球的体积为:“);output(v);printf(“圆柱的体积为:“);output(v2);.#includedouble f(double x)double y;if(x=0return(y);void main()double a,b,c,d,s;scanf(“%lf“,scanf(“%lf“,scanf(“%lf“,scanf(“%lf“,s=(f(a)+f(b)/(f(c)+(f(d);printf(“s=%.2lf“,s);.#includeint prime(int a)int j;int flag=1;