Challenge 1: Sum of All Odd Integers in an Array

In a given array, you need to compute the sum of all the odd integers.

Problem Statement

Implement a function sumAllOdds(int arr[], int size) which takes an array arr and its size and returns the sum of all the odd integers in the given array.

Input:

An array of integers and its size.

Output:

Sum of all odd integers in an array

Sample Input

arr[1,2,3,4,5,6,7], 7

Sample Output

16

Coding Exercise

This problem is designed for your practice, try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section.

Good Luck!

int sumAllOdds(int arr[], int size) {
int sum = -1;
// write your code here
return sum;
}

Solution Review

  • Initialize a variable sum and set it to 0
  • Use a for loop which runs till size of the arr
  • Use % modulo operator to check if arr ith index is odd
  • If the arr ith index is odd then add it to sum
  • Return the sum

In the next challenge, we’ll solve another problem of functions.