Recursive idea of array size n

Thread Starter

Ryan$

Joined Dec 14, 2018
178
Hi guys, I'm just wondering on something maybe it's simple but I miss understanding it because my preconviend idea are fault.

The recursive is something that we call the same function with smaller problem, my question is lets assume I have a function called draw(int arr[], left delimiter, right delimiter) , and I have an array of size 10 which is int arr[10]= {1,3,2,1,1,1,1,1,1,1}, I called inside the function draw() the function itself but with parameters draw(arr, 0, 2)
so now after called that function, I get new stack frame with parameters (arr, 0 ,2) so my new array inside the second call of that function is {1,1,1} , in that case if I write arr[3], what will be shown? a trash value? or just the value of the arr that I was having in the caller function(the primarily one) ? thanks in advance.
 

mckenney

Joined Nov 10, 2018
125
my new array inside the second call of that function is {1,1,1} , in that case if I write arr[3], what will be shown?
If you do something that is illegal (or "undefined") in the language, what happens depends on the language/implementation. (To mis-quote Richard Stallman: "One possibility is to start up a game of Rogue.")

The canonical recursive function has the form "f(size) {if (size==1) {actually do something} else f(size-1)". The "1"s are notional, but the idea is that it stops recursing (fixpoint) while you still have enough data to do something.

[Edit: Tried to add something helpful, and replaced an ambiguous term.]
 
Last edited:

WBahn

Joined Mar 31, 2012
30,076
Hi guys, I'm just wondering on something maybe it's simple but I miss understanding it because my preconviend idea are fault.

The recursive is something that we call the same function with smaller problem, my question is lets assume I have a function called draw(int arr[], left delimiter, right delimiter) , and I have an array of size 10 which is int arr[10]= {1,3,2,1,1,1,1,1,1,1}, I called inside the function draw() the function itself but with parameters draw(arr, 0, 2)
so now after called that function, I get new stack frame with parameters (arr, 0 ,2) so my new array inside the second call of that function is {1,1,1} , in that case if I write arr[3], what will be shown? a trash value? or just the value of the arr that I was having in the caller function(the primarily one) ? thanks in advance.
It depends on the meaning of the left and right delimiter within the functions, but if they are the indices of the first element and the last element that the function is supposed to consider, why would the effective array be {1,1,1} and not {1,3,2}.

What language are you talking about, because it can make difference?

In most languages you only have a single array and it has ten elements. If you write 9 to arr[3] then your array would not look like {1,3,2,9,1,1,1,1,1,1}. If you try to write beyond the array bounds, then what happens really depends on the language. Languages like C will let you write there and you may not notice any effect. But you may also change the value of a completely unrelated variable or violate the sandbox that the operating system gave you in which case it will likely kill your program. Other languages that do array-bounds checking would throw an exception giving you the opportunity to respond to the situation in a way that makes sense and, if you don't, then it will kill your program due to an unhandled exception.
 

402DF855

Joined Feb 9, 2013
271
so my new array inside the second call of that function is {1,1,1}
You haven't changed the array, only the delimiters. The array remains {1,3,2,1,1,1,1,1,1,1} and arr[3] is still 1. Perhaps you should provide actual code, as your description is not clear.
 

mckenney

Joined Nov 10, 2018
125
my new array inside the second call of that function is {1,1,1} , in that case if I write arr[3],
Yes, please try different words. I read this as: Your fixpoint is a 1x1x1 (3-D, 1-element) array, and you want to reference outside it, since you expect there is something there.
 
Top