# my Quiz

Printable View

• 01-14-2009
hanadi
my Quiz
hi all

today I had a quiz in template and recursion
it was easy I did will , but when I tried to solve the questions in VB it gave me different answers I will not say my answers untill I see urs to compare them with mine.
so help me pleas to find where the errors, is it from me or the compiler?

Code:

```#include<iostream> using namespace std; int sum(int m,int n){         if (m==0)         return n+1; if (n==0) return sum (m-1 , 1); else  sum (m-1 , sum ( m , n-1)); cout<<"done"; } int main(){ cout<<sum(2,5);         return 0;}```
• 01-14-2009
Elysia
Oh my goodness, dude, learn to indent!
• 01-14-2009
anon
What's it supposed to calculate? 2 + 5? Note that in some cases sum doesn't return anything, so it can't give good results.
• 01-14-2009
Salem
Mmm, looks like this - http://en.wikipedia.org/wiki/Ackermann_function
So (apart from the coding error anon spotted), lets say arithmetic overflow, or you blow the stack.
• 01-14-2009
DaveH
Quote:

Originally Posted by hanadi
hi all

today I had a quiz in template and recursion
it was easy I did will , but when I tried to solve the questions in VB it gave me different answers I will not say my answers untill I see urs to compare them with mine.
so help me pleas to find where the errors, is it from me or the compiler?

Code:

```#include<iostream> using namespace std; int sum(int m,int n){             if (m==0)         return n+1;     else if (n==0)         return sum (m-1 , 1);     else         sum (m-1 , sum ( m , n-1)); cout<<"done"; } int main(){     cout<<sum(2,5);     return 0;}```

Notice the bolded item that you missed.

Also, the only output here is "done".
• 01-14-2009
CornedBee
All elses in the function are redundant, since the then branches return early.