Simple recursion problems

May 26, 2008 at 6:46pm
Hello,
After several revision attepmts of this very simple program, I decided to come here and ask for help. I am studing recursive functions and one of my assignments is to print the numbers 1-10 to the screen using a recursive function. I am having troubles understanding recursion, if anyone could help with this, that'd be great, a tip on understanding recursion would be apreciated also, here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int rec(int num);

int main()
{
	int take=10;

	cout << rec(take);

	return 0;
}

//recursive function to print the numbers 1-10 to the screen
int rec(int num)
{
	int val;

	if(num==10) return num;
	val  = rec(num) - 1;
	return(val);

}

Thanks,
enduser000
May 26, 2008 at 6:52pm
Ok.
Here are some initial problems.

1. You want to print from 1 to 10, yet your take starts at to (Line 8)
2. You only have 1 cout statement, and your not building an output string.

You need to simplify your code. Remember that your recursive function should perform 3 tasks.
1. Check if the condition of > 10 has been reached, if so return.
2. cout the current value of num
3. re-call the function with num+1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//recursive function to print the numbers 1-10 to the screen
void rec(int num)
{
  if (num > X)
    return;

  cout << num << " ";

  rec(num+1);
}


int main()
{
  rec(1);

  return 0;
}

Last edited on May 26, 2008 at 6:54pm
May 26, 2008 at 7:45pm
Hey thanks for the help, really simplifed things.
enduser000
May 26, 2008 at 7:49pm
While we're on this topic, could anyone explain how this works to me? It's the code the book (http://msdn.microsoft.com/en-us/beginner/cc305129.aspx) and he decribe it as using the" telescope effect" but I just can' picture exactly it works in my head...
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//the quicksort
#include <iostream>
#include <cstring>
using namespace std;

void quicksort(char *items, int len);

void qs(char *items, int left, int right);

int main()
{
	char str[] = "jfmckldoelazlkper";

	cout << "Original order: " << str <<'\n';

	quicksort(str, strlen(str)); //call the function to sort the string (character sort)

	cout << "sorted order: " << str << '\n';

	return 0;
}

	//set up the call to the actual sorting function
void quicksort(char *items, int len)
{
	qs(items, 0, len-1);
}

	// A recursive version of Quicksort for sorting characters
void qs(char *items, int left, int right)
{
	int i, j;
	char x, y;

	i = left; j = right;
	x = items[( left+right) / 2 ];

	do{
		while((items[i] < x) && (i < right)) i++;
		while((x < items[j]) && (j > left)) j--;

		if(i <= j){
			y = items[i];
			items[i] = items[j];
			items[j] = y;
			i++; j--;
		}

	}while(i <= j);

	if(left < j) qs(items, left, j);
	if(i < right) qs(items, i, right);
}

enduser000
May 26, 2008 at 8:51pm
That is an incremental search alg. Instead of being accurate from the begining it slowly moves characters towards the front/back of the array based on their value against the middle char x = items[( left+right) / 2 ];.

It will swap chars around trying to move the lower ones to the left side of the middle char, and higher ones to the right side of the char. Eventually with enough iterations it will have them in the correct order.
Topic archived. No new replies allowed.