help: find max sub series

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sedaw
    New Member
    • Jan 2009
    • 36

    help: find max sub series

    hello !

    i need to find the longest sub series by recursion , without use loops .

    for example :

    if the series:

    arr = 45 1 21 3 33 6 53 9 18
    the sub series are:
    45 53
    1 21 33 53
    1 3 33 53
    1 3 6 53
    1 3 6 9 18
    1 33 53
    1 6 53
    1 6 9 18
    1 53
    1 9 18
    1 18
    21 33 53
    …….

    and the longes sub series is 1 3 6 9 18

    the function will return max = 5



    this is my work , i`ve got some syntax error:

    I would really appreciate any kind of help .....
    =============== =============== =============== ============






    int max_set(int arr[], int size, int state=1 ,int i=0, int max=0)
    {
    printf ("Please enter the size of the array\n");
    scanf("%d",size );
    if (size<=0)
    return(0);
    else
    printf ("Please enter "%d" values for the array\n", size);
    gets (arr);
    check_arr_value s(arr, size, state, i);
    if (max<=state)
    max=state;

    max_set(arr, size, state=1, i++, max);

    printf ("%d", max);
    return(state);
    }


    int check_arr_value s(int arr[], int size, int state, int i)
    {
    if ((arr[i]<=arr[i+1])&&(i<=size))
    state++;
    check_arr_value s(arr, size, state, i++);
    }

    #include<stdio. h>
    main()
    {
    }
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Try moving the #include up to the very top of your program. You need to have stdio.h included before you use stuff like printf, which is defined in stdio.h. In general, put includes at the top of your programs. Hope this helps, and please use code tags around your code. Put [CODE] before it and [/CODE] after it so it shows up in a code box. Thanks.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      I suggest you get this working by using a loop. Any recursion is replaceable with a loop. After it works, the guts of the loop become your recursive function and the loop controls are moved to the top of the function to stop the recursion at the correct point.

      Comment

      • Bassem
        Contributor
        • Dec 2008
        • 344

        #4
        I do think the same as weaknessforcats said.
        Recursive functions add overhead for your program, and only used when it make the code more readable and get more to reality.
        And it can be transform between them.
        Last edited by Bassem; Jan 10 '09, 07:40 PM. Reason: name correction

        Comment

        Working...