南開二級C語言上機(jī)改錯100題_第1頁
南開二級C語言上機(jī)改錯100題_第2頁
南開二級C語言上機(jī)改錯100題_第3頁
南開二級C語言上機(jī)改錯100題_第4頁
南開二級C語言上機(jī)改錯100題_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、南開二級c語言上機(jī)100題1、 改錯題1下列給定程序的功能是:讀入一個整數(shù)(2<=k10000,打印它的所有質(zhì)因子(即所有為素?cái)?shù)的因子)。例如,若輸入整數(shù)2310,則應(yīng)輸出2、3、5、7、11。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include        <conio.h>#include        <stdio.h&g

2、t;*isprime(int n);       去掉分號int i,m; m=1; for ( i=2; i<n; i+)*    if !(n%i)         if (!(n%i)     m=0; break; 

3、 return (m);main() int j,k;  clrscr();  printf("nplease enter an integer number between 2 and 10000:");scanf("%d",&k);  printf("nnthe prime factor(s) of %d 

4、is (are):",k);  for (j=2;j<=k;j+)     if (!(k%j)&&(isprime(j) printf("n %4d",j);  printf("n");)2、 編程題1m個人的成績存放在score數(shù)組中,請編寫函數(shù)fun,它的功能是:將低于平均分的人數(shù)作為函數(shù)值行會,將低于平均分的分?jǐn)?shù)放在below所指的數(shù)組中。例如,但score數(shù)組的數(shù)據(jù)為10、2

5、0、30、40、50、60、70、80、90時,函數(shù)返回的人數(shù)應(yīng)該時4,below中的數(shù)據(jù)應(yīng)為10、20、30、40。注意:部分源程序給出如下。請勿改動主函數(shù)main和其他函數(shù)中的任何內(nèi)容僅在函數(shù)fun的花括號中填入所編寫的若干語句。試題程序:#include <conio.h>#include <stdio.h>#include <string.h>int fun (int score,int m, int below)    

6、                     int i,k=0;float aver=0;                       

7、60;              for(i=0;i<m;i+)                                

8、60;      aver+=(scorei);                                        aver/=

9、m;                                     for (i=0;i<m;i+)        

10、0;                             if (scorei<aver)                

11、0;                    belowk=scorei;                           &#

12、160;            k+;                                     

13、;retern(k);main()int i, n, below9; int score 9=10,20,30,40,50,60,70,80,90; clrscr(); n=fun(score,9,below); printf("nbelow the average score are:"); for (i=0;i<n;i+) printf ("%d",belowi);3、 改錯2

14、下列給定程序中,函數(shù)fun的功能是:逐個比較a、b兩個字符串對應(yīng)位置中的字符,把a(bǔ)scii值大或等于的字符一次存放到c數(shù)組中,形成一個新的字符串。例如,若a中的字符串為abcdefgh,b中的字符串為:abcd,則c中的字符串為:abcdefgh。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <studio.h>#include <string.h>*void; fun(char *p,char *q,char *c)去分

15、號*int k=1;                                        1改為0*  while(*p!=*q)  

16、60;                            !=  改為  |*   if (*p<*q) ck=*q;      小于號改為小于等于 

17、    else      ck=*p;     if (*p) p+;     if (*q) q+;     k+;   main()char a10="abcdefgh",b10="abcd",c80=''&

18、#160;fun(a,b,c); printf("the string a:"); puts(a); printf("the string b:"); puts (b); printf("the result:"); puts(c);4、 改錯3下列給定程序中,函數(shù)fun的功能是:依次取出字符串中所有數(shù)字字符,形成新的字符串,并取代原字符串。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得

19、增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <stdio.h>#include <conio.h>void fun (char *s)  int i,j;   for (i=0,j=0;si!='0'i+)*       if (si>='0' && si<='9'

20、;)                sj=si;     j改為j*       sj="0"              sj='0'main(

21、)char item80; clrscr(); printf("nenter a string:");gets(item); printf("nnthe string is :%sn",item); fun (item); printf("nnthe string of changing is :%sn",item);5、 改錯4下列給定程序中,函數(shù)fun的功能是:分

22、別銅級字符串中大寫字母和小寫字母的個數(shù)。例如,給字符串s輸入:aaaabbb123ccccccd,則應(yīng)該輸出結(jié)果:upper=6,lower=8。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <conio.h>#include <stdio.h>*void fun (char *s,int a, int b)  a應(yīng)為*a,b應(yīng)為*b while(*s) 

23、0;  if (*s>='a' && *s<='z')*      a+;                       (*a)+;     if&#

24、160;(*s>='a' && *s<='z')*      b+;                       (*b)+;     s+;   m

25、ain() char s100;int upper=0,lower=0;  clrscr();  printf("nplease a string:");gets(s);  fun(s,&upper, &lower);  printf("n upper=%d lower=%dn",upper,lower);6、 改錯5假定整數(shù)數(shù)列中的數(shù)不重復(fù),并存放在數(shù)組中。下列給定程序中,函數(shù)f

26、un的功能是:刪除數(shù)列中值為x的元素。n中存放的是數(shù)列中元素的個數(shù)。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <stdio.h>#define n 20fun (int *a,int n, int x)int p=0,i; an=x; while (x!=ap) p=p+1; if (p=n) return -1;

27、0;else for (i=p;i<n;i+)*  ai+1=ai;       ai=ai+1;  return n-1; main()int wn=-3,0,1,5,7,99,10,15,30,90,x,n,i; n=10; printf("the original data:n"); for (i=0;i<n;i+) printf

28、("%5d",wi); printf("ninput x (to delete):");scanf("%d",&x); printf("delete:%dn",x); n=fun(w,n,x); if (n=-1) printf("*nor be found!*nn"); else printf("the data after&#

29、160;deleted:n");  for (i=0,i<n;i+) printf("%5d",wi);printf("nn"); 7、 改錯6下列給定程序中,函數(shù)fun的功能是:根據(jù)整型形參m的值,計(jì)算如下公式的值。t=1-1/2×2-1/3×3-1/m×m例如,若m中的值為5,則應(yīng)輸出:0.536389。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include <

30、;conio.h>#include <stdio.h>double fun (int m) double y=1.0;  int i;*  for (i=2;i<m;i+)            “<”改為“<=”*     y-=1/(i*i);  &

31、#160;         “1”改為“1.0”  return(y);main ()int n=5;clrscr();printf("nrhe result is %1fn",fun(n);8、 改錯7下列給定程序中,函數(shù)fun的功能是:用選擇法對數(shù)組中的n個元素按從小到大的順序進(jìn)行排序。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#inc

32、lude <stdio.h>#define n 20void fun(int a,int n) int i,j,t,p;  for (j=0;j<n-1;j+)*   p=j                      &#

33、160;    加上“;”    for (i=j;i,n;i+)           j改為j+1      if (ai<ap)*        p=j;       &

34、#160;            j改為i    t=ap;ap=aj;aj=t;    main()int an=9,6,8,3,-1,i,m=5;printf("排序前的數(shù)據(jù):");for (i=0;i<m;i+) printf("%d",ai);printf("n");fun(a,m);print

35、f("排序后的數(shù)據(jù):");for (i=0;i<m;i+) printf("%d",ai);printf("n");9、 改錯8(2004.7.27)下列給定程序中,函數(shù)fun的功能是:在字符串str中找出ascii碼值最大的字符,將其放在第一個位置上;并將該字符前的原字符向后順序移動。例如,調(diào)用fun函數(shù)之前給字符串輸入:abcdefgh,調(diào)用后字符串中的內(nèi)容為eabcdfgh。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#incl

36、ude <stdio.h>fun(char *p) char max,*q;int i=0;  max=pi;  while(pi!=0)    if(max<pi)    max=pi;*     p=q+i;            

37、60;    改為q=p+i;           i+;  *  while(q<p)                q<p改為q>p   *q=*(q-1);   &#

38、160;q-;      p0=max;main()char str80; printf("enter a string:");gets(str); printf("nthe original string:         ");puts(str); fun(str); printf("nthe 

39、string agter moving:");puts (str);ptintf("nn");10、 改錯9(2004.7.27)下列給定程序中,函數(shù)fun的功能是:從n個學(xué)生的成績中統(tǒng)計(jì)出低于平均分的學(xué)生人數(shù),此人數(shù)由函數(shù)值返回,平均分存放在形參aver所指的存儲單元中。例如,若輸入8名學(xué)生的成績:    80.5   60   72   90.5   98 

40、60; 51.5   88   64則低于平均分的學(xué)生人數(shù)為4(平均分為:75.5625)。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include <conio.h>#include <stdio.h>#define n 20int fun (float *s,int n,float *aver)float ave,t=0

41、.0; int count=0,k,i; for (k=0;k<n;k+)*   t=sk;                     t+=sk; ave=t/n; for (i=0;i<n;i+)  if (si<ave) cou

42、nt+;* *aver=&ave;                   去掉& return count;main()float s30,aver; int m,i; clrscr(); printf("nplease enter m:"scanf("%d&quo

43、t;,&m); printf("nplease enter %d mark:n",m); for (i=0;i<m;i+) scanf("%f",s+i); printf("nthe number of students:%dn",fun(s,m,&aver); printf("ave=%fn",aver);)11、 改錯10(2004.8.1)下列給定程序中,函數(shù)fun的功

44、能是:將s所指字符串中出現(xiàn)的t1所指子串全部替換成t2所指子字符串,所形成的新串放在w所指的數(shù)組中。在此處,要求t1和t2所指字符串的長度相同。例如,當(dāng)s所指字符產(chǎn)中的內(nèi)容為abcdabfab,t1所指子串中的內(nèi)容為ab,t2所指子串中的內(nèi)容為99時,結(jié)果,在2所指的數(shù)組中內(nèi)容應(yīng)為99cd99f99。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include<conio.h>#include<stdio.h>#include<string.h>int fun (

45、char *s, char *t1,char*t2,char*w)  int i;   char *p,*r,*a;  strcpy(w,s);  while(*w)   p=w;r=t1;*    while(r)              

46、60;           r改為*r     if(*r=*p)  r+;p+     else break;    if(*r='')     a=w;r=t2;*       &

47、#160;    while(*r)*a=*r;a+;r+     改為while(*r)*a=*r;a+;r+;      w+=strlen(t2);         else w+;  main() char s100,t1100,t2100,w100; clrscr(); prin

48、tf("nplease enter string s:");scanf("%s",s); printf("nplease enter string t1:");scanf("%s",t1); printf("nplease enter string t2:");scanf("%s",t2); if (strlen(t1)=strlen(t2)

49、60;       printf("nthe result is :%sn",w);    else printf("error:strlen(t1)!=strlen(t2)n");12、 改錯11(2004.8.1)給定程序modi1.c中,fun函數(shù)的功能是:先從鍵盤上輸入一個3行3列矩陣的各個元素的值,然后輸出主對角線元素之積。請改正程序中的錯誤,或在橫線處填上適當(dāng)?shù)膬?nèi)容并把橫線刪除,使它能得出正確的

50、結(jié)果。注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include <stdio.h>int fun ()int a 3 3, sum;int i, j;*_;                         

51、60;   sum = 1;for (i=0;i<3;i+) for (j=0;j<3;j+)*  scanf (%d a i j);        scanf("%d", &aij);for (i=0; i<3; i+)  sum=sum*ai i;printf

52、 (sum=%dn, sum);main ()     fun ();     13、 改錯12(8.1)下列程序的功能是:讀入一個整數(shù) k(2k10000),打印它的所有質(zhì)因子(即所有為素?cái)?shù)的因子)。例如,若輸入整數(shù):2310,則應(yīng)輸出:2、3、5、7、11。請改正程序中的語法錯誤,使程序能得出正確的結(jié)果。注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include    

53、     conio.h#include         stdio.h*isprime  ( int  n );                 isprime(int n)   int&

54、#160; i,  m;m = 1;for ( i = 2; i < n/ i+ )*  if  !( n%i )                       if

55、(!(n%i)     m=0; break    return ( m );main ()   int  j, k;clrscr ();printf (npease enter an integer number between 2 and 10000:); scanf (&#

56、160; %d,&k  );printf ( nn the prime factor (s) of % d is ( are ):, k );for (  j = 2; j <=  k; j+ )  if (  ( ! k%j

57、 ) ) && (  isprime ( j )  )      printf ( n % 4d, j );printf (n);14、 改錯13(8.4)下列程序中,fun函數(shù)的功能是:根據(jù)形參m,計(jì)算如下公式的值。t=1+ 1/2 + 1/3 + 1/4 +  

58、;+ 1/m例如,若輸入5,則應(yīng)輸出2.283333。請改正程序中的語法錯誤,使程序能得出正確的結(jié)果。注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include<conio.h>#include<stdio.h>double fun(int m)  double t=1.0;  int i;  for(i=2;i<=m;i+)*   t+=1.0/k;   

59、60;        k改為i*  _;        填return t;main()int m;clrscr();printf(“nplease enter 1 integer number:”);scanf(“%d”,&m);printf(“nthe result is %1fn”,fun(m);15、 改錯14(

60、8.4)下列程序中,fun和funx函數(shù)的功能是:用二分法求方程2x×x×x4x×x3x60的一個根,并要求絕對誤差不超過0.001。例如,若給m輸入100,給n輸入90,則函數(shù)求得的一個根為2.000。請改正程序中的語法錯誤,使程序能得出正確的結(jié)果。注意:不要改動main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序:#include<stdio.h>#include<math.h>double funx(double x)  return(2*x*x*x-4*x*x+3*x-6);doubl

61、e fun(double m, double n)* int r;改為double r; r=(m+n)/2;* while(fabs(n-m)<0.001)          "<" 改為" >= " if(funx(r)*funx(n)<0 m=r; else n=r;&

62、#160;r=(m+n)/2;  ) return r;)main()double m,n,root; printf("enter m n:n");scanf("%1f%1f",&m,&n); root=fun(m,n); printf("root=%6.3fn",root);16、 改錯15(8.5)下列給定程序中,函數(shù)fun的功能是:判斷字符ch是否與str所指串中的某個字符相同;若相同,則什么也不做,若不同,則將器

63、插在串的最后。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <conio.h>#include <stdio.h>#include <string.h>*void fun(char str, char ch)-1 while(*str && *str!=ch) str+;*    if (*str

64、=ch)-2   str0=ch;*    str1='0'-3      main()char s81,c; clrscr(); printf("nplease enter a string:n");gets(s) printf("nplease enter the character to sear

65、ch:"); c=getchar(); fun(s,c); printf("nthe result is %sn",s);15 題answer:1.   void fun(char *str, char ch)2.   if (*str='17、 改錯16(8.5)下列給定程序中,函數(shù)creatlink的功能是:創(chuàng)建帶頭結(jié)點(diǎn)的單項(xiàng)鏈表,并為各結(jié)點(diǎn)數(shù)據(jù)域賦0到m-1的值。請改正程序

66、中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <conio.h>#include <stdio.h>#include <stdlib.h>typedef struct aa int data;  struct aa *next;node;node *creatlink(int n, int m)node *h=null,&

67、#160;*p,*s; int i; s=(node *)malloc(sizeof(node);* h=p;-1 p->next=null; for (i=1;i<=n;i+) s=(node *)malloc(sizeof(node);*  s->data=rand()%m;-2  s->next=p->next;  p->next=s;  p=p->next; 

68、;* return p;-3outlink(node *h)node *p; p=h->next; printf("nnthe list:nn head"); while(p) printf("->%d",p->data);  p=p->next;  printf("n"); main() node *head;  clrs

69、cr();  head=creatlink(8,22);  outlink(head);16 題answer:1.   h=p=s;2.   s->data=rand()%(m-1);3.   return h;18、 改錯17(8.5)下列給定程序中,函數(shù)fun的功能是:計(jì)算并輸出k以內(nèi)最大的10個能被13或17整除的自然數(shù)之和。k的值由主函數(shù)傳入,若k的值為500,則函數(shù)值為4622。請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不

70、要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試題程序#include <conio.h>#include <stdio.h>int fun(int k)int m=0, mc=0,j; while (k>=2)&&(mc<10)* if (k%13=0)|(k%17=0)-1    m=m+k;mc+;  k-;  return m;*_-2main()clrscr(); printf("%dn",fun(500);17 題answer:1.   if (k%13=0)|(k%17=0)2.   19、 改錯18(8.5)下列給定程序中,函數(shù)fun的功能是:實(shí)現(xiàn)兩個整數(shù)的交換。例如給a和b分別輸入60和65,輸出為:a=65  b=60請改正程序中的錯誤,使程序能得出正確的結(jié)果。注意,不要改多main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!試

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論