Thread: finding the largest ascending subsequence..

  1. #46
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how did you get 5 lines
    only the simplest (of checking one cell) case took 14 lines
    and its not even comparing between the other results
    ??

    Code:
    int max_seq(int *arr, int i, int last, int len)
    {
      if (i==len)
       {
        	return 1;
       }
      if (*arr>last)
      {
    	  return 1+max_seq(arr,i+1,last,len);
      }
      else
      {
          return max_seq(arr,i+1,last,len);
      }
    }

  2. #47
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    "just 5 lines of code..."
    way to go! do you mind posting the code.

  3. #48
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    its actually very simple.. for every cell you check
    a) if theres a bigger sequence starting at the next cell
    b) if that sequence is bigger than the sequence starting at this very cell

    if we're checking for case a, were not increasing i but arr by one, so the functions can keep track of whether its supposed to calculate a new sequence or add to a existing sequence. (because i will be 0 in case a)

    Anyway, heres my function:
    Code:
    int max_seq(int *arr, int i, int last, int len)
    {
    	if(i > len)
    		return(0);
    	int eval = (i == 0 ? max_seq(arr+1, 0, 0, len-1) : 0);
    	int eval2 = max_seq(arr, i+1, (arr[i] > last ? arr[i] : last), len) + (arr[i] >= last ? 1 : 0);
    	return(eval > eval2 ? eval : eval2);
    }

  4. #49
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got this example 45 1 21 3 33 6 53 9 18 where the longest is 1 3 6 9 18
    it should return 5
    and this function returns 4
    why??

    Code:
    #include <stdio.h>
    int max_seq(int *arr, int i, int last, int len);
    int main()
    {
    	//45 1 21 3 33 6 53 9 18 
        int array[9] = {45, 1, 21,3,33,6,53,9,18};
    
        printf("%d\n",max_seq(array,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);
    }

  5. #50
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    duh.. because you can just put 8 numbers into an array of size 9

    Code:
    #include <stdio.h>
    
    int max_seq(int *arr, int i, int last, int len)
    {
    	if(i > len)
    		return(0);
    	int eval = (i == 0 ? max_seq(arr+1, 0, 0, len-1) : 0);
    	int eval2 = max_seq(arr, i+1, (arr[i] > last ? arr[i] : last), len) + (arr[i] >= last ? 1 : 0);
    	return(eval > eval2 ? eval : eval2);
    }
    
    int main(void)
    {
    	int i[] = { 45, 1, 21, 3, 33, 6, 53, 9, 18};
    	printf("%d is the longest sequence in i\n", max_seq(i, 0, 0, 9));
    	return(0);
    }
    this works perfectly:

    Code:
    mk@mk:~$ gcc -o max_seq max_seq.c 
    mk@mk:~$ ./max_seq 
    5 is the longest sequence in i

  6. #51
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  7. #52
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    you're welcome

    I've had quite some fun thinkin about how to make the solution as short as possible. I'm a minimalist

  8. #53
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    And here's my 2c
    Code:
    #include <stdio.h>
    
    int mxstart(int [],int,int,int);
    int mxcnt(int [],int,int,int,int,int);
    
    int main()
    {
        int c = 1;
        int a[] = {9,1,3,0,7,10,11};
    
        c = mxstart(a,0,sizeof a / sizeof a[0],c);
        printf("max count is %d\n", c);
    
        return 0;
    }
    
    int mxstart(int a[],int i,int l,int c)
    {
        int p=1;
    
        if (i != l) {
            c = mxcnt(a,i,i+1,l,c,p);
            c = mxstart(a,i+1,l,c);
        }
        return c;
    }
    
    int mxcnt(int a[],int i,int si,int l,int c,int p)
    {
        if (si == l)
            return (a[si] >= a[i] ? p+1 : p);
        else {
            if (a[si] >= a[i])
                c = mxcnt(a,si,si+1,l,c,p+1);
            else
                c = mxcnt(a,i,si+1,l,c,p);
            return c;
        }
    }
    Last edited by itCbitC; 01-14-2009 at 04:32 PM.

  9. #54
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Quote Originally Posted by transgalactic2 View Post
    you cant use loops or external functions.
    you're using one of those

    anyway, I don't think its possible to make the function any shorter than 5 lines without making the code unreadable

  10. #55
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    do you mean sizeof??

  11. #56
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you use two functions in your solution
    its not allowed

  12. #57
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    nah. you have written two functions and the first one calls the second. If I understand that condition correctly, then the task was to have only a single function which calls itself.

    edit: damn, transgalactic2 was a couple of seconds faster

  13. #58
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059

    Thumbs up mkruk - clever solution

    Ah! thanks for the clarification; by external functions I thought the op meant the C lib functions.

  14. #59
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i didnt put 8 members into 9 place array
    i counted this array ten times that there are 9 members
    and it need to return 5 because of 1 3 6 9 18
    but it return 4


    why??
    Code:
    #include <stdio.h>
    int max_seq(int *arr, int i, int last, int len);
    int main()
    {
    	 
     	int i[] = { 45, 1, 21, 3, 33, 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-14-2009 at 05:26 PM.

  15. #60
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    I copyed and pasted your code and compiled it as it is shown. And it returns 5.

    Code:
    mk@mk:~$ cat test.c
    #include <stdio.h>
    int max_seq(int *arr, int i, int last, int len);
    int main()
    {
             
            int i[] = { 45, 1, 21, 3, 33, 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);
    }
    mk@mk:~$ gcc -o test test.c
    mk@mk:~$ ./test 
    5 is the longest sequence in i
    my function is perfectly fine

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