???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
sorry i tried but i am unbale to get can u help me out !!
delete rank, back does not delete rank and back. To delete rank and back you would need to use
Note also the brackets.Code:delete[] rank; delete[] back;
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.
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); }
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 }
but the code dosent work out for
50
49
58
51
60
it gives
50 49 58 60
it must not give 49
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 12:36 PM.