Thread: for

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Question for

    if limit is 4

    how to use for calculat this program?


    #include<stdio.h>

    int main(void)
    {
    double tpt =0;
    int i, limit;

    for(i=0; i<=limit; i++)
    {
    1 1 1 1 1
    tot += - + - + - + - + -
    1*1 1*2 1*2*3 1*2*3*4 (limit-1)
    (4-1)
    1*2*3
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1 1 1 1 1 tot += - + - + - + - + - 1*1 1*2 1*2*3 1*2*3*4 (limit-1) (4-1) 1*2*3
    What is this? This isn't even *close* to a valid expression.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Hmmm, saw this post too late. There was another posting by you with this question. Can you tell us in words what you try to do?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Perhaps you should set limit to a value before comparing it.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    Question for

    Euler's equation

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > Euler's equation

    Um... okay.... Still doesn't explain your problem... Almost nothing in your code is valid C.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    "Bad programming = Compiler errors" -- Euler's equation.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    What's wrong in this program?
    How to fix it?

    use loop to terminates when the difference between
    two succesive values of e differ by less than
    0.0000001

    #include<stdio.h>

    void getLimit(int *);
    void calculation(int);

    int main(void)
    {
    int limit;

    getLimit(&limit);
    calculation(limit);

    return 0;
    }

    /**************************GET LIMIT********************/

    void getLimit(int *limit)
    {
    printf("Enter limit: ");
    scanf("%d", limit);

    return;
    }

    /**************************CALCULATION************* *******/

    void calculation(int limit)
    {
    int i, j, p, flag=0;
    double sum, diff, psum=1;

    for(i=1; i<limit; i++)
    {
    p=1;

    for(j=1; j<=i; j++)
    p=p*j;

    sum=psum + (1/p);
    diff=psum-sum;

    if(diff < 0.0000001)
    {
    flag=1;
    break;
    }

    if(flag==1)
    printf("Prev-next:%.2lf %.2lf %.2lf\n", psum, sum, diff);
    else
    printf("Num of iteration: %d, e=%.5lf\n", limit, sum);
    }
    return;
    }

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Your program looks a bit unstructured. Take some more effort in designing your algorithm.

    Second, there are many formula's of Euler. But I think your pointing at the formula to calculate e.

    e = sigma (x=0->inf) (1 / x!)

    Which you need to implement as

    e = sigma (x=0->limit) (1 / x!)

    Which is

    e = 1 / 0! + 1 / 1! + 1 / 2! + ... + 1 / limit!

    As you can see there are two main calculations to be made

    1. Sum
    2. Faculty

    A sum can be calculated by using a for-loop and faculty can be a function:

    Code:
    e = 0
    for (x = 0; x < limit; x++)
    {
        e += 1 / fac (x);
    }
    The faculty function is a recursive one

    x! = x * (x-1) * (x-2) * ... * 3 * 2 * 1

    So you see the end-step is reached if x is 1 and the recursion step is x * (x - 1). The function is now easily created:

    Code:
    int fac (int i)
    {
        if (i == 1 || i == 0)
        {
            return 1;
        }
        else
        {
            return (i * fac (i - 1));
        }
    }

Popular pages Recent additions subscribe to a feed