资源描述
实验六:参考答案
1、用数组实现输入任意十个整数,用擂台赛法求其最大值和最小值。(C语言源程序文件名为61.c,程序运行结果文件名为61.txt)
【参考程序】
#include<stdio.h>
void main()
{int i,max,min,a[10];
FILE *fp;
fp=fopen("e:\\6\\61.txt","w");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
max=min=a[0];
for(i=1;i<10;i++)
{if(a[i]>max) max=a[i];
if(a[i]<min) min=a[i];}
fprintf(fp,"max=%d,min=%d\n",max,min);
fclose(fp);
}
4、有一数组中的数为67,45,43,35,32,30,28,25,24,10,要求用折半查找的方法找出数组中24所在的位置。(C语言源程序文件名为62.c,程序运行结果文件名为62.txt)
【参考程序】
#include<stdio.h>
void main()
{static int a[10]={67,45,43,35,32,30,28,25,24,10};
int top,bot,mid,x;
FILE *fp;clrscr();
fp=fopen("e:\\6\\62.txt","w");
top=0;bot=9;
注意:当数组元素是降序排列时,top和bot的变换
scanf("%d",&x);
mid=(top+bot)/2;
while(x!=a[mid]&&top<=bot)
{if(x<a[mid]) top=mid+1;
else bot=mid-1;
mid=(top+bot)/2;
}
if(x==a[mid]) fprintf(fp,"%d,%d\n",a[mid],mid);
else fprintf(fp,"can not be found\n");
fclose(fp);
}
3、有一数组中的数为56,23,-5,4,120,333,21,-40,2,10,要求编程实现数组中的数据实现前半段和后半段平移,即数组中的数变为333,21,-40,2,10,56,23,-5,4,120。(要求将原数组和平移后的数组输出到文件中)(C语言源程序文件名为63.c,程序运行结果文件名为63.txt)
【参考程序】
#define N 10
#include<stdio.h>
main()
{static int a[N]={56,23,-5,4,120,333,21,-40,2,10},i,t;
FILE *fp;clrscr();
fp=fopen("e:\\6\\63.txt","w");
当数组中的数为单数时,中间一个数保持位置不变
for(i=0;i<N;i++)
fprintf(fp,"%d ",a[i]);
fprintf(fp,"\n");
for(i=0;i<N/2;i++)
{t=a[i];a[i]=a[(N+1)/2+i];a[(N+1)/2+i]=t;}
for(i=0;i<N;i++)
fprintf(fp,"%d ",a[i]);
fprintf(fp,"\n");
fclose(fp);
}
4、输入若干有序数(要求降序,比如67,45,43,35,32,28,25,24,10)放在数组中。然后输入一个数(比如30),插入到此有序数列中,插入后,数组中的数仍然有序。要求将原数组和插入后的数组输出到文件中)(C语言源程序文件名为64.c,程序运行结果文件名为64.txt)
【参考程序】
#include<stdio.h>
main()
{int a[10]={67,45,43,35,32,28,25,24,10};
int i,x,p,n;
FILE *fp;
此for循环输出原数组
scanf("%d",&x);
fp=fopen("e:\\6\\64.txt","w");
p=0,n=8;
for(i=0;i<=n;i++)
此while循环查找插入位置
fprintf(fp,"%d ",a[i]);
fprintf(fp,"\n");
while(x<a[p]&&p<=n)
此for循环挪数据,将插入位置空出来
p++;
for(i=n;i>=p;i--)
a[i+1]=a[i];
此赋值语句实现数据的插入
a[p]=x;
n++;
for(i=0;i<=n;i++)
此for循环实现插入数据后的数组输出
fprintf(fp,"%d ",a[i]);
fclose(fp);
}
5、有一数组中的数为56,23,-5,4,120,333,21,-40,2,10,要求编程将数组中数56删除,即数组中的数变为23,-5,4,120,333,21,-40,2,10。(要求将原数组和删除后的数组输出到文件中)(C语言源程序文件名为65.c,程序运行结果文件名为65.txt)
【参考程序】
#include<stdio.h>
main()
{static int a[10]={56,23,-5,4,120,333,21,-40,2,10},i,n,p,x;
FILE *fp;
n=9;
fp=fopen("e:\\6\\65.txt","w");
for(i=0;i<=n;i++)
fprintf(fp,"%d ",a[i]);
fprintf(fp,"\n");
scanf("%d",&x);
p=0;
while(x!=a[p]&&p<=n)
p++;
for(i=p;i<n;i++)
a[i]=a[i+1];
n--;
for(i=0;i<=n;i++)
fprintf(fp,"%d ",a[i]);
fprintf(fp,"\n");
fclose(fp);
}
四、附加题:
6、把a数组中的5个数和b数组中逆序的5个数一一相减,结果存在c数组中。例如:当a数组中的值是:1、3、5、7、8,b数组中的值是:2、3、4、5、8,程序运行后,c数组中存放的数据是-7、-2、1、4、6。 最后将三个数组分别输出到文件中。(C语言源程序文件名为66.c,程序运行结果文件名为66.txt)
【参考程序】
#include<stdio.h>
main()
{FILE *fp;
int a[]={1,3,5,7,8},b[]={2,3,4,5,8},c[5];
int i;
fp=fopen("e:\\6\\66.txt","w");
for(i=0;i<5;i++)
c[i]=a[i]-b[5-1-i];
for(i=0;i<5;i++)
fprintf(fp,"%d ",c[i]);
fclose(fp);
}
7、将9个人员的考试成绩进行分段统计,考试成绩放在a数组中,各分数段的人数存到b数组中:成绩为60到69的人数存到b[0]中,成绩为70到79的人数存到b[1],成绩为80到89的人数存到b[2], 成绩为90到99的人数存到b[3],成绩为100的人数存到b[4], 成绩为60分以下的人数存到b[5]中。(a数组长度定义为9)
例如,当a数组中的数据是:
93、85、77、68、59、43、94、75、98。
程序运行后,b数组中存放的数据应是:
1、2、1、3、0、2。
(C语言源程序文件名为67.c,程序运行结果文件名为67.txt)
【参考程序】
#define N 9
#include<stdio.h>
void main()
{static int a[N]={93,85,77,68,59,43,94,75,98},b[6];
int i;
FILE *fp;
clrscr();
fp=fopen("e:\\6\\67.txt","w");
for(i=0;i<N;i++)
思考,此for循环体可不可以改用switch语句实现,如可以,怎么改?
{if(a[i]==100) b[4]++;
if(a[i]>=90&&a[i]<=99) b[3]++;
if(a[i]>=80&&a[i]<=89) b[2]++;
if(a[i]>=70&&a[i]<=79) b[1]++;
if(a[i]>=60&&a[i]<=69) b[0]++;
if(a[i]<60) b[5]++;
}
for(i=0;i<6;i++)
fprintf(fp,"%4d",b[i]);
fclose(fp);
}
展开阅读全文