改錯題
1、在考生文件夾下,給定程序MODI.C的功能是:
從低位開始取出長整型變量s中奇數位上的數,依次構成一個新數放在t中。例如,當s中的數為:7654321時,t中的數為:7531。
請修改并運行該程序,然后將源程序文件MODI.C上傳。
#include
#include
main( )
{ long s, t, sl=10;
clrscr();
printf("\nPlease enter s:");
scanf("%ld", &s);
/************found************/
t = s / 10; s%10 改為 t=s%10;
while ( s > 0)
{ s = s/100;
t = s%10 * sl + t;
/************found************/
sl = sl*100 ;改為 S1=S1*10;
}
printf("The result is: %ld\n", t);
}
2、在考生文件夾下,給定程序MODI.C的功能是:
求一維數組a中的值為偶數的元素之和。例如,當一維數組a中的元素為:10,4,2,7,3,12,5,34,5,9 ,
程序的輸出應為:The result is: 62
#include
#include
main()
{ int a[10]={10,4,2,7,3,12,5,34,5,9},i,s;
clrscr();
s = 0;
for ( i=0; i<10; i++)
/************found************/
if (i % 2 == 0) 改為if (a[i] % 2 == 0)
s = s + a[i];
/************found************/
print("The result is: %d\n", s);
}
3、在考生文件夾下,給定程序MODI.C的功能是:
求一維數組a中值為偶數的元素之和。
例如,當一維數組a中的元素為:10,4,2,7,3,12,5,34,5,9 ,
程序的輸出應為:The result is: 62。
#include
#include
sum ( int arr[ ],int n )
{ int i,s;
clrscr();
s = 0;
for ( i=0; i
if (arr[i] % 2 == 0)
/************found************/
s = s + i; 改為s = s + [i];
return (s);
}
main()
{ int a[10]={10,4,2,7,3,12,5,34,5,9},i,s;
/************found************/
s = sum( a ,2 ); 改為s = sum( a ,10);
printf("The result is: %d\n", s);
}
4、在考生文件夾下,給定程序MODI.C的功能是:
求二維數組a中的最大值。
例如,當二維數組a中的元素為:
4 4 34
7 3 12
5 6 5
程序的輸出應為:The max is: 34 。
#include
#include
main()
{ int a[3][3]={4,4,34,7,3,12,5,6,5},i,j,max;
clrscr();
max = a[0][0];
for ( i=0; i<3; i++)
for ( j=0; j<3; j++)
/************found************/
if (max > a[i][j])
{
/************found************/
max == a[i][j];
}
printf("The max is: %d\n", max);
}
5、在考生文件夾下,給定程序MODI.C的功能是:
求一維數組a中的最大元素及其下標。
例如,當一維數組a中的元素為:1,4,2,7,3,12,5,34,5,9,
程序的輸出應為:The max is: 34,p
os is: 7 。
#include
#include
main()
{ int a[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;
clrscr();
max = a[0];