Recursive function. -
Zh3r0 - 04.12.2014
So I have this(which is a code written from e pseudocode from our last class) C little recursive function.
pawn Code:
int power3(int x, int n);
int power3(int x, int n)
{
int p;
if(n == 2)
return (x * x);
else
p = power3(x, (n/2));
return (p * p);
}
Now to check it in main()
pawn Code:
printf("%d", power3(2, 24));
It never calls power3 again, checked it using printf's before and after, it repeats endlessly the before printf.
pawn Code:
else
{
printf("B. %d", p);
p = power3(x, (n/2));
printf("A. %d", p);
}
And it crashes my console and stops working.
Ani ideas? Also, I would like to ask you guys for a coding forum, so I can stop bothering you guys
Re: Recursive function. -
Smithy - 04.12.2014
Consoles usually crash due to never-ending loops. I only know Java so I don't know how much help it is, but as far as I know, using and if statement cannot cause a loop. If it repeats the else constantly then you are not meeting the criteria of if(n == 2)
What is it trying to do?
Also, why have you defined int power3(int x, int n) twice? Once without a ; at the end.
Re: Recursive function. -
Zh3r0 - 04.12.2014
Quote:
Originally Posted by Harold
Consoles usually crash due to never-ending loops. I only know Java so I don't know how much help it is, but as far as I know, using and if statement cannot cause a loop. If it repeats the else constantly then you are not meeting the criteria of if(n == 2)
What is it trying to do?
Also, why have you defined int power3(int x, int n) twice? Once without a ; at the end.
|
It was just a declaration so I can write the code below main(), don't even know why I pasted that too, ignore it.
I don't even know what my teacher tried to show us, cause I can't make out what that function should do
Re: Recursive function. -
KingHual - 04.12.2014
Quote:
Originally Posted by Zh3r0
Now to check it in main()
pawn Code:
printf("%d", power3(2, 24));
|
you do know signed int32 can have a value of -2^16 to 2^16 and you're trying to calculate 2^24, right?
Re: Recursive function. -
Zh3r0 - 05.12.2014
Thing is, we'll get this in our exams, so I must study it no matter how poor the performance, the pseudocode doesn't stop either, I sometimes wonder how the hell these teachers teach...
Re: Recursive function. -
DaTa[X] - 05.12.2014
Return 1 Will FIX EVERYTHING
Re: Recursive function. -
Zh3r0 - 05.12.2014
Quote:
Originally Posted by DaTa[X]
Return 1 Will FIX EVERYTHING
|
No, still crashes.
Re: Recursive function. -
Vince - 05.12.2014
Quote:
Originally Posted by Zh3r0
Thing is, we'll get this in our exams, so I must study it no matter how poor the performance, the pseudocode doesn't stop either, I sometimes wonder how the hell these teachers teach...
|
That seems incredibly counterproductive. As far as I can tell, all that functions does is raise a number to the power of
n, which is indeed better done in a loop.
Re: Recursive function. -
Zh3r0 - 05.12.2014
Quote:
Originally Posted by Vince
That seems incredibly counterproductive. As far as I can tell, all that functions does is raise a number to the power of n, which is indeed better done in a loop.
|
I just followed a pseudocode, which was supposed to be written good, therefore I tested it, I know there are better methods, I think my teachers are a bit drunk cause I've discovered a while back, in a class .pdf that they added a function to sum up 2 numbers, creating a new variable, using a loop, incrementing that variable, and then returning 1! It was just pathetic considering the "return x + y;" which could've been used ...
Re: Recursive function. -
Smithy - 05.12.2014
If the aim was to add two values together until you get X value, they're correct in saying a loop would of been easier. It seems as though your teachers may of indeed been drunk.