Recursive function.
#1

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
Reply
#2

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.
Reply
#3

Quote:
Originally Posted by Harold
View Post
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
Reply
#4

Quote:
Originally Posted by Zh3r0
View Post
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?
Reply
#5

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...
Reply
#6

Return 1 Will FIX EVERYTHING
Reply
#7

Quote:
Originally Posted by DaTa[X]
View Post
Return 1 Will FIX EVERYTHING
No, still crashes.
Reply
#8

Quote:
Originally Posted by Zh3r0
View Post
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.
Reply
#9

Quote:
Originally Posted by Vince
View Post
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 ...
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)