Power recursion

Is it right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>

using namespace std;

int potenza(int x, int y)
{

if (x == 0 && y==0)
return -1;
if (x==0)
return 0;
if (y==0)
return 1;
else
return x*potenza(x,y-1);
}

int main()
{
double x;
cin>>x;
int y;
cin>>y;
cout<< potenza(x,y);
}
Last edited on
Is it right?

Yes, in some cases.
No for 00 = invalid,
No for 2-1 = fails.
But can I use this
1
2
if (x == 0 && y==0)
return -1;


My teacher made another exercise so usign return - 1 and return -2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46


#include <iostream>

using namespace std;

int divisione(int a, int b);



    int main(){

    int a,b;


        do{

            cin>>a ;
            cout<<endl;

            cin>>b;
            cout<<endl;
        }while(a < 0 || b < 0);


       cout<< divisione(a,b);
    }


    int divisione(int a, int b){

        if (a == 0  && b == 0)

            return -1;

        if(b == 0)
            return -2;

        if (a<b)
            return 0;


        return 1+divisione(a-b,b);

    }
Last edited on
My teacher made another exercise

First, I am not your teacher. If she/he stipulates conditions for your C++ training course that contradict (or partially differ from) results shown by your pocket calculator, you should know why.
Second, returning negative results as status indicator, bad habit but yes, why not. Use in all your routines the same value (for example -2 = Divide by 0) to see at once what went wrong.

Supplementation: The a. m. "results shown by your pocket calculator" is not a confident criterion to decide correct or not. Its result may also be mathematically questionable as yours.
Last edited on
I have seen this online:
"Since c does not support throwing exceptions when something was wrong, So programmers used the "return"as a way for a routine to sends some information to the parent routine that called it , it is considered to be an exit status , so mostly there was something of a convention that "0" meant success, a positive number meant minor problems, and a negative number meant some sort of failure. "
and also
"The return(-1) in C used whenever user want a negative return from the method, it means the the method return false value. "
So return -1 or -2 is simple something wrong... what do you think?
So return -1 or -2 is simple something wrong... what do you think?

To return -1 or -2 is too simple as error indicator for functions which could also return the same value as a valid result. For example your int potenza(int x, int y) allows x < 0 and as well y < 0. So (-1)3 = (-1)*(-1)*(-1) would result in an error because you define the result of -1 as Invalid data what is kind of "something wrong". The dual use of returned values as results and error indicator requires to avoid for sure any interference.
returning an error code is fine in some cases. Null pointer returned, for example, or -1 when the answer is always > 0 is fine, sure. But you MUST CHECK FOR that result EVERY TIME you use the function, which is ANNOYING to remember to do and a BUG RISK (if you forget to check). This design is used a lot, and it is a good useful pattern, but you have to use it correctly.


Here, why not fix the code?
It would not take much to make it work for a negative value and zero.
anything to zero power is 1, so check and return on that. If its negative, recurse with the abs of the value, and at the base case when you return check if the input was negative and handle it there. That code will only trigger on the initial call, since it will be positive in all the others, and you can fix it there.


anything to zero power is 1

Wrong. 00 has no restult. It is not a removable singularity.
true, you have to add if both are zero check. good catch, but you can add that check in as well. Return a nan code if its for doubles there. For ints, there isn't a clean answer.
Last edited on
Topic archived. No new replies allowed.