Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate which total is larger. You must program it with the following functions/prototypes Functions (reference when needed):
• Fill the array
• Total the odds and evens
• Compare the totals and display the report.

You joined 16mins. ago, created this first post on the forum and you are requesting a whole program to be made by us ? i mean ....

heres what i have

/*
 * C program to find the sum of odd and even numbers from 1 to N
*/

#include <stdio.h>

void main()
{
    srand(time(NULL));
    int arry[10]; 
    int i;
    int num;
    int odd_sum = 0;
    int even_sum = 0;

    for(i = 0; i < 10; i++){
     arry[i] = rand()%5+0;
     }
   for(i = 0; i <10; i++){
    printf("\nElement[%d]: %d", i, arry[i]);
   }
    for (i = 1; i <= num; i++){
     if (i % 2 == 0)
     even_sum = even_sum + i;
     else
     odd_sum = odd_sum + i;
    }
    printf("\nSum of all odd numbers  = %d\n", odd_sum);
    printf("Sum of all even numbers = %d\n", even_sum);

}

As pointed out your problem is here:

for ( i = 1; i <= num; i++ ) {
    if ( i % 2 == 0 )
        even_sum = even_sum + i;
    else
        odd_sum = odd_sum + i;
}

Instead of using i you should using the index of arry[i]:

for ( i = 1; i <= num; i++ ) 
{
    if ( arry[i] % 2 == 0 )
    {
        even_sum += arry[i];
    }
    else
    {
        odd_sum += arry[i];
    }
}

Something else to consider. Instead of printing the array and checking for odd and even numbers in extra loops, you can print and check when they're entered into the array:

void main()
{
    srand( time( NULL ) );
    int arry[10];
    int i;
    int num;
    int odd_sum = 0;
    int even_sum = 0;
    for ( i = 0; i < 10; i++ ) {
        arry[i] = rand() % 5;
        printf( "\nElement[%d]: %d" , i , arry[i] );
        if ( arry[i] % 2 == 0 ) {
            even_sum += arry[i];
        }
        else {
            odd_sum += arry[i];
        }
    }
    printf( "\nSum of all odd numbers  = %d\n" , odd_sum );
    printf( "Sum of all even numbers = %d\n" , even_sum );
}

Edited 1 Week Ago by tinstaafl

I would change tinstaafl's line 12 back to if (i % 2 ==0) since the problem was adding i contents instead of arry[i] contents where the random values lay.