C语言改错题以及参考答案
- 格式:docx
- 大小:22.08 KB
- 文档页数:12
改错题(一)1.Cmody.011.C,功能是:从字符串数组str1中取出ASCII码值为偶数且下标为偶数的字符依次存放到字符串t中。
如:若str1所指的字符串为:4Az18c?Ge9a0z! 则t所指的字符串为:4z8z注意:数组下标从0开始#include <math.h>#include <stdio.h>#include <string.h>#include <conio.h>void main(){char str1[100],t[200];int i,j;/**/ i=0;j=o;/**/strcpy(str1,"4Az18c?Ge9a0z!");for(i=0;i<strlen(str1);i++){/**/ if((str1[i]%2==0)&&(i%2!==0)) /**/{t[j]=str1[i];j++;}}t[j]='\0';printf("\nOriginal string :%s\n",str1);printf("\nResult string :%s\n",t);}2.Cmody012.C,fun()功能:根据n,计算大于10的最小n个能被3整除的正整数的倒数之和。
如:fun(8)=1/12+1/15+1/18+1/21+1/24+1/27+1/30+1/33=0.396#include <math.h>#include <stdio.h>#include <string.h>#include <conio.h>#define M 50double fun(int n){double y=0.0;int i,j;j=0;for(i=1;;i++){/**/ if(i<>10)&&(i%3==0)) /**/{ /**/ y+=1 1.0/i; /**/j++;}if(j==n) break;}return y;}void main(){printf("fun(8)=%8.3lf\n",fun(8));}(二)1.Cmody021.C,输出:*************************#include <stdio.h>void main(){/**/int i;,j; /**/for(i=1;i<=5;i++){for(j=1;j<=10-2*i;j++) printf(" ");/**/ for(j=1;j<=52*i-1;j++) /**/printf("*");printf("\n");}}2.Cmody022.C的功能是求解百元买百鸡问题:设一只公鸡2元,一只母鸡1元,一只小鸡0.5元。
『Turbo C——程序改错』题目1:在一个一维整型数组中找出其中最大的数及其下标。
程序中共有4条错误语句,请改正错误。
--------------------------------------------------------注意:不可以增加或删除程序行,也不可以更改程序的结构。
------------------------------------------------------*/#define N 10/**********FOU ND**********/ float fun(int *a,int *b,int n){int *c,max=*a;for(c=a+1;c<a+n; c++)if(*c>max){max=*c;/**********FOU ND**********/b=c-a;} return max;}main(){inta[N],i,max,p=0; printf("please enter 10integers:\n");for(i=0;i<N;i++)/**********FOUND**********/get("%d",a[i]);/**********FOUND**********/m=fun(a,p,N);printf("max=%d,position=%d",max,p);}--------------------------------------------------『错误答案』:1 intfun(int *a,int*b,int n)2*b=c-a;}3scanf("%d",&a[i]);4max=fun(a,&p,N);『Turbo C——程序改错』--------------------------------------------------------题目2:为一维数组输入10个整数;将其中最小的数与第一个数对换,将最大的数与最后一个数对换;输出数组元素。
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;}```详解答案:在调用一个函数之前,需要确保该函数已经被定义。
改错参考答案:(3)将字符串yy在屏幕上输出#include<stdio.h>void main(){/*********Found************/ char yy[100] = "ok??\n";/*********Found************/f0r (; *yy; yy++){putchar(*yy);}}#include<stdio.h>void main(){/*********Found************/ char*yy="ok??\n";/*********Found************/for (; *yy; yy++){putchar(*yy);}}(4)计算半径为2+3的圆的面积#include<stdio.h>#define PI3."14/*********Found************/#define S(r) PI*r*rvoid main(){/*********Found************/ int mianJi;mianJi = S(2+3);printf("mian ji=%5."2f\n", mianJi);}#include<stdio.h>#define PI3."14/*********Found************/#define S(r) PI*(r)*(r)void main(){/*********Found************/1floatmianJi; mianJi = S(2+3);printf("mian ji=%5."2f\n", mianJi);}(8)打开文件d:\te.c用于读并判断打开是否成功#include<stdio.h>void main(){FILE *fp;/*********Found************/char fileName[] = "d:\te.c";/*********Found************/fp = fopen(fileName, "w");/*********Found************/if (fp == EOF){puts("File Open Error!");exit(1);}putchar(fgetc(fp));fclose(fp);}#include<stdio.h>void main(){FILE *fp;/*********Found************/char fileName[] = "d:\\te.c";/*********Found************/fp = fopen(fileName, "r");/*********Found************/if (fp ==NULL){puts("File Open Error!");exit(1);}putchar(fgetc(fp));fclose(fp);}(9)申请100个字节的内存空间,显示其首地址,然后释放申请到的内存空间#include<stdio.h>#include<alloc.h>void main(){/*********Found************/char p[100];2/*********Found************/if (p = (char *)malloc(100) == NULL){printf("malloc memory fail!\n");return ;}printf("%p\n", p);/*********Found************/fclose(p);}#include<stdio.h>#include<alloc.h>void main(){/*********Found************/ char*p;/*********Found************/if ((p = (char *)malloc(100))== NULL){printf("malloc memory fail!\n"); exit(1);}printf("%p\n", p);/*********Found************/free(p);}(10)将字符串p显示在屏幕上#include<stdio.h>/*********Found************/#define BEGIN/*********Found************/#define ENDvoid main(){char *p = "";const int i = 0;for (printf("\n"); p[i]; )BEGINputchar(p[i]);i++;END}#include<stdio.h>/*********Found************/#define BEGIN{/*********Found************/#define END}3void main(){char *p = "";const int i = 0;for (printf("\n"); p[i]; )BEGINputchar(p[i]);/*********Found************/p++;END}(14)调用函数swap,将a和b的值交换,最后在屏幕上显示交换后的a,b之值#include<stdio.h>/*********Found************/void swap(int x, int y){int tmp;/*********Found************/______*x = *y;/*********Found************/y = x;}void main(){int a = 3, b = 4;swap(a, b);printf("a=%d b=%d\n", a, b);}#include<stdio.h>/*********Found************/void swap(int*x, int*y){int tmp;/*********Found************/tmp = *x;*x = *y;/*********Found************/*y = tmp;}void main(){int a = 3, b = 4;/*********Found************/swap(&a,&b);printf("a=%d b=%d\n", a, b);}4(15)调用函数swap,将a和b的值交换,最后在屏幕上显示交换后的a,b之值#include<stdio.h>void swap(int *x, int *y){int *tmp, xy;/*********Found************/*tmp = x;*x = *y;/*********Found************/y = *tmp;}void main(){int a = 3, b = 4;/*********Found************/swap(*a, *b);/*********Found************/printf("a=%d b=%d\n", &a, &b);}#include<stdio.h>void swap(int *x, int *y){int *tmp, xy;/*********Found************/xy=*x;*x = *y;/*********Found************/*y =xy;}void main(){int a = 3, b = 4;/*********Found************/swap(&a,&b);/*********Found************/printf("a=%d b=%d\n",a,b);}(16)worker的信息使用结构体存储,从键盘读入其各项信息并显示#include<stdio.h>void main(){struct WKER{long ID;long int num;char name[20];5char sex;/*********Found************/} ;worker.ID = 1L;/*********Found************/scanf("%d %s %s", &worker.num, , &worker.sex);/*********Found************/printf("worker's info:num=%d name=%s sex=%s\n",worker.num, , worker.sex);}#include<stdio.h>void main(){struct WKER{long ID;long int num;char name[20];char sex;/*********Found************/}worker;worker.ID = 1L;/*********Found************/scanf("%ld %s %c", &worker.num, ,&worker.sex);/*********Found************/printf("worker's info:num=%ld name=%s sex=%c\n",worker.num, , worker.sex);}(17)函数userLogin的功能主要是统计并返回登录的用户数,用户名最长30字节#include <stdio.h>long userLogin(char *userName){/*********Found************/long userCount = 0;userCount++;/*********Found************/return ;}void main( ){/*********Found************/char *userName;int i;while(1){printf("userName:");scanf("%s", userName);i = userLogin(userName);6}}#include <stdio.h>long userLogin(char *userName){/*********Found************/ staticlong userCount = 0;userCount++;/*********Found************/returnuserCount;}void main( ){/*********Found************/ charuserName[31];int i;while(1){printf("userName:");scanf("%s", userName);i = userLogin(userName);}}(19)将inBuf中字符串拆分成一个个的单词/*单词之间的分隔符由串divChar,程序中定义为“;?!,.>/\”。