power function

This is a discussion on power function within the C Programming forums, part of the General Programming Boards category; ok so my Proffessor wants me to write a program that computers the power function using "for" to controll the ...

  1. #1
    Unregistered
    Guest

    power function

    ok so my Proffessor wants me to write a program that computers the power function using "for" to controll the calculations with out using the math.h library. and i have no idea how to start. any help would be appreciated.

    thanks
    jeff

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    2

    Power using for

    This code is not the faster but i think that it is very simple.

    #include <stdio.h>

    int main()
    {
    int pow,i;
    double base,result;
    result=1;
    pow=GetInteger();
    base=GetInteger();
    for (i=1 ; i<=pow ; i++)
    result=result * base;
    printf("%g", result);
    return 0;
    }

  3. #3
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    Posts
    144
    here's the something that works, but realize that homework is there to help you learn. Giving up is never going to get you anywhere. You didn't show much effort. it's actually a policy of the board that homework should not be posted. enough lecture, but i think you could have solved this one.

    Code:
    double power (double base, int pow) 
    {
        if ( pow == 0 )
            return 1;
        
        double result = base;
    
        for (int i = 1; i < pow; i++) 
            result *= base;
        
        return result;  
    }
    i'll let you figure out negative exponents...
    Last edited by mix0matt; 11-05-2001 at 06:03 PM.
    THIS IS NOT JUST A CHRONICLING OF THINGS WE HAVE DONE IN THE PAST BUT OUR RISE TO POWER.

  4. #4
    free(me);
    Join Date
    Oct 2001
    Location
    Santo Domingo, DN, Dominican Republic
    Posts
    98

    Here's another interesting little way to do it...

    Code:
    float power(float m, float n)
    {
        if(n == 0)
            return 1;
        else
            m *= power(m, (--n));
            return m;
    }
    It's so much more intricate, and yet it's not really complex. It's just recursive. This was actually of my first programs, i've always had the inexplicable tendency of making things more complex than they really are or should be, of doing things the harder way.

    Oh well,
    biterman.
    Do you know how contemptous they are of you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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