最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

全國計(jì)算機(jī)等級(jí)考試二級(jí)C語言練習(xí)題(十三)

2021-02-14 10:06 作者:朝顏晚扶桑  | 我要投稿

1.程序填空題

程序的功能是計(jì)算 ,例如,輸入?yún)?shù)3,結(jié)果為10。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

long fun(int n)

{

int I;

long f;

f=___1___;

for(i=1;i<=n;i++)

f=___2___;

return f;

}

main()

{

long s;

int k,n;

printf("Please input the data:");

scanf("%d",&n);

s=___3___;

for(k=0;k<=n;k++)

s=___4___;

printf("the resutl is : %ld\n",s);

}

【答案】

(1)1

(2)f*i

(3)0

(4)s+fun(k)?

2.程序修改題

在給定程序中,函數(shù)fun的功能是:先從鍵盤上輸入一個(gè)3行3列的矩陣的各個(gè)元素的值,然后輸出主對(duì)角線元素之積。

請(qǐng)改正函數(shù)fun中的錯(cuò)誤,使它能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。

#include <stdio.h>

int fun()

{

int a[3][3],mul;

int I,j;

mul=1;

for(i=0;i<3;i++)

{

/**********found**********/

for(i=0;j<3;j++)

scanf("%d",&a[i][j]);

}

for(i=0;i<3;i++)

/**********found**********/

mul=mul*a[i][j];

printf("Mul=%d\n",mul);

}

main()

{

fun();

}

【答案】

(1)將for (i=0;j<3;j++)改為:

for (j=0;j<3;j++)

(2)將mul=mul*a[i][j];改為:

mul=

Chapter_8

mul*a[i][i];

3.程序設(shè)計(jì)題

請(qǐng)編寫函數(shù)fun(),其功能是:計(jì)算正整數(shù)n所有因子(1和n除外)之和作為函數(shù)值返回。

例如:n=256時(shí),函數(shù)值為254。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

int fun(int n)

{

?

}

main()

{

printf("%d\n",fun(256));

}

【答案】

int fun(int n)

{

int s=0,i;

for(i=2;i<n;i++)

if(n%i==0)

s+=i;

return s;

}

1.程序填空題

請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:從鍵盤輸入一組整數(shù),使用條件表達(dá)式找出最大的整數(shù)。當(dāng)輸入的整數(shù)為-1時(shí)結(jié)束。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <conio.h>

#define NUM 100

main()

{

int n[NUM];

int i=-1;

int MAX=-1;

printf("\nInsert integer with the '-1' as end: \n");

do

{

i++;

printf("n[%d]=",i);

scanf("%d",___1___);

MAX=___2___n[i] : MAX;

}while(___3___);

printf("The MAX=%d\n",MAX);

}

【答案】

(1)&n[i] (2)MAX<n[i]? (3)n[i]!=-1?

2.程序修改題

在給定程序中,函數(shù)fun()的功能是:為一個(gè)偶數(shù)尋找兩個(gè)素?cái)?shù),這兩個(gè)素?cái)?shù)之和等于該偶數(shù),并將這兩個(gè)素?cái)?shù)通過形參指針傳回主函數(shù)。

請(qǐng)改正函數(shù)fun中指定部位的錯(cuò)誤,使其能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <math.h>

void fun(int a,int *b,int *c)

{

int I,j,d,y;

for(i=3;i<=a/2;i=i+2)

{

/**********found**********/

y=1

for(j=2;j<=sqrt((double)i);j++)

if(i%j==0) y=0;

if(y==1)

{

/**********found**********/

d=a-I;

for(j=2;j<=sqrt((double)d);j++)

if(d%j==0) y=0;

if(y==1) { *b=I; *c=d; }

}

}

}

main()

{

int a,b,c;

do

{ printf("Input a: ");

scanf("%d",&a);

}while(a%2);

fun(a,&b,&c);

printf("%d=%d+%d\n",a,b,c);

}

【答案】

(1)將y=1 改為:y=1;

(2)將d=a-I;改為:d=a-i;

3.程序設(shè)計(jì)題

請(qǐng)編寫一個(gè)函數(shù)fun(char *s),函數(shù)的功能是把 s串中的內(nèi)容逆置。

例如,s串中原有的字符串為:abcdefg,則調(diào)用該函數(shù)后的內(nèi)容為:gfedcba。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <string.h>

#include <stdio.h>

#define N 81

fun(char*s)

{

?

}

main()

{

char a[N];

printf( "Enter a string : " );

gets(a);

printf( "The original string is : " );

puts(a);

fun(a);

printf("\n");

printf( "The string after modified : ");

puts(a);

}

【答案】

fun(char *s)

{

char b[N];

int i=0,j;

memset(b,0,N);

for(j=strlen(s)-1;j>=0;j--)

b[i++]=s[j];

strcpy(s,b);

}

1.程序填空題

請(qǐng)補(bǔ)充fun函數(shù),該函數(shù)的功能是求不超過給定自然數(shù)的各奇數(shù)之和。例如,輸入34,則輸出結(jié)果為289。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

int fun(int n)

{

int I,sum;

sum=___1___;

for(i=1;___2___;i+=2)

sum+=I;

return sum;

}

main()

{

int a;

do

{

printf("\nPlease enter natural numbers a:");

scanf("%d",&a);

}while(a<=0);

printf("\n不超過給定自然數(shù)%d的各奇數(shù)之和為%d\n",a,fun(a));

}

【答案】

(1)0 (2)i<=n

2.程序修改題

給定程序中,函數(shù)fun的功能是:把m(1≤m≤10)個(gè)字符串連接起來,組成一個(gè)新串,放入pt中。

例如,把3個(gè)串“abc”、“CD”、“EF”串連起來,結(jié)果是“abcCDEF”。

請(qǐng)改正程序中的語法錯(cuò)誤,使其能統(tǒng)計(jì)出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <string.h>

int fun(char str[][10],int m, char *pt)

{

/**********found**********/

int k,i;

for(k=0;k<m;k++)

{

q=strlen(str[k]);

for(i=0;i<q;i++)

/**********found**********/

pt[i]=str[k];

pt+=q;

pt[0]=0;

}

}

main()

{

int n,h;

char s[10][10],p[120];

printf("Please enter n:");

scanf("%d",&n); gets(s[0]);

printf("Please enter %d string:\n", n);

for(h=0;h<n;h++) gets(s[h]);

fun(s, n, p);

printf("The result is : %s\n", p);

}

【答案】

(1)將int k,i; 改為:int k,q,i;

(2)將pt[i] = str[k]; 改為:

pt[i] = str[k][i]

3.程序設(shè)計(jì)題

請(qǐng)編寫函數(shù)fun,用來求出數(shù)組的最小元素在數(shù)組中的下標(biāo)并存放在k所指的存儲(chǔ)單元中。

例如,輸入如下整數(shù):

234 345 753 134 436 458 100 321 135 760

則輸出結(jié)果為6,100。

注意:部分源程序給出如下。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <conio.h>

#include <stdio.h>

#include <windows.h>

int fun(int *s,int t,int *k)

{

?

}

main()

{

int a[10]={234,345,753,134,436,458,

100,321,135,760},k;

system("cls");

fun(a,10,&k);

printf("%d,%d\n",k,a[k]);

}

【答案】

int fun(int *s,int t,int *k)

{

int i; *k=0;

for(i=0;i<t;i++)

if(s[*k]>s[i]) *k=i;

return s[*k];

}

1.程序填空題

在給定程序中,函數(shù)fun的功能是計(jì)算score中m個(gè)人的平均成績aver,將低于aver的成績放在below中,通過函數(shù)名返回人數(shù)。

例如,當(dāng)score={10,20,30,40,50,60,70,80,90},m=9時(shí),函數(shù)返回的人數(shù)應(yīng)該是4,below= {10,20,30,40}。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <string.h>

int fun(int score[],int m,int below[])

{

int I,j=0;

float aver=0.0;

for(i=0; i<m; i++) aver+=score[i];

aver/=(float)m;

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

/**********found**********/

if(score[i]<aver)___1___;

return j;

}

main()

{

int I,n,below[9];

int score[9]={10,20,30,40,50,60,70,80,90};

/**********found**********/

n=fun(___2___);

printf("Below the average score are: " );

for(i=0; i<n; i++)

printf("%d ",below[i]);

}

【答案】

(1)below[j++]=score[i]

(2)score, 9, below

2.程序修改題

在給定程序中,函數(shù)fun的功能是:從鍵盤上輸入的每個(gè)單詞的第一個(gè)字母變?yōu)榇髮懽帜?,輸入時(shí)各單詞必須用空格隔開,用“.”結(jié)束輸入。

請(qǐng)改正程序中的錯(cuò)誤,使程序能輸出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

int fun(char *c,int status)

{

/***********found**********/

if(*c==' ') return 1;

else

{

if(status&&*c<='z'&&*c>='a')

/***********found**********/

*c+='a'-'A';

return 0;

}

}

main()

{

int flag=1;

char ch;

do

{

ch=getch();

flag=fun(&ch, flag);

putchar(ch);

}while(ch!='.');

}

【答案】

(1)將if(*c=' ') return 1;改為:

if(*c==' ') return 1;

(2)將*c += 'a' - 'A'; 改為:

*c += 'A' - 'a';

3.程序設(shè)計(jì)題

給定程序的功能是:刪除一個(gè)字符串中指定的字符。設(shè)有如下的字符串:turbo c and borland vc++,從鍵盤上輸入任一個(gè)字符,然后從上述字符串中刪除所有該字符。

例如,輸入字符n,則刪除后變?yōu)閠urbo c ad borlad vc++。

如果輸入了字符串中不存在的字符,則字符串照原樣輸出。(區(qū)分大小寫)刪除字符的操作通過一個(gè)函數(shù)來實(shí)現(xiàn),請(qǐng)編寫該函數(shù)的代碼。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

int fun(char s[], int c)

{

?

}

main()

{

static char str[]="turbo c and Borland

c++";

char ch;

printf("Enter a char to be deleted: ");

scanf("%c", &ch);

fun(str, ch);

printf("str[]=%s\n",str);

}

【答案】

int fun(char s[],int c)

{

char *p=s;

int i=0;

while(*p)

{

if(*p!=c) s[i++]=*p;

p++;

}

s[i]=0;

}

1.程序填空題

給定程序的功能是求出能整除x且不是偶數(shù)的各整數(shù),并放在數(shù)組pp中,這些除數(shù)的個(gè)數(shù)由n返回。

例如,若x的值為30,則有4個(gè)數(shù)符合要求,它們是1、3、5、15。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

void fun(int x,int pp[],int *n)

{

int I,j=0;

for(i=1;i<=x;i+=2)

/**********found**********/

if(___1___ ) pp[j++]= I;

/**********found**********/

___2___;

}

main()

{

int x,aa[1000],n,I;

printf("Enter an integer number:\n");

scanf("%d",&x);

fun(x,aa,&n);

for(i=0;i<n;i++)

printf("%d ",aa[i]);

printf("\n");

}

【答案】

(1)(x%i)==0 (2)*n=j

2.程序修改題

在給定程序中,函數(shù)fun的功能是:輸入兩個(gè)雙精度數(shù),函數(shù)返回他們的平方根的和值。

例如,輸入:22.993612和84.57629812

輸出:y = 13.991703。

請(qǐng)改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <math.h>

/**********found**********/

double fun(double *a,double *b);

{

double c;

/**********found**********/

c=sqrt(*a+*b);

return c;

}

main()

{

double a,b,y;

printf("Enter a, b : ");

scanf("%lf%lf",&a,&b);

y=fun(&a,&b);

printf("y=%f\n",y);

}

【答案】

(1)將double fun (double *a, double *b); 改為:

double fun (double *a, double *b)

(2)將c = sqrt( *a +*b ); 改為:

c = sqrt( *a ) + sqrt( *b );

3.程序設(shè)計(jì)題

請(qǐng)編寫一個(gè)函數(shù)void fun(int bb[], int *n, int y),其中*n表示bb數(shù)組中元素的個(gè)數(shù)。

函數(shù)的功能是:刪除bb中所有值為y的元素。Bb數(shù)組元素中的值和y的值由主函數(shù)通過鍵盤讀入。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

#define M 20

void fun(int bb[],int *n,int y)

{

?

}

main()

{

int aa[M],n,y,k;

printf("Please enter n : ");

scanf("%d", &n);

printf("Enter%d positive number :",n);

for(k=0;k<n;k++) scanf("%d",&aa[k]);

printf("The original data is : \n" );

for(k=0; k<n; k++)

printf("%5d",aa[k]);

printf("Enter a number to deleted: " );

scanf("%d",&y);

fun(aa,&n,y);

printf("The data after deleted %d :",y);

for(k=0;k<n;k++)

printf("%4d",aa[k]);

printf("\n");

}

【答案】

void fun(int bb[],int *n,int y)

{

int i, m=0;

for(i=0; i<*n; i++)

{ if(bb[i]!=y) bb[m++]=bb[i]; }

*n=m;

}

1.程序填空題

函數(shù)fun的功能是:統(tǒng)計(jì)長整數(shù)n的各個(gè)位上出現(xiàn)數(shù)字1、2、3的次數(shù),并通過外部(全局)變量c1、c2、c3返回主函數(shù)。例如:當(dāng)n=123114350時(shí),結(jié)果應(yīng)該為:c1=3 c2=1 c3=2。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

int c1,c2,c3;

void fun(long n)

{

c1=c2=c3=0;

while(n)

{

/**********found**********/

___1___(n%10)

{

/**********found**********/

case 1: c1++; ___2___;

/**********found**********/

case 2: c2++;___3___;

case 3: c3++;

}

n/=10;

}

}

main()

{

long n=123114350L;

fun(n);

printf("The result :\n");

printf("n=%ld c1=%d c2=%d c3=%d\n",n,c1,c2,c3);

}

【答案】

(1)switch (2)break; (3)break;

2.程序修改題

下列給定程序中,函數(shù)fun的功能是:把主函數(shù)中輸入的3個(gè)數(shù),最大的放在x中,最小的放在z中。

例如,輸入的數(shù)為:1 2 3,輸出結(jié)果應(yīng)當(dāng)是:x=3.0,y=2.0,z=1.0。

請(qǐng)改正程序中的錯(cuò)誤,使其能得到正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不能增行或刪行,也不得更改程序的結(jié)構(gòu)。

#include <stdio.h>

void fun(float *p,float *q,float *s)

{

/****found******/

float *a;

if(*p<*q)

{

a=*p;

*p=*q;

*q=a;

}

/****found******/

if(*s<*p)

{

a=*s;

*s=*p;

*p=a;

}

if(*q<*s)

{

a=*q;

*q=*s;

*s=a;

}

}

main()

{

float x,y,z;

printf("Input x y z:");

scanf("%f%f%f",&x,&y,&z);

printf("x=%4.1f,y=%4.1f,z=%4.1f\n\n",x,y,z);

fun(&x,&y,&z);

printf("x=%4.1f,y=%4.1f,z=%4.1f\n\n",x,y,z);

}

【答案】

(1)將float *a;改為:float a;

(2)將if(*s<*p)改為:if(*s>*p)

3.程序設(shè)計(jì)題

請(qǐng)編寫函數(shù)fun,其功能是:計(jì)算并輸出給定數(shù)組(長度為5)中每相鄰兩個(gè)元素的平均值的平方根之和。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun()的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

#include <math.h>

double fun(double a[5])

{

?

}

main()

{

double f,c[5]={4.0,22.0,35.0,46.0,18.0};

int I;

FILE *out;

printf("\nThe original data is :\n");

for(i=0;i<5;i++)

printf("%6.1f",c[i]);

printf("\n\n");

f=fun?;

printf("f=%f\n\n",f);

out=fopen("outfile.dat","w");

fprintf(out,"%f",f);

fclose(out);

}

【答案】

double sum=0.0;

int i,j=1;

for(i=0;i<5;i++)

if(j<=4)

{

sum+=sqrt((a[i]+a[i+1])/2.0);

j++;

}

return sum;

1.程序填空題

給定程序的功能是把s串中所有的字符前移一個(gè)位置,串中的第一個(gè)字符移到最后。

例如:s串中原有的字符串為:Mn.123xyZ,則調(diào)用該函數(shù)后,s串中的內(nèi)容為:n.123xyZM。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#define N 81

fun(char *s)

{

char b[N];

fprintf(b,"%s%c",s+1,*s);

/**********found**********/

___1___;

}

main()

{

char a[N];

printf( "Enter a string : " );

gets(a);

printf("The original string is : " );

puts(a);

/**********found**********/

___2___;

printf("The string after modified : ");

/**********found**********/

___3___;

}

【答案】

(1) s[0] (2)fun(a); (3)puts(a)?

2.程序修改題

給定程序中,函數(shù)Creatlink的功能是創(chuàng)建帶頭結(jié)點(diǎn)的單向鏈表,并為各結(jié)點(diǎn)數(shù)據(jù)域賦0~m-1的值。

請(qǐng)改正函數(shù)Creatlink中指定部位的錯(cuò)誤,使其能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <stdlib.h>

typedef struct aa

{ int data;

struct aa *next;

}NODE;

NODE*Creatlink(int n,int m)

{

NODE *h=NULL,*p,*s;

int I;

/**********found***********/

p=(NODE)malloc(sizeof(NODE));

h=p;

p->next=NULL;

for(i=1;i<=n;i++)

{

s=(NODE*)malloc(sizeof(NODE));

s->data=rand()%m;

s->next=p->next;

p->next=s;

p=p->next;

}

/**********found***********/

return p;

}

outlink(NODE *h)

{

NODE *p;

p=h->next;

printf("THE LIST :\n HEAD ");

while(p)

{

printf("->%d",p->data);

p=p->next;

}

printf("\n");

}

main()

{

NODE *head;

head=Creatlink(8,22);

outlink(head);

}

【答案】

(1)將p=(NODE)malloc(sizeof(NODE)); 改為:

p=(NODE *)malloc(sizeof(NODE));

(2)將return p; 改為:return h;

3.程序設(shè)計(jì)題

請(qǐng)編寫函數(shù)fun(),它的功能是,根據(jù)下列式子計(jì)算s,計(jì)算結(jié)果作為函數(shù)值返回;n通過形參傳入:

例如:若n的值為15時(shí),函數(shù)的值為:1.875000

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

float fun(int n)

{

?

}

main()

{

int n;

float s;

printf("\n讀入一個(gè)正整數(shù)N:");

scanf("%d",&n);

s=fun(n);

printf("計(jì)算結(jié)果是: %f\n",s);

}

【答案】

float fun(int n)

{

int i,j,t; float s=0;

for(i=1;i<=n;i++)

{

t=0;

for(j=1;j<=i;j++) t+=j;

s=s+1.0/t;

}

return s;

}

1.程序填空題

給定程序中,函數(shù)fun的功能是:有N×N矩陣,以主對(duì)角線為對(duì)稱線,對(duì)稱元素相加并將結(jié)果存放在下三角元素中,右上三角元素置為0。

例如,若N=4,有下列矩陣:

21 12 13 24

25 16 47 38

29 11 32 54

42 21 33 10

計(jì)算結(jié)果為:

21 0 0 0

37 16 0 0

42 58 32 0

66 59 87 10

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#define N 4

void fun(int (*a)___1___)

{

int I,j;

for(i=1;i<N;i++)

{

for(j=0;j<I;j++)

{

___2___=a[i][j]+a[j][i];

___3___=0;

}

}

}

main()

{

int I,j,a[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10};

printf("\nThe original array:\n");

for(i=0;i<N;i++)

{

for(j=0;j<N;j++)

printf("%2d ",a[i][j]);

printf("\n");

}

fun(a);

printf("\nThe result is:\n");

for(i=0;i<N;i++)

{

for(j=0;j<N;j++)

printf("%2d ",a[i][j]);

printf("\n");

}

}

【答案】

(1)[N] (2)a[i][j] (3)a[j][i]

2.程序修改題

在給定程序中,函數(shù)fun的功能是:將s所指字符串中位于奇數(shù)位置的字符或ASCII碼為偶數(shù)的字符放入t所指數(shù)組中(規(guī)定第一個(gè)字符放在第0位中)。

例如,字符串中的數(shù)據(jù)為:AABBCCDDEEFF,則輸出應(yīng)當(dāng)是:ABBCDDEFF。

請(qǐng)改正函數(shù)fun中指定部位的錯(cuò)誤,使其能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <string.h>

#define N 80

void fun(char *s,char t[])

{

int I,j=0;

for(i=0;i<strlen(s);i++)

/***********found**********/

if(i%2 && s[i]%2==0)

t[j++]=s[i];

/***********found**********/

t[j]=0

}

main()

{

char s[N],t[N];

printf("Please enter string s : ");

gets(s);

fun(s,t);

printf("The result is : %s\n",t);

}

【答案】

(1)將if(i%2 && s[i]%2==0) 改為:

if(i%2||s[i]%2==0)

(2)將t[j]=0 改為:t[j]=0;

3.程序設(shè)計(jì)題

函數(shù)fun的功能是:把a(bǔ)數(shù)組中的n個(gè)數(shù)的平方值,與b數(shù)組中逆序的n個(gè)數(shù)的平方值一一對(duì)應(yīng)相減,結(jié)果存放在c數(shù)組中。

例如,當(dāng)a數(shù)組中的值是:1、3、5、7、8,b數(shù)組中的值是:2、3、4、5、8

調(diào)用該函數(shù)后,c中存放的數(shù)據(jù)是:-63、-16、9、40、60

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

void fun(int a[],int b[],int c[],int n)

{

?

}

main()

{

int I,a[100]={1,3,5,7,8},

b[100]={2,3,4,5,8},c[100];

fun(a,b,c,5);

printf("The result is: ");

for(i=0;i<5;i++) printf("%d ",c[i]);

printf("\n");

}

【答案】

void fun(int a[],int b[],int c[],int n)

{

int i;

for(i=0; i<n; i++)

c[i]=a[i]*a[i]-b[n-1-i]*b[n-1-i];

}

1.程序填空題

給定程序的功能是:調(diào)用fun函數(shù)建立班級(jí)通訊錄。通訊錄中記錄每位學(xué)生的編號(hào)、姓名和電話號(hào)碼。班級(jí)的人數(shù)和學(xué)生的信息從鍵盤讀入,每個(gè)人的信息作為一個(gè)數(shù)據(jù)塊寫到名為myfile5.dat的二進(jìn)制文件中。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <stdlib.h>

#define N 5

typedef struct

{ int num;

char name[10];

char tel[10];

}STYPE;

void check();

/**********found**********/

___1___fun(STYPE *std)

{

/**********found**********/

___2___fp; int I;

if((fp=fopen("OUT98.dat","wb"))==NULL)

return(0);

printf("Output data to file !\n");

for(i=0;i<N;i++)

/**********found**********/

fwrite(&std[i],sizeof(STYPE),1,fp);

fclose(fp);

return(1);

}

main()

{

STYPEs[10]={{1,"aaaaa","111111"},{2,"bbbbb","222222"},{3,"ccccc","333333"},{4,"ddddd","444444"},{5,"eeeee","555555"}};

int k;

k=fun(s);

if(k==1)

{ printf("Succeed!"); check(); }

else printf("Fail!");

}

void check()

{

FILE *fp; int I;

STYPE s[10];

if((fp=fopen("OUT98.dat","rb"))==NULL)

{ printf("Fail !!\n"); exit(0); }

printf("Read file and output to screen :\n");

printf("num name tel\n");

for(i=0;i<N;i++)

{

fread(&s[i],sizeof(STYPE),1,fp);

printf("%6d %s %s\n",

s[i].num,s[i].name,s[i].tel);

}

fclose(fp);

}

【答案】

(1)int (2)FILE *

2.程序修改題

在給定程序中,函數(shù)fun的功能是:比較兩個(gè)字符串,將長的那個(gè)字符串的首地址作為函數(shù)值返回。

請(qǐng)改正函數(shù)fun中指定部位的錯(cuò)誤,使其能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

/**********found**********/

char fun(char *s,char *t)

{

int sl=0,tl=0;

char *ss,*tt;

ss=s; tt=t;

while(*ss)

{ sl++;

/**********found**********/

ss++;

}

while(*tt)

{ tl++;

/**********found**********/

tt++;

}

if(tl>sl) return t;

else return s;

}

main()

{

char a[80],b[80],*p,*q;

int I;

printf("Enter a string : ");

gets(a);

printf("Enter a string again : ");

gets(b);

printf("The longer is :%s\n",fun(a,b));

}

【答案】

float fun(float h)

{

long w ;

w=h*100+0.5;

return(float)w/100;

}?

3.程序設(shè)計(jì)題

請(qǐng)編寫一個(gè)函數(shù)float fun(float h),函數(shù)的功能是保留h小數(shù)點(diǎn)后兩位數(shù),并對(duì)第三位進(jìn)行四舍五入。

例如:h值為8.32433,則函數(shù)返回8.320000;

h值為8.32533,則函數(shù)返回8.330000。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

float fun(float h)

{

?

}

main()

{

float a;

printf("Enter a: "); scanf( "%f", &a);

printf("The original data is : ");

printf("%f \n\n", a);

printf("The result : %f\n", fun(a));

}

【答案】

float fun(float h)

{

long w ;

w=h*100+0.5;

return(float)w/100;

}

1.程序填空題

給定程序的功能是將十進(jìn)制正整數(shù)m轉(zhuǎn)換成k進(jìn)制(2≤k≤9)數(shù)的數(shù)字輸出。例如,輸入8和2,則應(yīng)輸出1000(即十進(jìn)制數(shù)8轉(zhuǎn)換成二進(jìn)制表示是1000)。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

void fun(int m,int k)

{

int aa[20],I;

for(i=0;m;i++)

{

/**********found**********/

aa[i]=___1___;

/**********found**********/

___2___;

}

for( ; I; i--)

/**********found**********/

printf("%d",___3___);

}

main()

{

int b,n;

printf("Please enter a number and a base:\n" );

scanf("%d %d",&n,&b);

fun(n, b);

}

【答案】

(1)m%k (2)m /= k (3)aa[ i-1 ]

2.程序修改題

在給定程序中,函數(shù)fun的功能是:計(jì)算s所指字符串中含有t所指字符串的數(shù)目,并作為函數(shù)值返回。

請(qǐng)改正函數(shù)fun中指定部位的錯(cuò)誤,使其能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

#include <string.h>

#define N 80

int fun(char *s,char *t)

{

int n;

char *p,*r;

n=0;

while(*s)

{ p=s;

/*********found**********/

R=t;

while(*r)

if(*r==*p) { r++; p++; }

else break;

/*********found**********/

if(*r=0)

n++;

s++;

}

return n;

}

main()

{

char a[N],b[N];

int m;

printf("Please enter string a : ");

gets(a);

printf("Please enter substring b : ");

gets(b);

m=fun(a,b);

printf("The result is : m=%d\n",m);

}

【答案】

(1)將R=t; 改為:r=t;

(2)將if(*r=0) 改為:if(*r==0)

3.程序設(shè)計(jì)題

請(qǐng)編寫函數(shù)fun,函數(shù)的功能是:移動(dòng)一維數(shù)組中的內(nèi)容;若數(shù)組中有n個(gè)整數(shù),要求把下標(biāo)從0到p(含p,p小于等于n-1)的數(shù)組元素平移到數(shù)組的最后。

例如,一維數(shù)組中的原始內(nèi)容為:1,2,3,4,5,6,7,8,9,10;p的值為3。移動(dòng)后,一維數(shù)組中的內(nèi)容應(yīng)為:5,6,7,8,9,10,1,2,3,4。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <stdio.h>

#define N 80

void fun(int *w,int p,int n)

{

?

}

main()

{

int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,

13,14,15};

int I,p,n=15;

printf("The original data:\n");

for(i=0; i<n; i++) printf("%3d",a[i]);

printf("Enter p: "); scanf("%d",&p);

fun(a,p,n);

printf("The data after moving:\n");

for(i=0;i<n;i++) printf("%3d",a[i]);

printf("\n\n");

}

【答案】

void fun(int *w,int p,int n)

{

int i,j=0,b[N];

for(i=p+1; i<n; i++) b[j++]=w[i];

for(i=0; i<=p; i++) b[j++]=w[i];

for(i=0; i<n; i++) w[i]=b[i];

}

1.程序填空題

給定程序中,函數(shù)fun的功能是:將形參student所指結(jié)構(gòu)體數(shù)組中年齡最小者的數(shù)據(jù)作為函數(shù)值返回,并在main函數(shù)中輸出。

請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。

注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <stdio.h>

typedef struct

{

char name[10];

int age;

}STUDENT;

STUDENT fun(STUDENT student[],int n)

{

STUDENT min;

int I;

min=___1___;

for(i=1;i<n;i++)

if(min.age>___2___)

min=student[i];

return min;

}

main()

{

STUDENT student[3]={"alice",17,"baren",16,"caren",18};

STUDENT min;

min=fun(student,3);

printf("\nThe min age result \n");

printf("\nName : %s,Age : %d\n",___3___,min.age);

}

【答案】

(1)*student (2)student[i].age (3)min.name?

2.程序修改題

在給定程序中,函數(shù)的功能是:讀入一個(gè)整數(shù)k(2≤k≤10000),打印它的所有質(zhì)因子(即所有為素?cái)?shù)的因子)。例如,若輸入整數(shù)2310,則應(yīng)輸出:2、3、5、7、11。

請(qǐng)改正程序中的錯(cuò)誤,使程序能得出正確的結(jié)果。

注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)!

#include <conio.h>

#include <stdio.h>

#include <windows.h>

/***********found**********/

IsPrime(int n);

{

int I,m;

m=1;

for(i=2; i<n; i++)

/***********found**********/

if!(n%i)

{ m=0; break; }

return(m);

}

main()

{

int j,k;

system("cls");

printf("Please enter an integer number

between 2and 10000: ");

scanf("%d",&k);

printf("\n\nThe prime factor(s) of

%dis(are): ",k);

for(j=2;j<=k;j++)

if((!(k%j))&&(IsPrime(j)))

printf("\n%4d",j);

printf("\n");

}

【答案】

(1)將IsPrime(int n); 改為 IsPrime(int n)

(2)將if!(n%i) 改為 if(!(n%i))

3.程序設(shè)計(jì)題

請(qǐng)編寫一個(gè)函數(shù)fun,其功能是:將兩個(gè)字符串連接(不得使用庫函數(shù)strcat),即把s2所指的字符串連接到s1所指字符串后。例如,分別輸入Firststring和Secondstring則程序輸出FirststringSecondstring。

注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。

#include <conio.h>

#include <stdio.h>

#include <windows.h>

void fun(char s1[],char s2[])

{

int I,n=strlen(s1),m=strlen(s2);

for(I=0;I<m;I++)

s1[n+I]=s2[I];

}

main()

{

char p1[80],p2[80];

system("cls");

printf("enter p1 and p2 :\n");

scanf("%s%s",p1, p2);

printf("p1=%s\n",p1);

printf("p2=%s\n",p2);

printf("invoke fun(p1,p2):\n");

fun(p1,p2);

printf("after invoking :\n");

printf("%s\n",p1);

}

【答案】

void fun(char s1[],char s2[])

{

int i,n=strlen(s1),m=strlen(s2);

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

s1[n+i]=s2[i];

}


全國計(jì)算機(jī)等級(jí)考試二級(jí)C語言練習(xí)題(十三)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
红安县| 横峰县| 开阳县| 庄浪县| 二连浩特市| 白朗县| 出国| 武威市| 泉州市| 莆田市| 洪江市| 桂平市| 蒙自县| 灵寿县| 固阳县| 乳源| 芦溪县| 柏乡县| 马公市| 江孜县| 重庆市| 南澳县| 浑源县| 南乐县| 和静县| 陇川县| 平昌县| 巴彦县| 合作市| 田东县| 奉节县| 德兴市| 弥渡县| 绥棱县| 河间市| 自治县| 遵化市| 林口县| 南安市| 秦安县| 信丰县|