Help with pow()

This is a discussion on Help with pow() within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> int main() { int base, exp; scanf("%d", &base) ; scanf("%d", &exp) ; printf("%d ^ %d ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Help with pow()

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
           int base, exp;
           scanf("%d", &base) ; 
           scanf("%d", &exp) ; 
           printf("%d ^ %d = %d\n", base, exp, pow(base,exp)) ; 
           return 0;
    }
    The above code prints the output of pow as a 0. What did I do wrong? I tried %f also, but that gave me a number with alot of zeroes after the decimal. How can I get a normal integer without casting pow? Thank you.

  2. #2
    JDD
    JDD is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Quote Originally Posted by jedi_jinn
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
           int base, exp;
           scanf("%d", &base) ; 
           scanf("%d", &exp) ; 
           printf("%d ^ %d = %d\n", base, exp, pow(base,exp)) ; 
           return 0;
    }
    The above code prints the output of pow as a 0. What did I do wrong? I tried %f also, but that gave me a number with alot of zeroes after the decimal. How can I get a normal integer without casting pow? Thank you.
    Code:
    printf("%i ^ %i = %i\n" , base, exp, pow(base, exp));
    Try that.
    Or, you can always error check.

    Code:
    int main()
    {
           int base, exp, powr;
           scanf("%d", &base) ; 
           scanf("%d", &exp) ; 
           
           powr = pow(base, exp);
           
           if(powr != 0)
           {
                printf("%i ^ %i = %i\n", base, exp, powr) ;        
           }
           else
           {
                printf("Error calculating pow()");
           return 0;
    }
    Last edited by JDD; 10-09-2006 at 01:50 AM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by jedi_jinn
    What did I do wrong?
    ^ is an exlusive or.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    JDD
    JDD is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Quote Originally Posted by Dave_Sinkula
    ^ is an exlusive or.
    Oh thats right, the only way i can think of printing it, would be to...

    Code:
    int main()
    {
           int base, exp;
           double powr;
           char expc;
           scanf("%i", &base) ; 
           scanf("%i", &exp) ; 
           
           powr = pow(base, exp);
           
           if(powr != 0)
           {
                printf("%i %c %i = %d\n", base, expc, exp, powr);        
           }
           else
           {
                printf("Error calculating pow()");
           return 0;
    }
    Last edited by JDD; 10-09-2006 at 02:05 AM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    What is the return value of pow? [Hint: it's not an int, with which %d corresponds.] Have you ever looked up what %g does?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    JDD
    JDD is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Quote Originally Posted by Dave_Sinkula
    What is the return value of pow? [Hint: it's not an int, with which %d corresponds.] Have you ever looked up what %g does?
    It returns a double doesn't it? ;x

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Oh thanks for the responses

    Do you know where I can find information on format descriptors like %d, %g, %e, %f?

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Google. It's free, fun, and fascinating. Do a Google today!
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the difference between pow() vs shift?
    By Overworked_PhD in forum C Programming
    Replies: 9
    Last Post: 01-06-2008, 01:04 PM
  2. trying to use pow()
    By Mikecore in forum C Programming
    Replies: 9
    Last Post: 10-09-2005, 07:30 PM
  3. alternatives to pow()
    By mcdms in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2004, 10:42 PM
  4. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 09:46 PM
  5. Problem with pow()
    By RMacFly in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21