Hey im trying to refresh my mind with a bit of recursion.
I want to add all numbers from ‘start’ to ‘end’ inclusive.
I.e if start was 1, and end was 5. Then the answer would be 1+2+3+4+5 = 15
So far I’ve got this
int calc(int start, int end){
if(start > end)
return total;
else{
total = total + start;
return sum1(start++, end);
}
}
Its not working (i get seg fault). What am i doing wrong?
EDIT: Sorry i am using the same variables in my actual code, when i wrote this i ended up reffering to them as start/end and forgot to change all the code.
What are
fromandtovariables inside your function? Maybe you use some globals instead of usingstartandendand that’s why you have the problem? Also why are you usingsum1inside thecalcfunction instead ofcalc?Try this instead: