-
Recursion exercise
Hi guys, seasons greetings.
I am doing a little exercise on recursion. This piece of code lists an array in reverse order.
Code:
//Chapter 6: Programming Exercises Question 12
#include <iostream>
using namespace std;
int func (int x[], int lo, int hi);
int main()
{
int intArray[] ={1,2,3,4,5,6,7,8,9,10};
int length = 10;
int low = 0;
int high = 9;
cout<<func(intArray, low, high);
return 0;
}
int func (int x[], int lo, int hi)
{
if(lo <= hi)
{
func( x, lo+1, hi);
cout<<x[lo]<<" ";
}
}
It works fine. Just when I get the print out, it has a number at the end.
eg,10 9 8 7 6 5 4 3 2 1 4416752
what is the "4416752"? where is it coming from? even if i tell it only tp print one number from the array, it appears after the numeber.
any ideas?
Ian
-
Re: Recursion exercise
Hi!
At least your code has one error. The func function is protyped to return an int. You dont return anything.
Try adding return 0 at the end of that function.
And change this line;
Code:
cout<<func(intArray, low, high);
to this;
Code:
func(intArray, low, high);
Laitinen
-
Re: Recursion exercise
Thanks, that seems to have solved it.