Trouble filling in Odd Numbers.

I'm having trouble filling in the odd numbers from a list. I can only use recursion to fill in those numbers (no loops). ListFirst is of type int (which gives me the first element of the list) and ListRest of type ListView gives me the rest of the list (not including the first list). Here is my snippet of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

 ListView GetOdd(ListView list) {

    
    if (ListFirst(list) % 2 == 1) {  //If remainder is odd
            return GetOdd(ListRest(list));
        }
    else{
        int newEl = ListFirst(list);
        newElement = NewList(newEl, NewList()); //NewList() makes a new list
        return newElement;
    }
 
}

There are two lists provided: (1 2 3 4 5) and (11 12 13 14 15) (of type ListView) that are passed through this function. The output that im getting is (2) and (12). If anyone could give me some insight on why it is giving me this output, any help would be appreciated!
What else is your ListView class capable of?

> The output that im getting is (2) and (12).
That's because you're ignoring odd numbers, and making a list out of even numbers.

> newElement = NewList(newEl, NewList());
If this is making a list from an element and a list, then what you need is
 
return NewList(newEl, GetOdd(ListRest(list)));
Topic archived. No new replies allowed.