Thread: finding the largest ascending subsequence..

  1. #61
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i swear my VS shows 4 while codeblock shows 5

    and i did rebuild on VS2005 for five times
    ??

  2. #62
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    where you from trans?

  3. #63
    Registered User TriTon's Avatar
    Join Date
    Dec 2008
    Posts
    5
    mkruk, your function is nice, but it doesn't work with negative numbers,
    or maybe that's just the way it's supposed to be?

    good work anyway.

  4. #64
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why it doesnt work on this example of negative numbers??
    it shows 4 instead of 6
    Code:
    #include <stdio.h>
    int max_seq(int *arr, int i, int last, int len);
    int main()
    {
    
            int i[] = { -3, -2, -1, 4, 8, 6, 53, 9, 18};
            printf("%d is the longest sequence in i\n", max_seq(i, 0, 0, 9));
    
        return 0;
    }
    
    int max_seq(int *arr, int i, int last, int len)
    {
            int eval,eval2;
            if(i > len)
                    return(0);
             eval = (i == 0 ? max_seq(arr+1, 0, 0, len-1) : 0);
             eval2 = max_seq(arr, i+1, (arr[i] > last ? arr[i] : last), len) + (arr[i] >= last ? 1 : 0);
            return(eval > eval2 ? eval : eval2);
    }
    Last edited by transgalactic2; 01-16-2009 at 07:54 AM.

  5. #65
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just a note: I think it should show 7, because here is a valid subsequence:
    Code:
    -3, -2, -1, 4, 6, 9, 18
    That's seven numbers.

    And, actually, that code you just posted prints 3 for me.

    Unfortunately, I have no time to debug.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #66
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it on the ascending order
    after 4 the next member must be 8

  7. #67
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why its not working?

  8. #68
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is the translated version:
    i traced it for this simple case
    and i got twice arr+1
    i get out of bounds
    ??
    Code:
    #include <stdio.h>
    int max_seq(int *arr, int i, int last, int len);
    int main()
    {
    
          int i[] = { -3,-2};
    	printf("%d is the longest sequence in i\n", max_seq(i, 0, 0, 2));
    
        return 0;
    }
    
    int max_seq(int *arr, int i, int last, int len)
    {
        int nSecondIf;
        int nFirstIf;
            int eval,eval2;
            if(i > len)
                    return(0);
            if(i==0)
           {
              eval= max_seq(arr+1, 0, 0, len-1) ;
            }
          else
    
          {
               eval=0;
          }
    
      if(arr[i] > last)
    	  nFirstIf = arr[i];
      else
    	  nFirstIf = last;
    
    
      if(arr[i] >= last)
    	  nSecondIf = 1;
      else
    	  nSecondIf = 0;
    
      eval2 = max_seq(arr, i+1, nFirstIf, len) + nSecondIf;
    
          if(eval > eval2)
           {
             return eval ;
            }
          else
    
          {
               return eval2;
          }
    
    }
    Last edited by transgalactic2; 01-16-2009 at 11:46 AM.

  9. #69
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't know why. But here's my annotated solution if you'd like to look at it. My solution prints 7 as expected.
    Code:
    #include <stdio.h>
    
    #define maximum(a, b) ((a) > (b) ? (a) : (b))
    
    int longest_ascending(int *data, int position, int previous, int length) {
        if(position >= length) return 0;
        
        int longest = 1;
        
        /* This is the first number in a sequence. */
        if(position == previous) {
            longest = maximum(longest,
                1 + longest_ascending(data, position + 1, previous, length));
            
            /* Try starting a sequence at the next position. */
            longest = maximum(longest,
                longest_ascending(data, position + 1, position + 1, length));
        }
        else {  /* This is not the first number in a sequence. */
            /* This number is greater than the previous one;
                try adding it to the sequence.
            */
            if(data[position] > data[previous]) {
                longest = maximum(longest,
                    1 + longest_ascending(data, position + 1, position, length));
            }
            
            /* Try leaving this number out of the sequence. */
            longest = maximum(longest,
                longest_ascending(data, position + 1, previous, length));
        }
        
        return longest;
    }
    
    int main() {
        int data[] = {-3, -2, -1, 4, 8, 6, 53, 9, 18};
        
        printf("Longest ascending sequence: %d\n",
            longest_ascending(data, 0, 0, sizeof(data) / sizeof(*data)));
        
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #70
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i fixed them thanks
    Last edited by transgalactic2; 01-16-2009 at 12:49 PM.

  11. #71
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you might as well tell us what you did in case other puzzled people are reading this thread and scratching their head.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #72
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the start of this thread i showed the code where it shows the length of the sequence
    of cell [0]
    so i built an iterative code
    and i was thinking how to turn it into a recursive one

  13. #73
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    my attempt:
    Code:
    #include <stdio.h>
    int foo(int a[],int size,int i,int j) {
      // pre-condition: j>i
      if(j>=size) return 1;
      int r1=foo(a,size,j,j+1)+(a[j]>a[i]);
      int r2=foo(a,size,i,j+1);
      return (r1>r2?r1:r2);
    }
    int main() {
      int a[]={45,1,21,3,33,6,53,9,18};
      printf("%u\n",foo(a,sizeof(a)/sizeof(int),0,1)); // 5
      int b[]={-3,-2,-1,4,8,6,53,9,18};
      printf("%u\n",foo(b,sizeof(b)/sizeof(int),0,1)); // 7
      int c[]={1,2,3,4,5,6,7,8,9,10};
      printf("%u\n",foo(c,sizeof(c)/sizeof(int),0,1)); // 10
      return 0
    }

  14. #74
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    for this input -9, -8, -7, -6, -5, -99, -3, 9, 18 i get 8
    but i should get 5
    where is the mistake
    ??
    Code:
    #include <stdio.h>
    
    #define maximum(a, b) ((a) > (b) ? (a) : (b))
    
    int longest_ascending(int *data, int position, int previous, int length) {
        
    	
        int longest = 1;
    	if(position >= length) return 0;
        
        
        /* This is the first number in a sequence. */
        if(position == previous) {
            longest = maximum(longest,
                1 + longest_ascending(data, position + 1, previous, length));
            
            /* Try starting a sequence at the next position. */
            longest = maximum(longest,
                longest_ascending(data, position + 1, position + 1, length));
        }
        else {  /* This is not the first number in a sequence. */
            /* This number is greater than the previous one;
                try adding it to the sequence.
            */
            if(data[position] > data[previous]) {
                longest = maximum(longest,
                    1 + longest_ascending(data, position + 1, position, length));
            }
            
            /* Try leaving this number out of the sequence. */
            longest = maximum(longest,
                longest_ascending(data, position + 1, previous, length));
        }
        
        return longest;
    }
    
    
    
    int main() {
    	 
        int data[] = {-9, -8, -7, -6, -5, -99, -3, 9, 18};
        
        printf("Longest ascending sequence: %d\n",longest_ascending(data, 0, 0, sizeof(data) / sizeof(*data)));
        
        return 0;
    }
    Last edited by transgalactic2; 01-16-2009 at 04:11 PM.

  15. #75
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    for this input -9, -8, -7, -6, -5, -99, -3, 9, 18 i get 8
    but i should get 5
    where is the mistake
    ??
    I think we're talking about different things. The longest consecutive ascending sequence has length 5: (-9, -8, -7, -6, -5). But the longest ascending subsequence, where you're allowed to jump over numbers, has length 8: (-9, -8, -7, -6, -5, -3, 9, 18).

    My program, along with most of the others in this thread, solve the second (and far more complicated problem). I think, from your original description of the problem, that that is what you're actually trying to do.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. Finding the 3 largest numbers of the five entered
    By Bkgrant in forum C Programming
    Replies: 11
    Last Post: 02-13-2009, 01:08 PM
  3. finding the largest number in a binary search
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 07-31-2008, 03:19 AM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM