Why does this recursive function not work otherwise.

This is a discussion on Why does this recursive function not work otherwise. within the C++ Programming forums, part of the General Programming Boards category; This recursive functions works this way... Code: void recurseReverse(vector<int>& aVec, intvec_sz start, intvec_sz end) { if ((end - start) <=1) ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Why does this recursive function not work otherwise.

    This recursive functions works this way...

    Code:
    void recurseReverse(vector<int>& aVec, intvec_sz start, intvec_sz end)
    {
         if ((end - start) <=1)
    	return;
       else
       {
          int temp = aVec[end - 1];
          aVec[end - 1] = aVec[start];
          aVec[start] = temp;
          recurseReverse(aVec, start + 1, end  - 1);
       }
    }
    but not this way...
    Code:
    void recurseReverse(vector<int>& aVec, intvec_sz start, intvec_sz end)
    {
       while((end - start) > 1)
       {
          int temp = aVec[end - 1];
          aVec[end - 1] = aVec[start];
          aVec[start] = temp;
          recurseReverse(aVec, start + 1, end  - 1);
       }
    }
    I assume that in the latter code, when it found that the end - start is one, it would return from the function

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while((end - start) > 1)
    end and start are not changed in the body of the loop. If the boody is entered it will never be left.
    Kurt

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I see. an if statement is more appropriate.

    Edit:makes it work.

    I have to look at the one I did in java, it was similar only with just arrays, not sure if i did a while loop or an if, but I see the stupid mistake I made.
    Last edited by indigo0086; 05-05-2006 at 09:18 AM.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by indigo0086
    I see. an if statement is more appropriate.

    Edit:makes it work.

    I have to look at the one I did in java, it was similar only with just arrays, not sure if i did a while loop or an if, but I see the stupid mistake I made.
    Isn't solution 1 an 2 the same now ?
    Kurt

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I changed the while to if
    Code:
    void recurseReverse(vector<int>& aVec, intvec_sz start, intvec_sz end)
    {
       if((end - start) > 1)
       {
          ....

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    No, Kurt's point was that you just made the solution recursive again. If you want to make the solution iterative, that's fine, but you have to carefully consider the while condition.
    Code:
    void iterReverse (vector<int>& aVec, intvec_sz start, intvec_sz end)
    {
       while((end - start) > 1) {
           int temp = aVec[end - 1];
           aVec[end - 1] = aVec[start];
           aVec[start] = temp;
           --end;
        }
    }
    or something like that.

  7. #7
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I was aiming for recursive, I just oversaw that the while statement kind of made it go on forever.

    And in your example you would have to add one to start, as I'm kind of shrinking the array on both sides by one, until it reaches the middle, if there is one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21