pta Exercise 5-2 Use functions to find odd sums

Topic Collection of Zhejiang University Edition "C Language Programming (3rd Edition)"

Exercise 5-2 Use functions to find odd sums (15 points)

This question requires the realization of a function to calculate the sum of all odd numbers in N integers, and to realize a function to judge 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

Code:

int even( int n )
{
    
    
    if (n%2==0)
    {
    
    
        return 1;
    }
    else
    {
    
    
        return 0;}
    }
int OddSum( int List[], int N )
{
    
    
    int i;
    int sum=0 ;
    for (i=0; i<N; i++)
    {
    
    
        if (even(List[i])==0)
        {
    
    
            sum+=List [i];
        }
    }
    return sum;
}

Guess you like

Origin blog.csdn.net/crraxx/article/details/109230918