8 Replies - 2357 Views - Last Post: 01 October 2012 - 07:24 PM Rate Topic: -----

#1 aremy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 12

Recursive count down then back up

Posted 01 October 2012 - 06:40 PM

Hello,
I have to write a program that will get passed 2 numbers, a high and a low. It needs to use recursion to print out numbers starting with the high number and incrementing down one at a time until it equals the low number then count back up. So if it was passed count(4,10) it would print out 10 9 8 7 6 5 4 4 5 6 7 8 9 10. I can pretty easily use recursion to count down or up, but I don't know how to make it switch from down to up when it needs to. If someone could help me out conceptually with what I need to do it would be much appreciated. I can probably figure it out with just an explanation,I don't need the whole code written for me.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive count down then back up

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Recursive count down then back up

Posted 01 October 2012 - 06:44 PM

Change the sign of the increment.
Was This Post Helpful? 0
  • +
  • -

#3 Midi_  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 109
  • Joined: 22-May 12

Re: Recursive count down then back up

Posted 01 October 2012 - 06:49 PM

View Postaremy, on 01 October 2012 - 06:40 PM, said:

Hello,
I have to write a program that will get passed 2 numbers, a high and a low. It needs to use recursion to print out numbers starting with the high number and incrementing down one at a time until it equals the low number then count back up. So if it was passed count(4,10) it would print out 10 9 8 7 6 5 4 4 5 6 7 8 9 10. I can pretty easily use recursion to count down or up, but I don't know how to make it switch from down to up when it needs to. If someone could help me out conceptually with what I need to do it would be much appreciated. I can probably figure it out with just an explanation,I don't need the whole code written for me.

Thanks


you need to make the program to decriment down to 4 skipping numbers. once you are done use that same loop(copy and paste it so you now have 2 separate loops) to incriment up to 10.

in short, use two opposite loops
Was This Post Helpful? 0
  • +
  • -

#4 aremy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 12

Re: Recursive count down then back up

Posted 01 October 2012 - 06:52 PM

Since it is the same function being called to count down as it is to count up how does the function know what direction to increment? It is just getting 2 numbers and it needs to somehow know if the function is on the way down or the way back up...

Quote

you need to make the program to decriment down to 4 skipping numbers. once you are done use that same loop(copy and paste it so you now have 2 separate loops) to incriment up to 10.

in short, use two opposite loops


The assignment needs to use recursion, wouldn't that be an iterative solution?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Recursive count down then back up

Posted 01 October 2012 - 07:09 PM

Start with an increment of -1. When the number reaches the low point, change it to 1.
You'll probably want to make it a static variable.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1304
  • View blog
  • Posts: 4,488
  • Joined: 19-February 09

Re: Recursive count down then back up

Posted 01 October 2012 - 07:17 PM

Another parameter could denote the direction/increment.

Edit: it could be a default parameter.

This post has been edited by #define: 01 October 2012 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Recursive count down then back up

Posted 01 October 2012 - 07:18 PM

I don't want to give you the answer, so I'll give you an analogue of the problem. Try this code out for size and see how it works. Once you see it in action, you can figure out how to modify it for your specific problem.

#include <iostream>

using namespace std;

void PadIt(int count, char ch)
{
    for(int i = 0; i < count; ++i)
        cout << ch;
}

void DoIt(int depth)
{
    if (depth == 5)
        return;

    PadIt(depth, '+');
    cout << '(';

    DoIt(depth+1);

    cout << ')';
    PadIt(depth, '-');
}

int main()
{
    DoIt(0);
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Recursive count down then back up

Posted 01 October 2012 - 07:20 PM

I took the 2 parameters to be a requirement of the assignment: Not 1, not 3, but 2. I assumed part of the lesson was in using static variables. ;)
Was This Post Helpful? 1
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Recursive count down then back up

Posted 01 October 2012 - 07:24 PM

Notice that I didn't need to give a direction for increment or decrement. The number of plusses and minuses just increased as the depth got deeper, and lessened as the depth got shallower.

View PostCTphpnwb, on 01 October 2012 - 07:20 PM, said:

I took the 2 parameters to be a requirement of the assignment: Not 1, not 3, but 2. I assumed part of the lesson was in using static variables. ;)


Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1