C12 Answer


/* Puzzle C12 -- add up only the ODD integers in an array of integers */


/* Add up the odd elements in an array
|  The array may contain negative odd integers,
|  so test if arr[j]%2 is other than zero. (It
|  might be +1 or -1).
*/
long addOddArray( int arr[], int size )
{
  int j;
  long sum = 0;
  
  for ( j=0; j<size; j++ )
    if ( arr[j]%2 != 0 )
      sum += arr[j] ;
    
  return sum;
}

void printArray( int arr[], int size )
{
  const int N = 10;
  int j;
  
  for ( j=0; j<size; j++ )
  {
    if ( j%N == N-1 )
      printf("%4d\n", arr[j] );
    else
      printf("%4d ", arr[j] );    
  }
}

#define SIZE 10
int main(int argc, char *argv[])
{
  int x[ SIZE ] = { 0, 2, 1, -3, -5, 2, 4, 6, 9, 60};
  
  printArray( x, SIZE );
  printf("\n");
  printf("sum = %ld\n", addOddArray( x, SIZE ) );
  
  system("pause");	
  return 0;
}