the sum of the odd numbers drawn in the array

Asked

Viewed 18 times

-1

Someone give me a hand on how to calculate the odd numbers drawn from that array at the end there

#include <stdio.h> 
#include <stdlib.h>
#include <time.h> 

const int T = 10;        // tamanho do array 
const int L = 24;       // limite para rand () [0-23] 

int main ()
{
    int n[T];                // numeros sorteados 
    int m = 0;              //  maior número 
    int i;
    int num;
    int sum;
    
    printf ("Os numeros sorteados foram\n");
    printf ("-------------------------\n");
    
    srand (time (NULL) );
    
    m = 0;
    i = 0;
    while (i < T)
    {
        n[i] = rand () % L;
        printf ("%2d ", n[i]);
        
        if (m < n[i]) 
        {
            m = n[i];
        }
        i++;
    }
    printf ("\n\n");
    
    printf ("Ordem inversa\n");
    printf ("-------------\n");
    
    i = T - 1;
    while (i >= 0)
    {
        printf ("%2d ", n[i]);
        i--;
    }
    printf ("\n");
    
    printf ("\n\n");
    
    printf ("A soma dos impares sorteados é \n");
    printf ("-------------\n");
    
     for(i=0; i<10; i++)
    {
        if(n[i]%2==1)
            sum=sum+n[i];
    }
    printf("Total Sum of Odd values is: %d ",sum);
    
    return 0;
    
}
  • From what I saw, it was only necessary to initialize the variable sum with a value, for your case would be zero. int sum=0;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.