Recursive problem
-
Hi. Suppose i have a function that takes an array and its length. That function should search the entire array for each element and return a result. example:
int rec(int arr[], int length) { if(length == 0) return 0; //search array for element length - 1; searchingarr(arr,length -1); rec(arr[],length - 1); }
Now . i cant use loops(while or for), no static variables. only one function inside the rec and use only recursive. i cant put a sign in the array that points to the end either. the problem here is that i want that each time im calling "searchingarr" i paste him the length of the array. here, the length is substracted by 1 each time i call rec. -
Hi. Suppose i have a function that takes an array and its length. That function should search the entire array for each element and return a result. example:
int rec(int arr[], int length) { if(length == 0) return 0; //search array for element length - 1; searchingarr(arr,length -1); rec(arr[],length - 1); }
Now . i cant use loops(while or for), no static variables. only one function inside the rec and use only recursive. i cant put a sign in the array that points to the end either. the problem here is that i want that each time im calling "searchingarr" i paste him the length of the array. here, the length is substracted by 1 each time i call rec.Could you restate your question exactly? What is the function looking for? What is it supposed to return?
-
Could you restate your question exactly? What is the function looking for? What is it supposed to return?
the function(searcharr) should take an element and check if there is a sequence of summed elements that is equal to the index of that element. example arr{ 1 , 3, 2 , 0 , 0 , 3} arr[2] = 3. because 2(i=2) + 0(i=3) + 0(i=4) gives us the value 3 that is the length of the sequence that when summed gives us the value of the index(2).
-
the function(searcharr) should take an element and check if there is a sequence of summed elements that is equal to the index of that element. example arr{ 1 , 3, 2 , 0 , 0 , 3} arr[2] = 3. because 2(i=2) + 0(i=3) + 0(i=4) gives us the value 3 that is the length of the sequence that when summed gives us the value of the index(2).
But arr[2] = 2, not 3.
-
But arr[2] = 2, not 3.
arr{ 1 , 3, 2 , 0 , 0 , 3} the index 2 in the array have a value that is equal to the sequence of 3 elements(2 , 0 , 0) thus the returning value is 3.
-
arr{ 1 , 3, 2 , 0 , 0 , 3} the index 2 in the array have a value that is equal to the sequence of 3 elements(2 , 0 , 0) thus the returning value is 3.
So what is your algorithm? Can you write the algorithm in words?
-
So what is your algorithm? Can you write the algorithm in words?
[Message Deleted]
-
So what is your algorithm? Can you write the algorithm in words?
It looks like homework.
Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.