recursion mirror number output

closed account (4ET0pfjN)
Where is return statement so that if I call for e.g. printNum(1), the output is:
123456789987654321? Also, if someone give me some assistance with my other post at http://www.cplusplus.com/forum/general/71234/. I'm having trouble understanding how I can use pointers to pointers b/c I need to modify the original front pointer in the list (in main function of course) if the new node is "smaller" than current front of list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printNum (int num)
{
  if ( num < 9 )
  {
    printNum( num + 1 );
  }
  cout << num;
}

int main ()
{
  printNum( 1 );
  return 0;
}
You're only printing each number once:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void printNum (int num)
{
  (cout<<num) //If you wanted to print the numbers like you have them,
  if ( num < 9 ) //You'd need to put another cout here.
  {
    printNum( num + 1 );
  }
  cout << num; //This is the only printing you're doing. 
}

int main ()
{
  printNum( 1 );
  return 0;
}


Also, the return is implicitly at the end of the function since it's a void function.
closed account (4ET0pfjN)
do you mind taking a look at my pointer to pointer issue with my ordered insert for singly linked list? It works, and I'm pretty sure the logic is right, but i think it's the ptrs to ptrs messing me up...The link is in this first post.
Last edited on
Topic archived. No new replies allowed.