2

I have been working on some basic coding and I'm struggling trying to figure out the right way to scan to integers in, the first being my x variable and second being the n which x is being raised to. I try 5^5 and get a -287648 back with my current code.

#include <stdio.h>
#include <math.h>

void x_to_the_n (void)
{
    int x=0;
    int n =0;
    long int y;
    printf("Enter a integer for X and N\n");
    scanf("%i\n%i\n",&x,&n);
        y=pow(x,y);
        printf("%i \n",y);
}

int main(void)
{
    x_to_the_n ();
    return 0;
}   
2
  • 1
    You need to have better variable names, so you don't get mixed up
    – user195488
    Mar 13, 2013 at 19:07
  • 2
    I think you are using y where you intend to use n, correct? Mar 13, 2013 at 19:07

3 Answers 3

9

I'm pretty sure you mean:

y = pow(x, n);
          ~~

You're getting a "weird" result because y is never initialized to anything; you are raising x to the power of (some garbage) and getting garbage out.

Note that, as @0A0D suggests in a comment, if you were to use more descriptive variables, this problem would be much more obvious:

int base = 0;
int exponent = 0;
long int result;
printf("Enter the base and exponent, on separate lines\n");
scanf("%i\n%i\n", &base, &exponent);
result = pow(base, result);
                   ~~~~~~~ oops!

Also, as @icepack has mentioned, since y is a long int, the format should be %li (not %i):

printf("%li\n", y);
3
  • 1
    Given that, the printing of y is also incorrect. Since it's a long int, the format should be: printf("%li \n",y); Mar 13, 2013 at 19:14
  • Better yet: "Enter in the base, hit enter and then the exponent" or something to that effect.. not sure if enter is interpreted as \r\n or just \n
    – user195488
    Mar 13, 2013 at 19:17
  • @0A0D Agreed. Edited. (I think the \r\n vs \n would depend on the OS?)
    – lc.
    Mar 13, 2013 at 19:18
0

You are using y that is not initialized as pow() function argument.

This causes overflow of the result.

-2

If you need a function to do integer exponentiation you could try something like this

#include <stdio.h>
unsigned int_pow(int n, unsigned x);
int main(void)
{
    int n, x;    
    printf("Enter an integer (n) followed by the power (x) to raise to: ");
    scanf("%i %i", &n, &x);    
    printf("%i^%i = %i\n", n, x, int_pow(n, x));    
    return 0;
}
unsigned int_pow(int n, unsigned x)
{
    unsigned i, a = n;
    switch (x)
    {
      case 0:
        return 1;
      case 1:
        return n;
      default:
        for (i = 1; i < x; ++i) {
             a *= n;
        }
        break;
    }
    return a;
}

you might want to add some code to check for bad values passed to the function. large exponents will overflow the unsigned int data type. you could write the function to return an unsigned long and take an unsigned long for the formal parameter x to handle larger numbers but if you know you will be handling large numbers you should probably be using float types.

if you know the returned value has to be used as an integer type then this code works well and saves any type conversion.Hope it helps.

1
  • 1
    This doesn't appear to answer the actual problem at hand. Dec 22, 2014 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.