2016最新二级C语言考试题库及答案(程序改错专项练习 精华版)
- PAGE 1 -
程序改错题(共15题)
1、在考生文件夹下,给定程序MODI.C的功能是:
求一维数组a中所有元素的平均值,结果保留两位小数。
例如,当一维数组a中的元素为:10,4,2,7,3,12,5,34,5,9
程序的输出应为:The aver is: 9.10 。
#include
#include
void main()
{
int a[10]={10,4,2,7,3,12,5,34,5,9},i;
float aver,s; /************found************/
float aver,s;
int aver,s;
/************found************/
s=a[0]; s = 0;
s=a[0];
for ( i=1; i<10; i++)
s += a[i];
aver = s / i;
printf("The aver is: %.2f\n", aver);
}
2、在考生文件夹下,给定程序MODI.C的功能是:
求二维数组a中的最大值和最小值。
例如,当二维数组a中的元素为:
4 4 34
37 3 12
5 6 5
程序的输出应为:The max is: 37
The min is: 3 。
#include
#include
void main()
{
int a[3][3]={4,4,34,37,3,12,5,6,5},i,j,max,min;
max = min = a[0][0];
for ( i=0; i<3; i++)
for(j=0;j<3;j++) /************found************/
for(j=0;j<3;j++)
for ( j=1; j<3; j++)
{ if ( max < a[i][j] )
max = a[i][j];
/************found************/
if(min>a[i][j]) if (min < a[i][j])
if(min>a[i][j])
min = a[i][j];
}
printf("The max is: %d\n", max);
printf("The min is: %d\n", min);
}
3、在考生文件夹下,给定程序MODI.C的功能是:
求一维数组a中的最大元素及其下标。
例如,当一维数组a中的元素为:1,4,2,7,3,12,5,34,5,9,
程序的输出应为:The max is: 34,pos is: 7 。
#include
#include
void main()
{
int a[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;
max = a[0];
pos = 0;
for ( i=1; i<10; i++)
/************found************/
if(max a[i])
if(max
{
max = a[i];
/************found************/
pos=i;i = pos;
pos=i;
}
printf("The max is: %d ,pos is: %d\n", max , pos);
}
4、在考生文件夹下,给定程序MODI.C的功能是:
求二维数组a中的最小值。
例如,当二维数组a中的元素为:
4 2 34
7 3 12
5 6 5
程序的输出应为:The min is: 2 。
#include
#include
void main()
{
int a[3][3]={4,2,34,7,3,12,5,6,5},i,j,min;
min = a[0][0];
for(i=0;i<3;i++) /************found**********