This question requires the realization of a function to calculate the sum of all odd numbers in N integers, and at the same time to realize a function for judging the parity.
Function interface definition:
int even( int n );
int OddSum( int List[], int N );
The function even returns the corresponding value according to the parity of the parameter n passed in by the user: when n is an even number, it returns 1; otherwise, it returns 0. The function OddSum is responsible for calculating and returning the sum of all odd numbers in the passed N integer List[].
Sample referee test procedure:
#include <stdio.h>
#define MAXN 10
int even( int n );
int OddSum( int List[], int N );
int main()
{
int List[MAXN], N, i;
scanf("%d", &N);
printf("Sum of ( ");
for ( i=0; i<N; i++ ) {
scanf("%d", &List[i]);
if ( even(List[i])==0 )
printf("%d ", List[i]);
}
printf(") = %d\n", OddSum(List, N));
return 0;
}
/* 你的代码将被嵌在这里 */
Input sample:
6
2 -3 7 88 0 15
Sample output:
Sum of ( -3 7 15 ) = 19
answer:
int even( int n )
{
//当n为偶数时返回1,否则返回0。
if(n%2==0) return 1;
else return 0;
}
int OddSum( int List[], int N )
{
//计算并返回传入的N个整数List[]中所有奇数的和。
int sum=0.0;
int i;
for (i = 0; i < N;i++)
{
if (List[i] % 2 != 0) sum += List[i];
}
return sum;
}