Yes, that is a perfectly good way of doing recursion. However as usual, I'd like to point out that you should use prefix increment. Also, my inner grammar nazi is forcing me to tell you that you used the wrong "practise", the verb has an s and the noun has a c.
I see no need for the increment operator being used on start here as the variable is not being used after the recursive call is made.
1 2 3 4 5 6 7 8
void addRec(int a[], int start, int end)
{
if(start < end) // invert logic to avoid unnecessary return
{
++a[start]; // use increment operator here (as just adding 1)
addRec(a, start + 1, end); // but just just add 1 here
}
}
Andy
PS Regarding
my inner grammar nazi is forcing me to tell you that you used the wrong "practise", the verb has an s and the noun has a c.
And Google gives me 96,000,000 for "practicing" (with quotes) but just 16,600,000 for "practising", so it looks like the latter form is dying.
I'm sorry, but I personally believe that the British version (where there is a difference) is the correct version here because they are different words and the general rule is that words like practice/practise and licence/license follow the same pattern as advice/advise. Maybe it is dying, but in my opinion, that is more of a reason to start using it right because not using it right becomes ambiguous.