文档库

最新最全的文档下载
当前位置:文档库 > 《C语言程序设计》第4节习题答案

《C语言程序设计》第4节习题答案

1、选择题

(1)C(2)D(3)B(4)D(5)C(6)B(7)B(8)C(9)A(10)C(11)C(12)B 2、填空题

(1)①int men[10] ;

②float step[4]={1.9,-2.33,0,20.6};

③_int grid[4][10] ;

(2)①int m[10]={9,4,7,49,32,-5};

②0 9

③scanf("%d",m[1]);

④m[0] =39;

⑤m[0] =a[3]+a[5];

(3)①3 ②5 ③8 ④8 ⑤9 ⑥12 ⑦9

(4)①int a[10]={9,4,12,8,2,10,7,5,1,3}; ②0 9 ③8 8

(5)10

3、程序填空题

(1)

array[20]

20

&array[i ]

20

max=array[i];

min=array[i];

sum+array[i];

sum/20

(2)

-5.770000

2

1

(3)

ABCDEFGHIJKL

EFGHIJKL

ABFDEFGHIJKL

ABFDEFGHI

4、程序设计题

(1)

#define SIZE 100

#include

void main()

{

int a[SIZE+1],i,j;

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

a[i]=i;

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

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

if(a[i]!=0&&a[j]%a[i]==0)

a[j]=0;

printf("\n");

j=0;

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

{

if(a[i]!=0)

{

printf("%-4d",a[i]);

j++;

}

if(j==10)

{

j=0;

printf("\n");

}

}

}

(2)

#define S 14

#include"stdio.h"

void main()

{

int a[S][S],i,j,n;

printf("please enter n:");

scanf("%d",&n);

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

{

a[i][i]=1;

a[i][1]=1;

}

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

for(j=2;j

a[i][j]=a[i-1][j-1]+a[i-1][j];

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

{

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

printf(" ");

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

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

printf("\n");

}

}

(3)

#define SIZE 80

#include

void main()

{

char str1[SIZE+SIZE],str2[SIZE];

int i,j;

puts("Please enter 2 string:");

scanf("%s",str1);

scanf("%s",str2);

i=0;

while(str1[i]!='\0')

i++;

j=0;

while(str2[j]!='\0')

{

str1[i]=str2[j];

i++;

j++;

}

str1[i]='\0';

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

}

(4)

#define SIZE 80

#include

#include

void main()

{

char string[SIZE];

int len,i,j,flag;

long number;

puts("Please enter a digital string:");

scanf("%s",string);

len=strlen(string);

if(string[0]=='-')

{

flag=-1;

i=1;

}

else

{

flag=1;

i=0;

}

number=string[i]-'0';

for(i++;i

number=number*10+(string[i]-'0');

number=flag*number;

printf("%d\n",number);

}

(5)

#define SIZE 40

#include

void main()

{

int m,n,i,j;

float price[SIZE],sum;

printf("\nPlease input M and N:");

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

m=m-n;

printf("Please input %d price :",n);

sum=0;

for(i=0;i

{

scanf("%f",&price[i]);

sum=sum+price[i];

}

printf("Aver are %10.2f.",sum/n);

while(m>0)

{

for(i=0;i

price[i]=price[i+1];

printf("\nPlease input one price:"); scanf("%f",&price[n-1]);

m--;

sum=0;

for(i=0;i

{

sum=sum+price[i];

}

printf("Aber are %10.2f",sum/n);

}

}