Thread: Help!!

  1. #1
    Unregistered
    Guest

    Unhappy Help!!

    i've just started learning C language from two weeks ago (and its the first programming language that i learn!!)
    i have to do the following assignment:
    Write a program that computes the value of e^x by using the formula:
    e^x = 1 + x/1! + x^2 / 2! + x^3 / 3! +.......

    i wrote the following program but i keep receiving error!

    #include <stdio.h>
    #include <math.h>
    int main ()
    {
    float x, result,fact=1; int power=0,a=1;
    printf ("Enter the value of X");
    scanf ("%f",&x);
    result= 0;
    fact=power;
    while (power <= 3){
    ++power;

    while ( (power-a) >= 1){
    fact= fact * (power-a);
    ++a;
    }
    result = (result + pow(x,power)) / fact;
    if (power >= 3) {
    printf ("the result is %f",result + 1);
    break;
    }
    }
    return 0;
    }

    thanx for any help and sorry for my bad english.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > i wrote the following program but i keep receiving error!

    You know, well obviously you don't, it is often USEFUL to actually have the error message. I'm going going to read all of your code and sit around and figure out what error you're getting. If you want help, be helpful and provide us with the information we need to be of assistance.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    Seems to work OK for me (under Dev C++)
    Visit entropysink.com - It's what your PC is made for!

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I compiled and ran it through gcc, and while it compiles the output is always the same.
    The world is waiting. I must leave you now.

  5. #5
    Unregistered
    Guest

    Unhappy

    its me again!
    i receive the following error:
    The exception Floating-point division by zero.
    (0xc0000008e) occured in the application at location 0x004011d6

    i am using Borland C++ Builder 5.5
    thanx for trying to help me.

  6. #6
    Unregistered
    Guest

    Unhappy

    i changed power=0 to power=1 and now it works fine, but the output is wrong!!!!
    please HELP!

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >result= 0;
    >fact=power;

    Now fact == 0.

    >while (power <= 3){
    >++power;
    >while ( (power-a) >= 1){
    >fact= fact * (power-a);
    >++a;
    >}

    Here fact == 0, since 0 * A = 0, whatever the value of A is.

    >result = (result + pow(x,power)) / fact;

    This all results in a division by 0.

Popular pages Recent additions subscribe to a feed