In C Not C++
write a function that takes in the reference of an int array and
return the value -1 if sum is odd, and 1 if the sum is even.
#include <stdio.h> int sum_even_odd(int *arr, int size); int main() { int arr1[] = {4, 9, 1, 2}; int arr2[] = {4, 9, 1, 2, 3}; printf("%d\n", sum_even_odd(arr1, 4)); printf("%d\n", sum_even_odd(arr2, 5)); return 0; } int sum_even_odd(int *arr, int size) { int sum = 0, i; for (i = 0; i < size; ++i) { sum += arr[i]; } if (sum % 2 == 0) return 1; else return -1; }
Get Answers For Free
Most questions answered within 1 hours.