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.
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
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??
what is the general solution
so it will be done from 1 function??
in what point in this recursive function
i can put the value that it returns into a variable??
This appears to be a similar topic.
http://cboard.cprogramming.com/showthread.php?t=108515
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.
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
??
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
i got this crude code
??
but i dont know how to integrate in itCode: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
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
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.
what you advise to do
in order to combine them?
and to get the largest set
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.
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) { }