Thread has been deleted

Last comment
I have this exercise that i have almost no clue how to solve.
here is the function: gyazo.com/646a662e04e2efdb110623b3ed2f5e..
and then we have this array:
int arr[] = {1, 5, 3, 4, 3, 2};
and im supposed to figure out what return i get from:
arrayFunc (6, arr)
we are supposed to do solve this on paper with pen only.

did you learn recursion yet?

yes but no. i dont understand it well...

basically the function calls itself
int arr[] = {1, 5, 3, 4, 3, 2};
first pass, 2 + ... (# of elements = 5 now)
second pass 3 + ... (# of elements = 4 now)
third pass 4 + ... (# of elements = 3 now)
fourth pass - since 3 < 4, it is arr[0], which is 1
2+3+4+1=10

i understand it starts with 2+ but how do you get 3+ and 4+?

bc imagine its calling the function again, this time with #ofelements variable as the previous value - 1, making the index one less each time, therefore 3, then 4

ahh okay now i think i understand. thank so much man, this might save me.

np mens))

10 i think

but can you tell me how you solved it plz???

#12

its recursion, you see in +arrayFunc(nrofElements-1, array) you call the function again.
So at the beginning you say
return array[nrOfElements-1] which is 5, so the value 2
then + arrayFunc(nrofElements-1, array) so you do the same but with 5 instead of 6
in the function call with 5 now the same happens, also here call the function again
you do this until the value you called the function with is < 4
at this point return array[0] which is 1, and from there on all the values in the methods are returned
so all in all its return 2 + 3 + 4 + 1

thank you so much dude

it will give arr[5] + arr[5] = 2 + 2 = 4

This is correct

mens

mens dont confuse him

Yes

it looks like it would return
2 + 3 + 4 + 1
so 10

can you explain more please? the "2" comes from array[nrOfElements-1] right? but the the second part i dont understand how to calculate.

arrayFunc is entered for the first time
nrOfElements starts at 6
hit the first if statement, check is nrOfElements is less than 4; it isn't
instead, return array[5] (which is 2) and the arrayFunc
when the arrayFunc is 'returned' in this way, it is called recursion
no return actually occurs yet; instead, the arrayFunc in the return statement is executed
so now arrayFunc is executed again with the new parameters (nrOfElements = 5, array[] = array)
check if nrOfElements is less than 4; it isn't
return array[4] (which is 3) + arrayFunc but now nrOfElements is 4
check nrOfElements again, if statement fails again
return array[3] (which is 4) + arrayFunc but now nrOfElements is 3
check the if statement; it enters! return [0] (which is 1)
return occurs; enter the previous arrayFunc (back when nrOfElements was 4)
add array[0] to array[3] to get 5; function ends, return to previous arrayFunc
add array[4] to 5 and get 8; function ends, return to previous arrayFunc
add array[5] to 8 to get 10; function ends, return to original caller with the 10
/done

thanks man

stfu nerd

+1

im curious about those studying programming. what job in particular will you be in?

You develope skynet and terminator