Help Code Is Not Working

This is a discussion on Help Code Is Not Working within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by DaveH Is it? I might be wrong, its been a long time since I dealt with recursive ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DaveH View Post
    Is it? I might be wrong, its been a long time since I dealt with recursive functions, but the curr value never gets altered, so that function keeps being called with the same parameters. Infinite loop. If I'm missing something, please correct me.
    curr is set to back[curr] in the call.

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    Code:
    delete rank, back ;
    ???
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    sorry i tried but i am unbale to get can u help me out !!

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    delete rank, back does not delete rank and back. To delete rank and back you would need to use
    Code:
    delete[] rank;
    delete[] back;
    Note also the brackets.
    And for all that is good, do not return when curr is zero. You still need to print out the array element. Do NOT return. You should merely skip the recursive call.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    i tried but still it didnt work
    can u tell me why by looking at my code
    Code:
    #include<iostream.h>
    #include<conio.h>
    int maxRank (int curr, int* arr, int* rank) {
        int max = 0 ;
        for (int i = 0; i <= curr; i++) {
    	if (arr[i]<arr[curr] && rank[i]>rank[max]) {
    	    max = i ;
    	}
        }
    
        return max ;
    }
    
    void printReverse (int curr, int* arr, int* back) {
      //  if (curr == 0) return ;
        printReverse (back[curr], arr, back) ;
        cout << arr[curr] << " " ;
    }
    
    void longestMonotoneSubsequence (int count, int* arr) {
        int* back = new int[count] ;
        int* rank = new int[count] ;
        int max = 0 ;
        rank[0] = 0, back[0] = 0 ;
        for (int i = 0; i <= count; i++) {
            back[i] = maxRank (i, arr, rank) ;
            rank[i] = rank[back[i]]+1 ;
            if (rank[i] > rank[max]) {
                max = i ;
            }
        }
    
        printReverse (max, arr, back) ;
        cout << "\n" ;
    
    }
    
    void main()
    {
    int n;
    int a[100];
    cin>>n;
    for(int i=0;i<n;i++)
    {
    cin>>a[i];
    }
    longestMonotoneSubsequence(n,a);
    }

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I am so tired of this thread I will fix the code:
    Code:
    void printReverse (int curr, int* arr, int* back) {
      if (curr != 0)
        printReverse (back[curr], arr, back) ; //Make the recursive call UNLESS we are at the end.
        cout << arr[curr] << " " ;  //You must ALWAYS ALWAYS ALWAYS print the number
    }

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    but the code dosent work out for
    50
    49
    58
    51
    60
    it gives
    50 49 58 60

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    13
    it must not give 49

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So stepping through you code with those numbers:
    when i=0, back[0] is set to 0 and rank[0] is set to 1.
    when i=1, back[1] is set to 0 and rank[1] is set to 2.
    when i=2, back[2] is set to 1 and rank[2] is set to 3.

    So your printReverse should actually stop, not when curr == 0, but when back[curr] == 0.

    Edit: And note that it must so give 49, as per the following example:
    52 49 51 58 60
    In this case, starting with 52 you can only get three up; starting with 49, you can get four up.
    Last edited by tabstop; 11-12-2008 at 11:36 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 07:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 11:00 AM

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