C语言考试改错填空编程题题库·
- 格式:doc
- 大小:111.00 KB
- 文档页数:14
c语言改错题及答案【篇一: c 语言二级上机(t 填空 ,改错 ,编程 )题库及答案】100套题目(每套题目包含 3 道题:一道程序填空题、一道程序修改题、一道程序设计题),真实考试的时候,考生输入准考证后计算机随机为你抽取一套考试,每个考生考试时只需考一套题目(包含三道题)二级 c 语言上机试题汇编※※※※※※※※※※※※※※※※※※※※※※※第01套:给定程序中,函数 fun 的功能是:将形参 n 所指变量中,各位上为偶数的数去除,剩余的数按原来从高位到低位的顺序组成一个新的数,并通过形参指针 n 传回所指变量。
例如,输入一个数:27638496 ,新的数:为739 。
请在程序的下划线处填入正确的内容并把下划线删除 , 使程序得出正确的结果。
注意:源程序存放在考生文件夹下的blank1.c中。
不得增行或删行,也不得更改程序的结构!给定源程序:#include stdio.hvoid fun(unsigned long *n){ unsigned long x=0, i; int t;i=1;while(*n)/**********found**********/{ t=*n % __1__;/**********found**********/if(t%2!= __2__){ x=x+t*i; i=i*10; }*n =*n /10;}/**********found**********/*n=__3__;}main(){ unsigned long n=-1;while(n99999999||n0){ printf(please input(0n100000000): ); scanf(%ld,n); }fun(n);printf(\nthe result is: %ld\n,n);}【参考答案】(1)10 (2) 0 (3) x解题思路:第一处: t 是通过取模的方式来得到*n 的个位数字,所以应填:10 。
1.下列给定程序中,函数fun的功能是:先将字符串s中的字符按正序存放到字符串t中,然后把s中的字符按逆序连接到字符串t的后面。
例如,当s中的字符串为"ABCDE"时,则t中的字符串应为"ABCDEEDCBA"。
#include <stdio.h>#include <string.h>void fun (char *s, char *t){ int i, sl;sl = strlen(s);/************found************/for( i=0; i<sl; i++)t[i] = s[i];for (i=0; i<sl; i++)t[sl+i] = s[sl-i-1];/************found************/t[sl*2] = '\0';}2.下列给定程序中,函数fun的功能是:读入一个字符串(长度<20),将该字符串中的所有字符按ASCII码值升序排序后输出。
例如,若输入"edcba",则应输出"abcde"。
#include <string.h>#include <stdio.h>void fun(char t[]){char c;int i,j;/*************found**************/for(i=strlen(t)-1;i;i--)for(j=0;j<i;j++)/*************found**************/if(t[j]>t[j+1]){c= t[j];t[j]=t[j+1];t[j+1]=c;}}3.下列给定程序中函数fun的功能是:从N个字符串中找出最长的串,并将其地址作为函数值返回。
各字符串在主函数中输入,并放入一个字符串数组中。
1 下列给定程序中,函数fun 的功能是:计算S=f(-n)+f(-n+1)+…+f(0)+f(1)+f(2)+…f(n)的值。
例如,当n 为5时,函数值应为10.407143。
f(x)函数定义如下:f(x)=(1)(2)0002(1)(2)0x x x x x x x x ⎧+/->⎪==⎨⎪-/-<⎩或请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main 函数,不得增行或删行,也不得更改程序的结构。
试题程序:#include <stdlib.h> #include <conio.h> #include <stdio.h> #include <math.h>/*************found**************/ f(double x)2 下列给定程序中,fun 函数的功能是:根据形参m,计算下列公式的值:t=1+1/2+1/3+1/4+…+1/m例如,若输入5,则应输出2.283333。
3 下列给定程序中,函数fun 的功能是:分别统计字符串中大写字母和小写字母的个数。
例如,给字符串s 输入:AaaaBBb123CCccccd,则应输出结果:upper=5,lower=9。
4 下列给定程序中函数fun 的功能是:计算正整数num 各位上的数字之积。
例如,若输入252,则输出应该是20。
若输入202,则输出应该是0。
5 下列给定程序中函数fun 的功能是:计算n!。
例如,给n 输入5,则输出120.000000。
6 下列给定的程序中,函数fun 的功能是:计算并输出k 以内最大的10个能被13或17整除的自然数之和。
k 的值由主函数传入,若k 的值为500,则函数的值为4622。
7 下列给定程序中,函数fun()的功能是:根据整型形参m 的值,计算如下公式的值:t=1-1/(22)1/(33)...1/()m m ⨯-⨯--⨯8 在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元素中。
第一套1. 程序填空程序通过定义学生结构体数组,存储了若干个学生的学号、姓名和三门课的成绩。
函数fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大排序)。
第一处struct student t;第二处for(i=0;i<n-1;i++)第三处if(strcmp(a[i]. name,a[j]. name)>0)2 程序修改/**fou nd**/q=p+i;/**fou nd**/while(q>p)3程序设计int fun(STREC *a,STREC *b,intl,i nt h){int i,j=0;for(i=0;i<N;i++)if((a[i].s>=1 &&a[i].s<=h) b[j++]=a[i]; return j;}第二套1. 程序填空给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序连接。
函数fun的功能是:删除链表中数据域值相同的结点,使之只保留一个。
第一处free(q);第二处q=p->next; 第三处q=q_>next;2. 程序修改/**fou nd**/p=j;/**fou nd**/p=i;3. 程序设计void fun (i nt m,i nt *a,i nt *n){int i;*n=0;for(i=7;i<=m;i++)if((i%7==0)||(i%11==0)) a[(* n)++]=i;}第三套1. 程序填空函数fun的功能是:逆置数组元素是的值。
例如:若a所指数组中的数据依次为:1、2、3、4、5、6、7、8、9,则逆置后依次为:9、8、7、6、5、4、3、2、1。
形参n给出数组中数组中数据的个数。
第一处for(i=0;i<n/2;i++) 第二处a[i]=a[n-1-i];第三处a[n-i-1]=t;2程序修改/**fou nd**/ n=*p_ ‘0';/**fou nd**/n=n*8+*p- ‘O';3.程序设计STREC fun (STREC*a,char *b){STREC c;Int i;c.num[0]= ' \0 ';c.s=-1; for(i=0;i<N;i++) if(strcmp(a[i]. num,b)==0) {strcpy(c. nu m,a[i]. nu m);c.s=a[i].s;break;} return c;}第四套1. 程序填空Z |给定程序中,函数fun的功能是:在带有头结点的单向链表中,查找数据域中值为ch的结点。
c语言题库改错题及详解答案C语言是一种广泛使用的计算机编程语言,它以其强大的功能和灵活性而闻名。
在学习和掌握C语言的过程中,练习题库中的改错题是提高编程技能的有效方式。
以下是一些常见的C语言改错题及其详解答案。
1. 题目:编写一个程序,计算并输出1到10的累加和。
错误代码:```cint main() {int sum = 0;for(int i = 1; i <= 10; i++) {sum = sum + i;}print("Sum is %d", sum);return 0;}```错误点: `print` 函数应为 `printf`。
正确代码:```c#include <stdio.h>int main() {int sum = 0;for(int i = 1; i <= 10; i++) {sum += i; // 可以简化为 sum += i;}printf("Sum is %d\n", sum);return 0;}```2. 题目:编写一个程序,判断输入的年份是否为闰年。
错误代码:```cint main() {int year;printf("Enter a year: ");scanf("%d", &year);if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {printf("%d is a leap year.\n", year);} else {printf("%d is not a leap year.\n", year);}return 0;}```错误点:逻辑判断的括号使用不正确。
正确代码:```c#include <stdio.h>int main() {int year;printf("Enter a year: ");scanf("%d", &year);if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {printf("%d is a leap year.\n", year);} else {printf("%d is not a leap year.\n", year);}return 0;}```3. 题目:编写一个程序,实现字符串的反转。
第1题【程序改错】功能:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。
例如:当s中的字符串为:“ABCDE”时,则t中的字符串应为:“EDCBAABCDE”。
------------------------------------------------------*/#include <conio.h>#include <stdio.h>#include <string.h>void fun (char *s, char *t){/**********FOUND**********/int i;sl = strlen(s);for (i=0; i<sl; i++)/**********FOUND**********/t[i] = s[sl-i];for (i=0; i<sl; i++)t[sl+i] = s[i];/**********FOUND**********/t[2*sl] = "0";}main(){char s[100], t[100];printf("\nPlease enter string s:"); scanf("%s", s);fun(s, t);printf("The result is: %s\n", t);}答案:1). int i,sl;2). t[i] = s[sl-i-1];3). t[2*sl] = '\0'; 或 t[2*sl] = 0;第2题【程序改错】功能:求出以下分数序列的前n项之和。
和值通过函数值返回main 函数。
2/1+3/2+5/3+8/5+13/8+21/13 ……例如:若n = 5,则应输出:8.391667。
------------------------------------------------------*/#include <conio.h>#include <stdio.h>/**********FOUND**********/fun ( int n ){int a, b, c, k; double s;s = 0.0; a = 2; b = 1;for ( k = 1; k <= n; k++ ){/**********FOUND**********/s = (double)a / b;c = a;a = a + b;b = c;}/**********FOUND**********/return c;}main( ){int n = 5;printf( "\nThe value of function is: %lf\n", fun ( n ) );}答案:1). double fun(int n)2). s = s + (double)a / b; 或 s += (double)a / b; 或 s += a /(double)b; 或s=s+a/(double)b;3). return s;第3题【程序改错】功能:读入一个整数m( 5≤m≤20 ),函数getarr调用函数rnd获得m个随机整数,函数sortpb将这m个随机整数从小到大排序。
c语言改错题题库及详解答案C语言是一种广泛使用的计算机编程语言,它以其高效性、灵活性和强大的功能而受到程序员的青睐。
然而,即使是经验丰富的程序员也难免会在编写C语言程序时犯一些错误。
下面是一个C语言改错题题库及其详解答案,帮助学习者识别并改正常见的编程错误。
题目1:错误的变量初始化```cint main() {int a;printf("%d", a); // 错误:变量a未初始化return 0;}```详解答案:在C语言中,如果一个变量在使用前没有被显式地初始化,它的值是不确定的。
为了修复这个错误,我们应该在声明变量时对其进行初始化。
```cint main() {int a = 0; // 正确的初始化printf("%d", a);return 0;}```题目2:错误的数组索引```cint main() {int arr[5] = {1, 2, 3, 4, 5};printf("%d", arr[5]); // 错误:数组索引越界return 0;}```详解答案:数组索引是从0开始的,所以对于一个有5个元素的数组,有效的索引范围是0到4。
访问数组的第6个元素会导致越界错误。
```cint main() {int arr[5] = {1, 2, 3, 4, 5};printf("%d", arr[4]); // 正确的索引return 0;}```题目3:错误的循环使用```cint main() {int i;for (i = 0; i <= 10; i++) {printf("%d ", i);} // 错误:循环条件错误return 0;}```详解答案:循环条件应该是`i < 10`,以确保循环不会无限执行。
```cint main() {int i;for (i = 0; i < 10; i++) {printf("%d ", i);}return 0;}```题目4:错误的函数调用```cint main() {int result = add(5, 3); // 错误:add函数未定义printf("%d", result);return 0;}```详解答案:在调用一个函数之前,需要确保该函数已经被定义。
改错题1、在考生文件夹下,给定程序MODI.C的功能是:从低位开始取出长整型变量s中奇数位上的数,依次构成一个新数放在t中。
例如,当s中的数为:7654321时,t中的数为:7531。
请修改并运行该程序,然后将源程序文件MODI.C上传。
#include <conio.h>#include <stdio.h>main( ){ long s, t, sl=10;clrscr();printf("\nPlease enter s:");scanf("%ld", &s);/************found************/t = s / 10; s%10while ( s > 0){ s = s/100;t = s%10 * sl + t;/************found************/sl = sl*100 ;}printf("The result is: %ld\n", t);}2、在考生文件夹下,给定程序MODI.C的功能是:求一维数组a中的值为偶数的元素之和。
例如,当一维数组a中的元素为:10,4,2,7,3,12,5,34,5,9 ,程序的输出应为:The result is: 62#include <conio.h>#include <stdio.h>main(){ int a[10]={10,4,2,7,3,12,5,34,5,9},i,s;clrscr();s = 0;for ( i=0; i<10; i++)/************found************/if (i % 2 == 0)s = s + a[i];/************found************/print("The result is: %d\n", s);}3、在考生文件夹下,给定程序MODI.C的功能是:求一维数组a中值为偶数的元素之和。
例如,当一维数组a中的元素为:10,4,2,7,3,12,5,34,5,9 ,程序的输出应为:The result is: 62。
#include <conio.h>#include <stdio.h>sum ( int arr[ ],int n ){ int i,s;clrscr();s = 0;for ( i=0; i<n; i++)if (arr[i] % 2 == 0)/************found************/s = s + i;return (s);}main(){ int a[10]={10,4,2,7,3,12,5,34,5,9},i,s;/************found************/s = sum( a ,2 );printf("The result is: %d\n", s);}4、在考生文件夹下,给定程序MODI.C的功能是:求二维数组a中的最大值。
例如,当二维数组a中的元素为:4 4 347 3 125 6 5程序的输出应为:The max is: 34 。
#include <conio.h>#include <stdio.h>main(){ int a[3][3]={4,4,34,7,3,12,5,6,5},i,j,max;clrscr();max = a[0][0];for ( i=0; i<3; i++)for ( j=0; j<3; j++)/************found************/if (max > a[i][j]){/************found************/max == a[i][j];}printf("The max is: %d\n", max);}5、在考生文件夹下,给定程序MODI.C的功能是:求一维数组a中的最大元素及其下标。
例如,当一维数组a中的元素为:1,4,2,7,3,12,5,34,5,9,程序的输出应为:The max is: 34,pos is: 7 。
#include <conio.h>#include <stdio.h>main(){ int a[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;clrscr();max = a[0];pos = 0;for ( i=1; i<10; i++)/************found************/if (max > a[i]){max = a[i];/************found************/i = pos;}printf("The max is: %d ,pos is: %d\n", max , pos); }6、在考生文件夹下,给定程序MODI.C的功能是:输出1~1000之间的所有完数,并输出每个完数的所有因子。
所谓完数,就是指一个正整数,它的各因数之和等于其自身。
例如,28=1+2+4+7+14,28是一个完数。
#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <math.h>main(){ int i,j, t;clrscr();for(i=1;i<=1000;i++){ t=0;for(j=1;j<i;j++)if(i%j==0) t+=j;/************found************/if(t=i){ printf("\n%d is a full number\n",i);printf("factors of %d are:\t",i);/************found************/for(j=1,j<i,j++)if(i%j==0) printf("%d,",j);}}printf("\n");}7、在考生文件夹下,给定程序MODI.C的功能是:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。
例如:当s中的字符串为:"ABCDE"时,则t中的字符串应为:"EDCBAABCDE"。
#include <conio.h>#include <stdio.h>#include <string.h>main(){ char s[80], t[80];/************found************/int i;clrscr();printf("\nPlease enter string s:"); scanf("%s", s); sl = strlen(s);for (i=0; i<sl; i++)/************found************/t[i] = s[sl-i];for (i=0; i<sl; i++)t[sl+i] = s[i];t[2*sl] =’\0’;printf("The result is: %s\n", t);}8、在考生文件夹下,给定程序MODI.C的功能是:先将在字符串s中的字符按正序存放到t串中,然后把s中的字符按逆序连接到t串的后面。
例如:当s中的字符串为:"ABCDE"时,则t中的字符串应为:"ABCDEEDCBA"。
#include <conio.h>#include <stdio.h>#include <string.h>main(){ char s[80],t[80];int i, sl;clrscr();printf("\nPlease enter string s:");scanf("%s", s);sl = strlen(s);/************found************/for( i=0; i<=sl; i+=2)t[i] = s[i];for (i=0; i<sl; i++)t[sl+i] = s[sl-i-1];/************found************/t[s l] =’\0’;printf("The result is: %s\n", t);}9、在考生文件夹下,给定程序MODI.C的功能是:求两实数平方根之和,并输出此和。
例如:输入12和20,输出结果是:c= 7.936238。
#include <conio.h>#include <math.h>main( ){/************found************/int a, b,c;clrscr();printf ( "Enter a & b : ");scanf ("%lf%lf", &a, &b );/************found************/c= sqr(a)+sqr(b) ;printf ("c= %f \n", c);}10、在考生文件夹下,给定程序MODI.C的功能是:输出200~300之间的所有素数,要求每行输出8个素数。
#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <math.h>main(){ int m,j,n=0,k;clrscr();for(m=200;m<=300;m++){ k=sqrt(m);for(j=2;j<=k;j++)/************found************/if(m%j==0) continue;if(j>k){ if(n%8==0) printf("\n");/************found************/printf("%6d",j);n++;}11、输出100~200之间既不能被3整除也不能被7整除的整数并统计这些整数的个数,要求每行输出8个数。