Explore BrainMass
Share

Explore BrainMass

    Passing array as parameter for finding the count of odd numbers

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    This question has three parts.

    1. Write a function that takes an array of int as a parameter and returns a count of odd numbers in the array.
    Assume the array has MAX elements where MAX is a global constant int. That means that before all of the functions
    there is a declaration like:

    const int MAX = 10;
    And arrays are declared like:
    int myArray[MAX];

    2. Write a function that takes an array of int as a parameter and returns the sum of odd numbers in the array.
    Assume the array has MAX elements where MAX is a global constant int.

    3. Rewrite your answer to the previous question as a recursive function.

    © BrainMass Inc. brainmass.com October 10, 2019, 8:21 am ad1c9bdddf
    https://brainmass.com/computer-science/cpp/passing-array-parameter-finding-count-odd-numbers-620977

    Solution Preview

    Check the attached .cpp file for inline comments.

    1. Solution for part 1

    int oddNum(int intArray[])
    {
    int count = 0;
    for(int i =0; i<=MAX; i++)
    {

    if(intArray[i]%2 ...

    Solution Summary

    The code is very simple and easy to understand, written in C++. A picture as well as .cpp file is also attached with inline comments. The code can easily be modified. It has three separate functions for each of the questions part. First two functions uses a for loop to scan all the elements within an array, counts odd numbers and sums them up, while the third function uses a 'while' loop to process elements in given array.

    $2.19