全國(guó)計(jì)算機(jī)等級(jí)考試二級(jí)C語言練習(xí)題(七)
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:輸出一個(gè)3×3的矩陣,要求必須使用行指針表示輸出變量。
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
main()
{
static int array[3][3]={{9,8,7},{6,5,4},{3,2,1}};
int (*p)[3],j,I;
/**********found**********/
p=___1___;
system("cls");
for(i=0;i<3;i++)
{
printf("\n\n");
for(j=0;j<3;j++)
/**********found**********/
printf("%4d",___2___);
}
}
【答案】
(1)array (2)*(*(p+i)+j)?
2.程序修改題
下列給定程序中,函數(shù)fun()的功能是:首先把b所指字符串中的字符按逆序存放,然后將a所指字符串中的字符和b所指字符串中的字符,按排列的順序交叉合并到c所指數(shù)組中,過長(zhǎng)的剩余字符接在c所指數(shù)組的尾部。
例如,當(dāng)a所指字符串中的內(nèi)容為abcdefg,b所指字符串中的內(nèi)容為1234時(shí),c所指數(shù)組中的內(nèi)容應(yīng)該為a4b3c2d1efg;而當(dāng)a所指字符串中的內(nèi)容為1234,b所指字符串中的內(nèi)容為abcdefg時(shí),c所指數(shù)組中的內(nèi)容應(yīng)該為1g2f3e4dcba。
請(qǐng)改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
void fun(char *a,char *b,char *c)
{
int I,j; char ch;
i=0; j=strlen(b)-1;
/**********found**********/
while(i>j)
{
ch=b[i];b[i]=b[j];b[j]=ch;
i++;j--;
}
while(*a||*b)
{
if(*a){*c=*a; c++; a++;}
if(*b){*c=*b; c++; b++;}
}
/**********found**********/
*c=o;
}
main()
{
char s1[100],s2[100],t[200];
system("cls");
printf("Enter s1 string: ");
scanf("%s",s1);
printf("Enter s2 string: ");
scanf("%s",s2);
fun(s1,s2,t);
printf("The result is :%s\n",t);
}
【答案】
(1)將while(i>j) 改為:while(i<j)
(2)將*c=o; 改為:*c='\0';?
3.程序設(shè)計(jì)題
請(qǐng)編寫函數(shù)fun(),該函數(shù)的功能是:實(shí)現(xiàn)B=A+A′,即把矩陣A加上A的轉(zhuǎn)置,存放在矩陣B中。計(jì)算結(jié)果在main()函數(shù)中輸出。
例如,輸入下面矩陣:
1 2 3
4 5 6
7 8 9
其轉(zhuǎn)置矩陣為:
1 4 7
2 5 8
3 6 9
則程序輸出:
2 6 10
6 10 14
10 14 18
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#include <conio.h>
#include <windows.h>
void fun(int a[3][3],int b[3][3])
{
?
}
main()
{
int a[3][3]={{1,2,3},{4,5,6},
{7,8,9}},t[3][3];
int I, j;
system("cls");
fun(a,t);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%7d",t[i][j]);
printf("\n");
}
}?
【答案】
void fun(int a[3][3],int b[3][3])
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=a[i][j]+a[j][i];
}
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:把字符串str1中的非空格字符拷貝到字符串str2中。
例如,若str1=“glad to see you!”,則str2=“gladto seeyou!”。
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <windows.h>
#define N 80
main()
{
static char str1[N]="glad to see you!";
char str2[N];
int i=0,j=0;
system("cls");
printf("\n***** str1*****\n ");
puts(str1);
while(str1[i])
{
/**********found**********/
if(___1___)
str2[j++]=str1[i];
/**********found**********/
___2___;
}
printf("\n***** str2 *****\n ");
for(i=0;i<j;i++)
printf("%c",str2[i]);
}
【答案】
(1)str1[i]!=' ' (2)i++?
2.程序修改題
在給定程序中,函數(shù)fun()的功能是:利用插入排序法對(duì)字符串中的字符按從大到小的順序進(jìn)行排序。插入法的基本方法是:先對(duì)字符串中的頭兩個(gè)元素進(jìn)行排序,然后把第3個(gè)字符插入到前兩個(gè)字符中,插入后前3個(gè)字符依然有序;再把第4個(gè)字符插入到前3個(gè)字符中,待排序的字符串已在主函數(shù)中賦予。
請(qǐng)改正程序中的錯(cuò)誤,使它能得到正確結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <string.h>
#include <stdio.h>
#define N 80
void insert(char *aa)
{
int I,j,n; char ch;
n=strlen(aa);
for(i=1;i<n;i++)
{
ch=aa[i];
j=i-1;
/**********found**********/
while((j>=0)||(ch>aa[j]))
{
aa[j+1]=aa[j];
j--;
}
/**********found**********/
aa[j]=ch;
}
}
main()
{
char a[N]="JRTYDFKLIOPQWEGHMNBVCUASXZ";
int I;
printf("The original string: %s\n",a);
insert(a);
printf("The string after sorting: %s\n",
a);
}
【答案】
(1)將while((j>=0)||(ch>aa[j])) 改為:
while((j>=0)&&(ch>aa[j]))
(2)將aa[j]=ch; 改為:aa[j+1]=ch;?
3.程序設(shè)計(jì)題
請(qǐng)編寫一個(gè)函數(shù)void fun(char orig[],char result[],int flg),其功能是:刪除一個(gè)字符串中指定下標(biāo)的字符。其中,orig指向原字符串,刪除后的字符串存放在result所指的數(shù)組中,flg中存放指定的下標(biāo)。
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun()的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#include <conio.h>
#define NUM 100
void fun(char orig[],char result[],int flg)
{
?
}
main()
{
char s1[NUM],s2[NUM];
int flg;
FILE *out;
printf("Please Input s1:\n");
gets(s1);
printf("Input want to deleted:");
scanf("%d",&flg);
fun(s1,s2,flg);
printf("The result is:%s\n",s2);
fun("test String",s2,9);
out=fopen("outfile.dat","w");
fprintf(out,"%s",s2);
fclose(out);
}
【答案】
int n,m=0;
for(n=0;n<NUM;n++)
if(n!=flg)
{
result[m]=orig[n];
m++;
}
result[m]='\0';
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:輸出一個(gè)N×N矩陣,要求非周邊元素賦值0,周邊元素賦值1。
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <windows.h>
#define N 10
main()
{
int bb[N][N];
int I,j,n;
system("cls");
printf("Input n:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
/**********found**********/
if(___1___)
bb[i][j]=1;
else
/**********found**********/
___2___;
}
printf("\n*** the result ***\n");
for(i=0;i<n;i++)
{
printf(" \n\n");
for(j=0;j<n;j++)
printf("%4d",bb[i][j]);
}
}
【答案】
(1)i==0||i==n-1||j==0||j==n-1
或 i==0||j==0||i==n-1||j==n-1
或 j==0||i==0||j==n-1||i==n-1
(2)bb[i][j]=0
2.程序修改題
給定程序中,函數(shù)fun()的功能是:找出一個(gè)大于給定整數(shù)m且緊隨m的素?cái)?shù),并作為函數(shù)值返回。
請(qǐng)改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <conio.h>
#include <stdio.h>
#include <windows.h>
int fun(int m)
{
int I,k;
for(I=m+1; ;I++)
{ for(k=2;k<I;k++)
/**********found**********/
if(I%k!=0)
break;
/**********found**********/
if(k<I)
return(I);
}
}
main()
{
int n;
system("cls");
printf("Please enter n: ");
scanf("%d",&n);
printf("%d\n",fun(n));
}
【答案】
(1)將if(i%k!=0) 改為:if(i%k==0)
(2)將if(k<i) 改為:if(k>=i)?
3.程序設(shè)計(jì)題
已知學(xué)生的記錄由學(xué)號(hào)和學(xué)習(xí)成績(jī)構(gòu)成,N名學(xué)生的數(shù)據(jù)已存入a結(jié)構(gòu)體數(shù)組中。請(qǐng)編寫函數(shù)fun(),該函數(shù)的功能是:找出成績(jī)最高的學(xué)生記錄,通過形參返回主函數(shù)(規(guī)定只有一個(gè)最高分)。
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#define N 10
typedef struct ss /*定義結(jié)構(gòu)體*/
{ char num[10];
int s;
}STU;
fun(STU a[],STU *s)
{
?
}
main()
{ STU a[N]={{"A01",81},{"A02",89},
{"A03",66},{"A04",87},{"A05",77},
{"A06",90},{"A07",79},{"A08",61},
{"A09",80},{ "A10",71}},m;
int I;
system("cls");
printf("*****The original data*****");
for(i=0;i<N;i++)
printf("No=%sMark=%d\n",a[i].num,
a[i].s);
fun(a,&m);
printf("***THE RESULT***\n");
printf("The top :%s, %d\n",m.num,m.s);
}
【答案】
fun(STU a[],STU *s)
{
int i;
*s=a[0];
for(i=0;i<N;i++)/*找出成績(jī)最高的學(xué)生記錄*/
if(s->s<a[i].s)
*s=a[i];
}
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:把一個(gè)整數(shù)插入到一個(gè)已經(jīng)按從小到大排序的數(shù)組中。插入后,數(shù)組仍然有序。
例如,在數(shù)組bb[N]={12,23,31,44,51,63,71,79,85,95}中插入93,結(jié)果為bb[N]={11,21,31,41,51,61,71,79,81,93,95}
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <windows.h>
#define N 10
main()
{
int I,j;
int n;
int bb[N+1]={12,23,31,44,51,63,71,79,85,95};
system("cls");
printf("Input n \n");
scanf("%d",&n);
printf("\nn=%d ",n);
printf("\n*** original list ***\n");
for(i=0;i<N;i++)
printf("%4d ",bb[i]);
for(i=0;i<N;i++)
{
if(n<=bb[i])
{
/**********found**********/
for(j=N;___1___;j--)
/**********found**********/
___2___;
bb[j]=n;
/**********found**********/
___3___;
}
}
if(i==N)
bb[i]=n;
printf("\n*** new list ***\n");
for(i=0;i<N+1;i++)
printf("%4d ",bb[i]);
}
【答案】
(1)j>i (2)bb[j]=bb[j-1] (3)break
2.程序修改題
下列給定程序中,函數(shù)fun()的功能是:根據(jù)整型參數(shù)m,計(jì)算如下公式的值:
y=1/(100×100)+1/(200×200)+1/(300×300)+…
+1/(m×m)
例如,若m=2000,則應(yīng)輸出0.000160。
請(qǐng)改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <conio.h>
#include <stdio.h>
#include <windows.h>
/**********found**********/
fun(int m)
{ double y=0,d;
int I;
/**********found**********/
for(I=100,I<=m,I+=100)
{
d=(double)I*(double)I;
y+=1.0/d;
}
return(y);
}
main()
{
int n=2000;
system("cls");
printf("The result is %1f\n",fun(n));
}
【答案】
(1)將fun(int m) 改為:double fun(int m)
(2)將for(i=100,i<=m,i+=100) 改為:
for(i=100;i<=m;i+=100)?
3.程序設(shè)計(jì)題
請(qǐng)編寫函數(shù)fun(),其功能是:將所有大于1小于整數(shù)m的非素?cái)?shù)存入xx所指數(shù)組中,非素?cái)?shù)的個(gè)數(shù)通過k傳回。
例如,輸入17,則應(yīng)輸出4 6 8 9 10 12 14 15 16。
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <conio.h>
#include <stdio.h>
#include <windows.h>
void fun(int m,int *k,int xx[])
{
?
}
main()
{
int m,n,zz[100];
system("cls");
printf("Please enter an integer number
between 10 and 100: ");
scanf("%d",&n);
fun(n,&m,zz);
printf("There are %d non-prime numbers
less than %d: ",m,n);
for(n=0;n<m;n++)
printf("\n %4d",zz[n]);
}
【答案】
void fun(int m,int *k,int xx[])
{
int i,j,n=0;
for(i=2;i<m;i++)
{
for(j=2;j<i;j++)
if(i%j==0) break;
if(j<i) xx[n++]=i;
}
*k=n;
}
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:把一個(gè)二維字符數(shù)組每行字符串最大的字符拷貝到字符數(shù)組s中。
例如,如果str[3]={"efjh","efknls","owys"},則s="jsy"。
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <windows.h>
main()
{
int i=0;
char *str[3]={"efjh","efknls","owys"};
char **p;
char s[8];
system("cls");
/**********found**********/
___1___;
for(i=0;i<3;i++)
{
s[i]=*p[i];
while(*p[i])
{
if(s[i]<*p[i])
s[i]=*p[i];
/**********found**********/
___2___;
}
}
/**********found**********/
___3___;
printf(" new string \n");
puts(s);
}
【答案】
(1)p=str (2)p[i]++ (3)s[i]='\0'
2.程序修改題
下列給定程序中,函數(shù)fun()的功能是:將字符串p中所有字符復(fù)制到字符串b中,要求每復(fù)制3個(gè)字符之后插入一個(gè)空格。例如,在調(diào)用fun()函數(shù)之前給字符串a(chǎn)輸入ABCDEFGHIJK,調(diào)用函數(shù)之后,字符串b中的內(nèi)容則為ABC DEF GHI JK。
請(qǐng)改正程序中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <stdio.h>
void fun(char *p,char *b)
{
int I,k=0;
while(*p)
/**********found**********/
{ i=1;
/**********found**********/
while(i<3||*p)
{
b[k]=*p;
k++;p++;i++;
}
if(*p)
/**********found**********/
{ b[k]=' '; }
}
b[k]='\0';
}
main()
{
char a[80],b[80];
printf("Enter a string: "); gets(a);
printf("The original string: ");
puts(a);
fun(a,b);
printf("The string after insert space:");
puts(b); printf("\n\n ");
}
【答案】
(1)將i=1; 改為:i=0;
(2)將while(i<3||*p) 改為:while(i<3&&*p)
(3)將b[k]= ' '; 改為:b[k++]=' ';?
3.程序設(shè)計(jì)題
學(xué)生的記錄由學(xué)號(hào)和成績(jī)組成,N名學(xué)生的數(shù)據(jù)已在主函數(shù)中放入結(jié)構(gòu)體數(shù)組s中,請(qǐng)編寫函數(shù)fun(),它的功能是:按分?jǐn)?shù)的高低排列學(xué)生的記錄,高分在前。
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <stdio.h>
#define N 16
typedef struct
{ char num[10];
int s ;
}STREC;
int fun(STREC a[])
{
?
}
main()
{
STREC s[N]={{"GA005",85},{"GA003",76},
{"GA002",69},{"GA004",85},{"GA001",91},
{"GA007",72},{"GA008",64},{"GA006",87},
{"GA015",85},{"GA013",91},{"GA012",64},
{"GA011",66},{"GA017",64},{"GA018",64},
{"GA016",72}};
int I;
FILE *out;
fun(s);
printf("The data after sorted :\n");
for(i=0;i<N;i++)
{
if((i)%4==0) /*每行輸出4個(gè)學(xué)生記錄*/
printf("\n");
printf("%s %4d",s[i].num,s[i].s);
}
printf("\n");
out=fopen("out47.dat", "w");
for(i=0;i<N;i++);
{
if((i)%4==0&&i)
fprintf(out,"\n");
fprintf(out,"%4d",s[i].s);
}
fprintf(out,"\n");
fclose(out);
}
【答案】
int fun(STREC a[])
{
int i,j;
STREC t;
for(i=1;i<N;i++)
for(j=0;j<N-1;j++)
if(a[j].s<a[j+1].s)
{ t=a[j]; a[j]=a[j+1]; a[j+1]=t; }
}
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:從鍵盤輸入若干字符放到一個(gè)字符數(shù)組中,當(dāng)按回車鍵時(shí)結(jié)束輸入,最后輸出這個(gè)字符數(shù)組中的所有字符。
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <ctype.h>
#include <windows.h>
main()
{
int i=0;
char s[81];
char *p=s;
system("cls");
printf("Input a string \n");
for(i=0;i<80;i++)
{
s[i]=getchar();
if(s[i]=='\n')
/**********found**********/
___1___;
}
/**********found**********/
s[i]=___2___;
printf("display the string \n");
while(*p)
/**********found**********/
putchar(___3___);
}
【答案】
(1)break (2)′\0′ (3)*p++?
2.程序修改題
在給定程序中,函數(shù)fun()的功能是:輸入的兩個(gè)數(shù)中較小的數(shù)。
例如:輸入5、10,結(jié)果為min is 5。
請(qǐng)改正fun()程序中的錯(cuò)誤,使其能得出正確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <stdio.h>
#include <conio.h>
/**********found**********/
int fun(int x,y)
{
int z;
z=x<y?x:y;
return(z);
}
main()
{
int a,b,c;
scanf("%d,%d\n",&a,&b);
c=fun(a,b);
printf("min is %d",c);
}
【答案】
將int fun(int x, y) 改為:
int fun(int x,int y)
3.程序設(shè)計(jì)題
編寫函數(shù)fun,其功能是統(tǒng)計(jì)一個(gè)長(zhǎng)度為2的字符串在另一個(gè)字符串中出現(xiàn)的次數(shù)。例如,假定輸入的字符串為asdasasdfgasdaszx67asdmklo,子字符串為as,則應(yīng)當(dāng)輸出6。
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
int fun(char *str,char *substr)
{
?
}
main()
{
char str[81],substr[3];
int n;
system("cls");
printf("輸入主字符串: ");
gets(str);
printf("輸入子字符串: ");
gets(substr);
puts(str);
puts(substr);
n=fun(str,substr);
printf("n=%d\n ",n);
}
【答案】
int fun(char *str, char *substr)
{
int i,j=0;
for(i=0;str[i+1]!='\0';i++)
if(str[i]==substr[0]&&
str[i+1]==substr[1])
j++;
return j;
}
1.程序填空題
請(qǐng)補(bǔ)充main函數(shù),該函數(shù)的功能是:從鍵盤輸入兩上字符串并分別保存在字符數(shù)組str1和str2中,用字符串str2替換字符串str1前面的所有字符。注意str2的長(zhǎng)度不大于str1,否則需要重新輸入。
例如,如果輸入str1=“abced”,str2=“fk”,則輸出“fkced”。
請(qǐng)?jiān)诔绦虻南聞澗€處填入正確的內(nèi)容并把下劃線刪除,使程序得出正確的結(jié)果。
注意:不得增行或刪行,也不得更改程序的結(jié)構(gòu)!
#include <stdio.h>
#include <string.h>
#include <windows.h>
main()
{
char str1[81],str2[81];
char *p1=str1,*p2=str2;
system("cls");
do
{
printf("Input str1 \n");
gets(str1);
printf("Input str2 \n");
gets(str2);
/**********found**********/
}while(___1___ );
/**********found**********/
while(___2___)
*p1++=*p2++;
printf("Display str1 \n");
/**********found**********/
puts(___3___);
}
【答案】
(1)strlen(str1)<strlen(str2) (2)*p2 (3)str1
2.程序修改題
下列給定程序中,函數(shù)fun()的功能是:應(yīng)用遞歸算法求某數(shù)a的平方根。求平方根的迭代公式如下:
?
例如,2的平方根為1.414214。
請(qǐng)改正程序中的錯(cuò)誤,使其能得出正
Chapter_5
確的結(jié)果。
注意:不要改動(dòng)main函數(shù),不得增行或刪行,也不得更改程序的結(jié)構(gòu)。
#include <math.h>
#include <stdio.h>
/**********found**********/
fun(double a,double x0)
{
double x1,y;
x1=(x0+a/x0)/2.0;
/**********found**********/
if(fabs(x1-x0)>0.00001)
y=fun(a,x1);
else y=x1;
return y;
}
main()
{
double x;
printf("Enter x: "); scanf("%lf",&x);
printf("The square root of %lf is %1f\n",
x, fun(x,1.0));
}
【答案】
(1)將 fun(double a,double x0) 改為:
double fun(double a,double x0)
(2)將 if(fabs(x1-x0)>0.00001) 改為:
if(fabs(x1-x0)>=0.00001)
3.程序設(shè)計(jì)題
下列程序定義了N×N的二維數(shù)組,并在主函數(shù)中自動(dòng)賦值。請(qǐng)編寫函數(shù)fun(),功能為:使數(shù)字右上半三角元素中的值乘以m。
例如,若m的值為2,a數(shù)組中的值為
a=1 9
2 7
則返回主程序后a數(shù)組的值應(yīng)為
2 18
2 14
注意:部分源程序已給出。請(qǐng)勿改動(dòng)主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號(hào)中填入所編寫的若干語句。
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define N 5
int fun(int a[][N], int m)
{
?
}
main()
{
int a[N][N],m, I, j;
system("cls");
printf("*****The array*****\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
a[i][j]=rand()%20;
printf("%4d",a[i][j]);
}
printf("\n");
}
do
m=rand()%10;
while(m>=3); /*產(chǎn)生一個(gè)小于3的隨機(jī)數(shù)*/
printf("m=%4d\n",m);
fun(a,m);
printf("THE RESULT\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
【答案】
int fun(int a[][N], int m)
{
int i,j;
for(i=0;i<N;i++)
for(j=i;j<N;j++)
a[i][j]=a[i][j]*m;
}