Help Code Is Not Working

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 11-10-2008
tabstop
Quote:

Originally Posted by DaveH
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.
• 11-10-2008
hk_mp5kpdw
Code:

`delete rank, back ;`
???
• 11-12-2008
srivatsan
sorry i tried but i am unbale to get can u help me out !!
• 11-12-2008
tabstop
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.
• 11-12-2008
srivatsan
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); }```
• 11-12-2008
tabstop
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 }```
• 11-12-2008
srivatsan
but the code dosent work out for
50
49
58
51
60
it gives
50 49 58 60
• 11-12-2008
srivatsan
it must not give 49
• 11-12-2008
tabstop
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.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12