1、P115 第7章 数组参考答案
四、编程题
1. 参考程序:
#include 2、m+a[i];
ave=sum/n;
printf("pinjunfenwei:%f\n",ave);
for(i=0;i 3、e input NO %d:”, i+1);
scanf(“%d”, &a[i]);
}
for (i = 0 ; i <= N/2 -1; i++)
{
temp = a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
for (i = 0 ; i < N; i++)
printf(“%3d”, a[i]);
}
3.打印杨辉三角前十行参考程序:
#include 4、 i < N; i++)
{
a[i][i] = 1;
a[i][0] = 1;
}
for (i = 1 ; i < N; i++)
for (j = 1 ; j <= i; j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for (i = 0 ; i < N; i++)
{
for (j = 0 ; j <= i; j++)
printf("%5d", a[i][j]);
printf("\n");
}
}
P123 第8章 函数参考答案
四、编程题
1. 参考程序:判断 5、素数
#include 6、"%d is prime!", x);
else
printf("%d is not prime!", x);
}
2、将数字字符转换成整数:
#include 7、tput number:");
printf("%ld\n", data);
}
long fun(char str[])
{
int i;
long data;
data=0;
for(i=0;str[i]!='\0';i++)
data=data*10+(str[i]-'0');
return data;
}
P140 第九章 编译预处理参考答案
P142 第十章 指针参考答案
四、编程题
1. void swap(int *p1,int *p2)
{in 8、t temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
main()
int a,b,*p1=&a,*p2=&b;
sacnf(“%d%d”,p1,p2);
swap(p1,p2)
printf(“%d,%d”,a,b);
}
2.main()
{float he(float ,float);
float cha(float ,float);
float ji(float ,float);
float shang(float ,float);
void process(float ,float ,float 9、 ( *fun)(float ,float));
float a,b;
scanf(“%f,%f”,&a,&b);
printf(“he=”);
process(a,b,he);
printf(“cha=”);
process(a,b,cha);
printf(“ji=”);
process(a,b,ji);
printf(“shang=”);
process(a,b,shang);
}
float he(float x,float y)
{return (x+y);}
float cha(float 10、 x,float y)
{return (x-y);}
float ji(float x,float y)
{return (x*y);}
float shang(float x,float y)
{return (x/y);}
void process(float x,float y,float (*fun)(float ,float))
{float result;
result=(*fun)(x,y);
printf(“%f\n”,result);
}
3. int fun(char *p)
{char *f;
f=p;
while(*p!='\0')
11、
p++;
return p-f;
}
main()
{char s[100],*p;
scanf("%s",s);
p=s;
printf("%d\n",fun(p));
}
P153 第11章 结构体与共用体参考答案
四、编程题
1. 参考程序:
#include 12、);
void output(struct student w[], int n);
main()
{
struct student st[N];
int n;
printf("Please input the number of worker(<10):");
scanf("%d", &n);
input(st, n);
output(st, n);
}
void input(struct student w[], int n)
{
int i, j;
float temp;
for(i=0;i 13、 14、p;
}
}
}
void output(struct student w[], int n)
{
int i, j;
for(i=0;i 15、
printf("%g ", w[i].score[j]);
}
}
P164 第13章 文件参考答案
三、编程题
1. 参考程序:字符串打印
#include 16、);
printf(“%s”, str);
}
fclose(fp);
}
2、参考程序:
#include 17、t(0);
}
if ((fp2=fopen(argv[2],”a”))==NULL)
{
printf(“Open argument2 file error!”);
exit(0);
}
while (!feof(fp1))
fputc(fgetc(fp1),fp2);
fclose(fp1);
fclose(fp2);
}
3、将指定的文本文件中的小写字母改为大写字母。
#include 18、ong pos;
printf("Please input filename:");
gets(filename);
if ((fp=fopen(filename,"r+"))==NULL)
{
printf("Open error!");
exit(0);
}
pos=0;
while (!feof(fp))
{
fseek(fp,pos,0);
ch=fgetc(fp);
if (ch>='a' && ch<='z')
{
ch = ch -32;
fseek(fp,pos,0);
fputc 19、ch,fp);
}
pos++;
}
fclose(fp);
}
4、将100~200之间的素数保存到A盘根目录的“test.txt”文本文件中。(每行存5个数,每个数占10列)
include 20、 i = 100;i<=200; i++)
{
for(j = 2; j <= i-1; j++)
if (i % j == 0)
break;
if (j == i )
{
count++;
fprintf(fp,"%10d", i);
if (count % 5 ==0 )
fprintf(fp,"%c", '\n');
}
}
fclose(fp);
}
5、将平均成绩不及格的学生数据保存到A盘的根目录的文件“stud.dat”中。
#include 21、 50
struct student
{
0 long num;
char name[20];
float score[3];
float average;
};
void input(struct student w[], int n);
void save(struct student w[], int n);
main()
{
struct student st[N];
int n;
printf("Please input the number of worker(<10 22、):");
scanf("%d", &n);
input(st, n);
save(st, n);
}
void input(struct student w[], int n)
{
int i, j;
float temp;
for(i=0;i 23、rintf("name:");
gets(w[i].name);
w[i].average = 0;
for (j=0; j < 3; j++)
{
printf("Score[%d]=", j);
scanf("%f",& temp );
w[i].score[j] = temp;
w[i].average= w[i].average+ w[i].score[j];
}
w[i].average = w[i].average / 3. 24、0;
}
}
void save(struct student w[], int n)
{
FILE *fp;
int i;
if ((fp=fopen("a:\\stud.dat","wb"))==NULL)
{
printf("Open error!");
exit(0);
}
for(i=0;i






