c语言改错题

在考生目录下,给定程序MODI1.C的功能是:
求一维数组a中值为奇数的元素之和。
例如,当一维数组a中的元素为:11,4,2,7,3,12,5,34,5,9
程序的输出应为:The result is: 40。

程序中有两处错误,错误都在提示行:
/***********found***********/的下面一行,请考生注意。

请改正程序中的错误,使它能得出正确的结果。
注意:程序中的其它地方请考生不要随意改动,不得增行
或删行,也不得更改程序的结构!
#include <conio.h>
#include <stdio.h>
main()
{ int arr[10]={11,4,2,7,3,12,5,34,5,9},i;
/************found************/
int s=0;
clrscr();
for ( i=1; i<10; i++)
/************found************/
if (i % 2 == 1)
s = s + arr[i];
printf("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。
请修改并运行该程序,然后将源程序文件MODI.C提交。
#include
sum ( int arr[ ],int n )
{ int i,s;
system(“CLS”);
s = 0;
for ( i=0; i<n; i++)
if (arr[i] % 2 == 0)
/************found************/
//s = s + i;
s = s + arr[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);}
5、在考生文件夹下,给定程序MODI.C的功能是:先将在字符串s中的字符按逆序存放到t串中,然后把s中的字符按正序连接到t串的后面。例如:当s中的字符串为:"ABCDE"时,则t中的字符串应为:"EDCBAABCDE"。请修改并运行该程序,然后将源程序文件MODI.C提交。
#include
#include
main()
{ char s[80], t[80];
/************found************/
// int i;
int i, sl;
system(“CLS”);
printf("\nPlease enter string s:"); scanf("%s", s);
sl = strlen(s);
for (i=0; i<sl; i++)
/************found************/
// t[i] = s[sl-i];
t[i] = s[sl-i-1];
for (i=0; i<sl; i++)
t[sl+i] = s[i];
t[2*sl] =’\0’;
printf("The result is: %s\n", t);}
6、在考生文件夹下,给定程序MODI.C的功能是:输出100~200之间既不能被3整除也不能被7整除的整数并统计这些整数的个数,要求每行输出8个数。请修改并运行该程序,然后将源程序文件MODI.C提交。
#include
#include
#include
#include
main()
{ int i;
/************found************/
//int n;
int n=0;
system(“CLS”);
for(i=100;i<=200;i++)
{
/************found************/
// if(i%3==0&&i%7==0)
if(i%3!=0&&i%7!=0)
{ if(n%8==0) printf("\n");
printf("%6d",i);
n++;
}
}
printf("\nNumbers are: %d\n",n); }
7、在考生文件夹下,给定程序MODI.C的功能是:求一维数组a中所有元素的平均值。
例如,当一维数组a中的元素为:10,4,2,7,3,12,5,34,5,9,6,8程序的输出应为:The everage is: 8.75 。请修改并运行该程序,然后将源程序文件MODI.C提交。
#include
#include
float average( a,n)
/************found************/
//int a,n;
// 把int a, n; 删除
{ int j; float aver;
/************found************/
//float s;
float s=0;
for ( j=0; j<n; j++)
s += a[j];
aver = s / n;
return (aver);}
main()
{ int a[12]={10,4,2,7,3,12,5,34,5,9,6,8};
system(“CLS”);
printf("Theaverageis:%.2f\n",average(a,12));}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-19
/************found************/
if (i % 2 == 1)
s = s + arr[i];
这里错误在于,需求中要求的是求数组中值为奇数,而不是下标为奇数,所以可以改为
if(a[i]%2==1)
s+=arr[i];
上面的那处错误没有发现!
第2个回答  2012-02-16
#include <conio.h>
#include <stdio.h>
main()
{ int arr[10]={11,4,2,7,3,12,5,34,5,9},i;
/************found************/
int s=0;
clrscr();
for ( i=1; i<10; i++) // for ( i=0; i<10; i++),下标从0开始
/************found************/
if (i % 2 == 1) // if (arr[i] % 2 == 1),判断数组内的数字是否为奇数
s = s + arr[i];
printf("The result is: %d\n", s);
}追问

第一个found下面呢

追答

/************found************/
int s=0;
clrscr();
for ( i=1; i<10; i++) // for ( i=0; i<10; i++),下标从0开始

错的是第三行,int s=0;这肯定没有错的

第3个回答  2012-02-17
#include <conio.h>
#include <stdio.h>
main()
{ int arr[10]={11,4,2,7,3,12,5,34,5,9},i;
/************found************/
int s=11;
clrscr();
for ( i=1; i<10; i++)
/************found************/
if (arr[i] % 2 == 1)
s = s + arr[i];
printf("The result is: %d\n", s);
}
改了之后的本回答被网友采纳
第4个回答  2012-02-17
#include <conio.h>
#include <stdio.h>
main()
{ int arr[10]={11,4,2,7,3,12,5,34,5,9},i;
/************found************/
int i;(用了这么久的I,总该先定义一下吧)
int s=0;
clrscr();
for ( i=1; i<10; i++)
/************found************/
if (i % 2 == 1) //if(a[i-1]%2==1)这么改应该没错吧
s = s + arr[i];
printf("The result is: %d\n", s);
}
相似回答