Please help interpret this question.

Hi all,

I have a hard time understanding this question from my textbook:

"Write a recursive function named oddNum that accepts three arguments: an array, the size of the array and an integer parameter that represents the number of odd values in the array. The function should return the number of odd values in the array or 0 if there is no odd value in the array. Demonstrate the function in a driver program. No output statement in the function."

I don't quite understand the purpose of "number of odd values in the array" as the parameter if I'm asked to return the number of odd values....it wouldnt make sense to return a recursive function if something is already given....this makes me lol a bit....and frustrated at the same time...

thank you all for the help.
It looks like they want you to pass a reference to a variable that the function will update with the value that represents the total number of odd numbers. That's why it says "No output statement in the function." *finger wag*

I have no effing clue why you would want to use recursion for any part of this but if its part of the assignment I'm sure you can contrive some recursive aspect of the function and still have it get the job done.


Ok so im not sure if this will compile, but thats not really the point. Hopefully it gives you an idea of what i mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

void updateNumOfOddElements
  (int array[], int sizeOfArray, int &numOfOddElements){

    for (int i=0 ; i<sizeOfArray ; ++i) {
        if (array[i] % 2 == 1) {
            ++numOfOddElements;
        }
    }

}

int main() {

    int array[] = {1, 2, 3, 4, 5};
    int sizeOfArray = 5;
    int numOfOddElements = 0;

    updateNumOfOddElements(array, sizeOfArray, numOfOddElements);

    std::cout << numOfOddElements << endl;

    return 1;

}


Do note the ampersand on line 6. That's the key to this riddle.

*edit* my curiosity got the better of me. i had to try to compile it and the darn thing wont compile. haha i guess this one is going to hop over to its own thread. too late to donk around with it now. im going to bed.
Last edited on
1
2
3
4
5
  /*  It looks like they want you to pass a reference to a 
variable that the function will update with the value that 
represents the total number of odd numbers. 
That's why it says "No output statement in the function."  */


omg...I've never thought of that LOL...wow it makes much more sense now.

I've gotten the recursive function to search for odd numbers done....now I gotta integrate the reference oddnumber amount in the function....this is such a pain in the rear. It is actually a simple program without the recursion and updates.....but my school's textbook just love to mess with ppl's head...

tyvm for the help :)
Last edited on
I would love to see your code when you are done.

Also so much for going to bed.

Also also I figured out what was wrong with my code. I edited my post above so that it runs now. And returns the correct answer. Bonus. :D
Last edited on
Topic archived. No new replies allowed.