Question

In C Not C++ write a function that takes in the reference of an int array...

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.

Homework Answers

Answer #1
#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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a c++ function which takes two parameters: an array of ints and an int size...
Write a c++ function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
(C++) Write a function called triple that takes an array of integers as a parameter as...
(C++) Write a function called triple that takes an array of integers as a parameter as well as the length of the array. It should triple each value in the array. The function should not return anything. (Note that the contents of the array WILL be modified.)
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c =...
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c = &array[5];   c[-1] = 2;   array[1] = b[1]-1;   *(c+1) = (b[-1]=5) + 2;   sum += *(a+2) + *(b+2) + *(c+2);   printf("%d %d %d %d\n", sum, array[b-a], array[c-a], *a-*b); array[8]={ ?? }; what is array[8]?
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Write a function named “highestScore” that takes an array of floating point scores and its size...
Write a function named “highestScore” that takes an array of floating point scores and its size as parameters and return the highest of these scores. The function prototype: float highestScore(float scores[], int size);
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
Write a function in (C++) named - accending_order - that takes a reference to a vector...
Write a function in (C++) named - accending_order - that takes a reference to a vector of string (like {"HI 32", "HELLO 78", "NAME 97", "WORLD 07"}), and returns nothing. The function should reorder the vector so that the courses are sorted by number in ascending order. Write this function without - int (main). CODE IN C++ ONLY. EXPECTED OUTPUT: {"WORLD 07", "HI 32", "HELLO 78", "NAME 97"}
[ Write in C not C++] Define a C function void placeElements(int *a, int i, int...
[ Write in C not C++] Define a C function void placeElements(int *a, int i, int j) that inter-exchange the array elements at indexiandj. For a[] = {5,7,4,2,1},a callto placeElements(a,2,4) makesa[] = {5,7,1,2,4}. [8]