资源描述:
《C语言上机练习题.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1.阅读以下程序,说出其功能和运算结果。#includeintmain(){intn;intfactorial(intn);scanf("%d",&n);printf("%d",factorial(n));return0;}intfactorial(intn){inti,fact=1;for(i=1;i<=n;i++)fact=fact*i;returnfact;}若输入5↙2.改正下列程序中的错误,在屏幕上显示“WelcometoYou!”。#includeintmian(){print
2、f("WelcometoYou!")return0;}3.若输入4,程序运行结果为【1】,若输入-4,运行结果为【2】,若输入10,运行结果为【3】。#includemain(){intx,y;scanf("%d",&x);if(x<1){y=x;printf("x=%d,y=x=%d",x,y);}elseif(x<10){y=2*x-1;printf("x=%d,y=2*x-1=%d",x,y);}else{y=3*x-11;printf("x=%d,y=3*x-11=%d",x,y);}1
3、0}4.当输入19,2,21时下列程序运行的结果是( )。#includemain(){inta,b,c,max;printf("pleasescanthreenumbera,b,c:");scanf("%d,%d,%d",&a,&b,&c);max=a;if(max(代表回车),则下面程序的运行结果是 。#includevoidm
4、ain(){intv1=0,v2=0;charch;for(;(ch=getchar())!=‘#’;)switch(ch){case‘a’:case‘h’:default:v1++;case‘o’:v2++;}printf(“%d,%d”,v1,v2);}6.下面程序的运行结果是 。#includevoidmain(){inti,j,k=19;while(i=k-1){k-=3;if(k%5==0){i++;continue;}elseif(k<5)break;i++;}printf(“i=%d,k
5、=%d”,i,k);}7.下列程序的输出为 。#includevoidmain()10{inti,j,k=0,m=0;for(i=0;i<2;i++){for(j=0;j<3;j++)k++;k-=j;}m=i+j;printf("k=%d,m=%d",k,m);}8.下面程序的运行结果是 。#includevoidmain(){intn[3],i,j,k;for(i=0;i<3;i++)n[i]=0;k=2;for(i=0;i6、n[j]=n[i]+1;printf(“%d”,n[1]);}9.阅读程序,写出程序运行结果。#include#define MAX 100voidmain() { int f[MAX],i,j,k=2,m=5;for(i=0;i<=m;i++) f[i]=1; f[k-1]=2;for(i=k;i<=m;i++)for(j=i-k;j<=i-1;j++) f[i]+=f[j]; printf("%d%5d%5d",k,m,f[m]);}10.下面程序可求出矩阵a的两条对角线上的元素之和。请填
7、空。#includevoidmain(){inta[3][3]={1,3,6,7,9,11,14,15,17},sum1=0,sum2=0,i,j;for(i=0;i<3;i++)for(j=0;j<3;j++)if(i==j)sum1=sum1+a[i][j];for(i=0;i<3;i++)for(【1】;【2】;j--)if((i+j)==2)sum2=sum2+a[i][j];printf(“sum1=%d,sum2=%d”,sum1,sum2);}1011.已知以下矩阵:=×下面程序的功能是求出Y
8、1,Y2,Y3,Y4的值。请填空。#includevoidmain(){floata[4][4]={{1,-0.2,0,0},{-0.8,1,-0.2,-0.2},{0,-0.8,1,-0.2},{0,-0.8,-0.8,1}};floatx[