Thread: finding the largest ascending subsequence..

  1. #31
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by matsp View Post
    And how do you propose we solve that problem?

    Of course, you can remove ALL of the prev variables, as they are just set, never referenced.

    You can also remove best if you only want to find ONE max value.

    --
    Mats


    can you propose a practical solution
    ??

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    can you propose a practical solution
    ??
    Yes. You need to:
    1. Understand how "find largest sequence" works.
    2. Understand how you translate a looping piece of code into a recursive solution.

    I have a strong feeling that you are working very hard on getting others to solve the problem bit by bit. That method will NEVER teach you how to solve the problem yourself, which you eventually will have to do.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #33
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i have done a big peace of work myself doing this base code
    and i would understand how "find largest sequence" works
    if i had something to learn it from

    i got this base code and i dont know what to do next
    Code:
    #include <stdio.h>
    int max_set(int arr[], int size,int index,int bef);
    int main()
    {
        int array[7] = {9, 1, 3,0,7,10,11};
    
        printf("%d\n",max_set(array,7,0,array[0]));
        return 0;
    }
    
    int max_set(int arr[], int size,int index,int bef) 
    {
    	int temp,bigger;
       if (index==size)
       {
         return 1;
       }
       if (arr[index]>bef)
    	    {
         return 1+max_set(arr,size,index+1,arr[index]);
       }
       else
       {
         return max_set(arr,size,index+1,bef);
       }
           
    }//end of function

  4. #34
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i could write an iterative code
    that solves it using external loop

    do you know how to transform this code into the single function recursion??

  5. #35
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the general solution
    so it will be done from 1 function??

  6. #36
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in what point in this recursive function
    i can put the value that it returns into a variable??

  7. #37
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    This appears to be a similar topic.

    http://cboard.cprogramming.com/showthread.php?t=108515

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but it's not "how to solve this problem" that is the question here, but how to solve it recursively.

    I'm not sure if transgalactic have a teacher that still wishes that he (she) was using lisp or some other purely functional language where recursion is the natural method of solving ALL repetitions, but I personally think it's a bad idea to try to solve all problems [as one function, at least] via recursion.

    Like Salem says, it's like "Can you tie your shoelaces with one hand" - it has little practical use in real life [except for those relatively few people who for some reason can't use both hands for daily tasks, e.g. from birth defects, accidents, etc].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #39
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in what point in this recursive function i can store the
    value that it returns into a cell of an array
    ??

    it appears that there is no point in the code of this recursion where there i can
    store the total result that it returns

    ??

  10. #40
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to insert the resolt into a cell of an array

    but its not working

    Code:
    #include <stdio.h>
    int max_set(int arr[], int size,int index,int bef);
    int main()
    {
        int array[7] = {9, 1, 3,0,7,10,11};
        int b[1];
    	b[0]=0;
        printf("%d\n",max_set(array,7,0,array[0],b[1]));
        return 0;
    }
    
    
    int max_set(int arr[], int size,int index,int bef,int b[]) 
    {
    	int temp,bigger;
       if (index==size)
       {
         return 1;
       }
       if (arr[index]>bef)
    	    {
         return 1+max_set(arr,size,index+1,arr[index],b[1]);
    	 b[1]=max_set(arr,7,0,arr[0],b[1]);
       }
       else
       {
         return max_set(arr,size,index+1,bef,b[1]);
       }
           
    }//end of function

  11. #41
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i got this crude code

    ??
    Code:
    int max_set(int arr[], int size,int index,int bef,int t_set) 
    {
         int temp;
    	 if (index==size)
    	 {
            return t_set;
    	 }
    	 temp=max_set(arr,size,index+1,bef,t_set)
         if(temp>t_set)
    	 {
            return temp;
    	 }
    	 else
    	 {
            return t_set;
    	 }
    
    
    
    }//end of function
    but i dont know how to integrate in it
    this base code
    ??
    Code:
    int max_set(int arr[], int size,int index,int bef) 
    {
    	int temp,bigger;
       if (index==size)
       {
         return 1;
       }
       if (arr[index]>bef)
    	    {
         return 1+max_set(arr,size,index+1,arr[index]);
       }
       else
       {
         return max_set(arr,size,index+1,bef);
       }
           
    }//end of function

  12. #42
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The posted code is oversimplified in the sense that you have a mind boggling number of cases to consider before the recursive function can even return from processing a single set.

  13. #43
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what you advise to do
    in order to combine them?
    and to get the largest set

  14. #44
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post the latest 'n greatest code you have currently. As stated previously each frame of a recursive function that is pushed onto the stack is a link in the chain so imo don't try to solve bits and pieces of it separately and expect them all to somehow automagically work together because that will invariably break linkages across the frames.

  15. #45
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    oh god you can't be serious

    I tried to implement it and it isnt very difficult. My implementation is all recursive, exactly as you require it to be and its just 5 lines of code (not counting the head and the two braces).

    I admit, I had to think about it a couple of minutes but it works with returning nothing but an int.
    Anyway, I don't want to spoil the party so im just posting the function header

    Code:
    int max_seq(int *arr, int i, int last, int len)
    {
    
    }

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