收藏 分销(赏)

c语言程序填空题.docx

上传人:Fis****915 文档编号:551513 上传时间:2023-12-06 格式:DOCX 页数:9 大小:18.34KB 下载积分:6 金币
下载 相关 举报
c语言程序填空题.docx_第1页
第1页 / 共9页
c语言程序填空题.docx_第2页
第2页 / 共9页


点击查看更多>>
资源描述
程序填空题 1. 下述程序是一个选择排序程序,请填完整空缺部分。 Void select _sort (int p [ ], int n ) { int i, j, k, temp; for ( i =0 ;i<n-1; i ++ ) { k = i; for ( j=i+1 ; j<n ; j++) if ( p[j]<p[k]) k=j; if (k!=i) { temp= p[k]; p[k]=p[i];p[i]=temp;} } } main() { int i,a[20]; for (i =0; i<20; i++) scanf (“%d”,&a[i]); select_sort(a,20); for ( i = 0;i<20; i++) printf (“%d\t”,a[i];printf (“\n”); } 2. 下面程序的功能是:计算1到10之间的奇数之和及偶数之和,请填空。 # include <stdio.h> main() { int a, b, c , i; a = c = 0 ; for(i=0;i<=10;i+=2) { a + = i ; b=i+1 ; c += b ; } printf ( “ 偶数之和= %d \n “, a ) ; printf ( “ 奇数之和= %d \n “, c – 11 ); } 3. 下面程序的功能是:输出100 以内 能被 3 整除且个位数为 6 的所有整数,请填空: main() { int i , j ; for ( i = 0 ;i<10; i + + ) { j = i * 10 + 6 ; if ( j%3!=0 ) continue ; printf ( “ %d” , j ) ; } } 4.程序读入20个整数,统计非负数的个数,并计算非负数之和。 #include “stdio.h” main() { int I,a[20],s,count; ____s=count=0____; for(I=0;I<20;I++) scanf(“%d”,_&a[i]__); for(I=0;I<20;I++) { if(a[I]<0) continue; s+=a[I]; count++ } printf(“s=%d\t count=%d\n”,s,count); } 5.下面的程序用以分别求出数组两条对角线上的各元素之和。 #include “stdio.h” main() { int a[3][3]={1,2,3,4,5,6,7,8,9}; int sum1,sum2,k,j; sum1=sum2=0; for(k=0;k<3;k++) sum1+=a[i][i]; for(k=0;k<3;k++) for(j=2;j>=0;j--) if(_(i+j)==2________) sum2+=a[k][j]; printf(“sum1=%d\tsum2=%d\n”,sum1,sum2); } 6.已知整型数组w中的元素已经由小到大排序,函数fun用以将整数x插入到数组w中,要求插入后的数组元素依然有序。其中n所指的存储单元中存放着数组元素的个数。 void fun(int *w, int x, int *n) { int k,p=0; w[*n]=x; while(x>w[p]) __p++____; for(k=*n;k>p;k--) w[k]=__w[k-1]__; ___w[p]=x____________; ++*n; } 7.函数strlen用于确定一个给定字符串str的长度(不包括‘\0’)。 strlen(char *str) { int num=0; while(*str++!=’\0’) ++num; return(_num___); } 8.已知第一个学生的年龄为10岁,,第二个学生年比第一个学生的年龄大2岁,……以此类推,以下程序用于计算学生年龄,请补充完整。 #include “stdio.h” age(int n) { int c; if(n= =1) c=10; else c=__age(n-1)+2____; return( c ) ; } main() { int n=5; printf(“age:%d\n”,age(n)); } 9.以下是一个求字符串长度的函数的递归算法,请补充完整(不包括‘\0’) int strlen(char * str) { if(*str==’\0’) return(0); else return(__1+strlen(str+1)___); 10. 以下程序将用户从键盘上随机输入的30个学生的学号、姓名、数学成绩、计算机成绩及总分写入数据文件score.txt中,假设30个学生的学号从1~30连续。输入时不必按学号顺序进行,程序自动按学号顺序将输入的数据写入文件。请在程序中的空白处填入一条语句或一个表达式。 # include <stdio.h> FILE *fp; Main( ) { struct st { int number; char name[20]; float math; float computer; float total; } student; int i,j; if( (fp=fopen(“score.txt”,”w”))==null ) { printf(“file open error\n”); exit(1); } for(i=0;i<30;i++) { scanf(“%d,%20s,%f,%f”,&student.number, student.name ,&student.math,&puter); student.total=student.math+puter; j=student.number-1; fseek(fp,j* sizeof(student),0); if(fwrite(&student, sizeof(student), 1, fp)!=1) printf(“write file error\n”); } fclose(fp); } 11.下面的程序用来统计文件中字符的个数,请填空。 #include <stdio.h> main() { FILE *fp; long num=0; if(( fp=fopen("fname.dat","r"))==NULL) { printf( "Can"t open file! \n"); exit(0);} while(!feof(fp)) { fgetc(fp); num++;} printf("num=%d\n", num); fclose(fp); } 12. 【程序说明】将从键盘输入的三个整数,按从小到大的顺序输出。 【程序】 #include <stdio.h> int main() { int a,b,c,*pa=&a,*pb=&b,*pc=&c,*p; scanf("%d%d%d", pa,pb,pc); if(*pa>*pb) {p=pa; pa=pb; pb=p;} if(*pa>*pc) {p=pa; pa=pc; pc=p;} if(*pb>*pc) {p=pb; pb=pc; pc=p;} printf("%d <= %d <= %d\n", *pa,*pb,*pc); return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服