Question: Create a function named oddSum. This function will return the sum of every odd value in the array. The function has two parameters: int arr[],
Create a function named oddSum. This function will return the sum of every odd value in the array.
The function has two parameters:
- int arr[], an array of integers
- int size, the size of the array.
Use the following function definition:
// This is what I have but it keeps adding numbers that are not an odd value. It will just add every number in the array instead of adding odd values only.
int oddSum(int arr[], int size){
int totalSum = 0; if (size <= 0) return 0; else for (int i = 0; i < size; i++) { if (arr[i % 2 != 0]) totalSum = totalSum + arr[i]; } return arr[0] + oddSum(arr + 1 , size - 1);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
