CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2006
    Posts
    15

    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

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    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
    Last edited by laitinen; January 1st, 2007 at 07:42 AM.

  3. #3
    Join Date
    Jun 2006
    Posts
    15

    Re: Recursion exercise

    Thanks, that seems to have solved it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured