Fiirst experiences with Recursion, Maybe a simper way?

This is my first recursion homework assignment, I think i got the LLL (Question 3) Right, but I wanted to check with the other ones, is there a simpler way to do this? Thanks!

-Ryan

Recursion
1) Write the code to count the number of positive values (greater than zero) in an array of integers
1
2
3
4
5
6
7
8
9
10
void count(int array, int counter, int i)
{
	while(array[i])  //while the number is not null is not null
	{
		if(array[i] > 0)
		++i;  ++ counter		
cout<< counter;	

count(array, counter, i);
}
}
2) Write the code to display all values in an array of integers
1
2
3
4
5
6
7
8
9
10
11
void display(int array, int i)

{
	if (!array[i])
	return;
	else
	{
	cout<<array[i];
	display(array, ++i);
	}
}

3) Write the code to display all values in a linear linked list of integers

1
2
3
4
5
6
7
8
9
 Void display (node* head)

{
		If(head)
		{
			Cout<<head->data;
			Display(head->next);
		}
	}
closed account (Lv0f92yv)
For number three, double check the function call vs. the function definition.

For number two, some consider it better style to explicitly use i+1 in your case, instead of changing the value of the variable itself in the parameter list.

I might rethink number one. Typically, looping inside a recursive function defeats the purpose of the function being used recursively. That exercise can become very simple if you consider one element at a time in the function.
Topic archived. No new replies allowed.