Making a function x^n

This is a discussion on Making a function x^n within the C Programming forums, part of the General Programming Boards category; I'd like to be able to make a function x^n, where x is a float, and n is a positive ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    Making a function x^n

    I'd like to be able to make a function x^n, where x is a float, and n is a positive integer.

    What about something like this?:

    Code:
    int a;
    float x_n=1;
    float nthpower(float x, int n)
      {
       for(a=0;a<=n;a++);
       x_n=x_n*x;
      }
    Last edited by bertazoid; 10-21-2008 at 06:24 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Except a and x_n need to be defined inside the function, rather than at global scope. You also need to return your answer. And your for-loop goes one too many times.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is already such a function, called pow().
    First of all what you do is wrong. you would want:
    Code:
    float nthpower(float x, int n)
    {
       int a;
       float x_n=1;
       for(a=0;a<n;a++);
       x_n=x_n*x;
       return x_n;
    }
    1) You need int a, float x_n inside the function
    2) You ned < not <=
    3) You need of course a return;
    4) You need to check for n begin negative. If it's 0 it's OK
    5) There are might be more optimized ways to do so
    Last edited by C_ntua; 10-21-2008 at 06:40 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
       for(a=0;a<n;a++);
       x_n=x_n*x;
    The semicolon above is wrong too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by tabstop View Post
    Except a and x_n need to be defined inside the function, rather than at global scope. You also need to return your answer. And your for-loop goes one too many times.
    Ok, is this better?

    Code:
    float nthpower(float x, int n)
      {
       int a;
       float x_n=1;
       for(a=1;a<=n;a++)
       x_n=x_n*x;
       return x_n;  
       }
    And I can have an if function in there to see whether n is positive.
    Last edited by bertazoid; 10-21-2008 at 06:44 AM.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by bertazoid View Post
    Ok, is this better?

    Code:
    float nthpower(float x, int n)
      {
       int a;
       float x_n=1;
       for(a=1;a<=n;a++);
       x_n=x_n*x;
       return x_n;  
       }
    Yeah except the simicolon has to go away!

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by C_ntua View Post
    There is already such a function, called pow().

    Ahh ok, so thats part of <math.h> ? I'm enjoying learning how to make such functions anyway

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by C_ntua View Post
    Yeah except the simicolon has to go away!
    lol I've never known such a fast forum, it's like instant messaging, by the time I have edited/posted a reply, there is already more replies. Not complaining though, other forums I've been to have been dead. And thanks all for the help.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you can solve it by recursion:
    Code:
    power (int x,int n) {
    
    if  (n==0){
    
    return 1;
    }
    else
    {
    return power(x,n-1)*x;
    }
    }

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant understand where is my bug in this code
    Code:
    #include <stdio.h>
    power (int x,int n);
    
    int main()
    {           
    
    
    printf("&#37;d",power(2,1);  //i got a bug in this place i dont know why
    
    return 0;
    }
    int power (int x,int n) {
    
    if  (n==0){
    
    return 1;
    }
    else
    {
    return power(x,n-1)*x;
    }
    }

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you mean the missing ) on the printf, or something else? A "bug" in my mind is not a typo that the compiler can detect, but something that causes incorrect behaviour (result, sequence of events, etc) compared to what you expect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by transgalactic2 View Post
    you can solve it by recursion:
    Code:
    power (int x,int n) {
    
    if  (n==0){
    
    return 1;
    }
    else
    {
    return power(x,n-1)*x;
    }
    }
    Thanks

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    oohhh such a foolishness
    sorry

    my recursion works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

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