0

How would i be able to create a recursive function that should accept its parameter as a vector of ints.

Pretty much i need all even numbers entered to be added while all odd numbers to be subtracted how would i do that ?

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 2
    `template void f(const TVector& v) { f(v); } // recursive function` – James McNellis May 12 '11 at 05:11
  • 6
    Adding the homework tag since no one would do this recursively for any other reason. – Brendan Long May 12 '11 at 05:15
  • Why do you need recursive function for adding/subtracting ? – iammilind May 12 '11 at 05:17
  • "Pretty much i need all even numbers entered to be added while all odd numbers to be subtracted how would i do that?" I can't grok this requirement. By adding and subtracting do you mean performing the mathematical operation or the act of inserting and eliminating elements in a vector? If the former, adding/subtracting by what? You need at least two numbers to add/subtract anything. And by "numbers entered" do you mean that a user is inputting these numbers? And what makes you think recursion is the best way to implement such a function? You need to be more specific in your question. – In silico May 12 '11 at 05:18
  • 1
    the program would take a set of numbers and depending on if they are even or odd it would either add or subtract. for example: if i enter 1,2,3,4,5,6,7,8 the program should output 1-2+3-4+5-6+7-8=whatever that is the reason it has to be recursion using a vector of integers is because thats what the professor specified – user749829 May 12 '11 at 05:19
  • 3
    What code do you have, what have you tried, and what problem are you having? – Brendan Long May 12 '11 at 05:20
  • @user749829: What's the base case? That is, at what point is this recursive function supposed to terminate? It can't go on forever since you'll overflow the stack. Is the user supposed to enter some special value that signifies the end of the stream of numbers? – In silico May 12 '11 at 05:24
  • @In The base case is a vector of length 1, surely. @user749829 your function will work from the back of the vector; V(1:n) = V(1:n-1) + V(n) or ... - V(n) depending on parity, recurse until n = 1. But you should probably actually put in an effort yourself if you want more help here. – Matt Phillips May 12 '11 at 05:42

1 Answers1

3

You are trying to go about solving the problem the wrong way. First, an iterative solution is far more straightforward here (and makes far more sense) than a recursive solution. However, if you do need a recursive solution, a function that takes a pair of iterators makes far more sense:

template <typename ForwardIterator>
typename std::iterator_traits<ForwardIterator>::value_type
do_math_stuff(ForwardIterator first, ForwardIterator last)
{
    if (first == last)
        return 0;

    return (*first % 2) 
        ? (*first + do_math_stuff(std::next(first), last))
        : (*first - do_math_stuff(std::next(first), last));
}

int main()
{
    std::array<int, 8> x = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int result = do_math_stuff(x.begin(), x.end());
}

If you write a function that takes a whole container as an argument you have to make a brand new container for each recursive call to the function, which is just silly. By having the function take iterators into the container, you not only allow the function to be used with any forward iterable range but you also no longer require any copies of the container to be made.

Algorithms should be implemented in terms of iterators, not containers.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Hmm that code isnt working when i try to compile in ms visualstudio 2010 – user749829 May 12 '11 at 05:39
  • I agree containers a good way of implementing such a problem but its the directions i was given by my instructor so i have no choice but to follow it heh. – user749829 May 12 '11 at 05:41
  • With the correct Standard Library headers (array, iterator), this certainly does compile successfully under Visual C++ 2010. As for "I have no choice but to follow it," that is absolutely wrong. If an instructor tells you to solve a problem the wrong way, learn what the correct method (or at least a better method) to solve the problem is, understand that method, solve the problem using that method, then go explain to your instructor why you are right. – James McNellis May 12 '11 at 05:48
  • Got it now, sorry instead of using array, how can i implement using a vector ? std::vector v(8) { 1, 2, 3, 4, 5, 6, 7, 8 }; doesnt work – user749829 May 12 '11 at 05:54
  • There is no difference: the whole point of using iterators is that the algorithm is container independent: it can operate on any forward iterable range, whether that range comes from an array, a `std::vector`, or somewhere else. If you're asking "how do I use `std::vector`?", I recommend that you get [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis May 12 '11 at 05:57
  • Alright, thanks for all the help 1 last thing though, how would i be able to specify for a user input so the user can input whatever numbers they want instead of having a set array of numbers ? – user749829 May 12 '11 at 06:10
  • can you explain please why you templatized it? as far as I understand only ints are acceptable input type, as well as % operation on them... although nice work! – Code_So1dier May 12 '11 at 06:16
  • @Code_So1dier: `long` and `long long` would also work with this function, as would any integer-like user-defined data type (like a "big int" struct type). – James McNellis May 12 '11 at 06:17
  • Would associative containers work with this iterators too? Thanks. – Code_So1dier May 12 '11 at 06:22
  • @Code_So1dier: Sure, you would just need a transform iterator to convert the range of key-value pairs into a range of values (for example, `boost::transform_iterator` provides a generic transforming iterator that when combined with a user-defined transformation function could do this). But, no, to answer the question you were really asking, you can't _directly_ use this with an associative container. – James McNellis May 12 '11 at 06:24
  • ok another quick question, instead of adding all even and subtracting all odd, what would i do if i had to a set of arrays/vectors say {1,2,3,4,5,6,7,8} the program would have to do 1-2+3-4+5-6+7-8 =answer ? – user749829 May 12 '11 at 06:35