Thread: Problem with formula

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    4

    Question Problem with formula

    Hello, I have a maths assignment and i would like some help putting these formula in code, and how I would implement these in a loop function. The x is given.

    An = (-x/n)*An-1 - 1/n*(n-1) * An-2

    if x = 3

    A(0) = 2
    A(1) =0

    A(2) = (-3/2)*0 - 1/2*(1)* 0 = -1

    This is basically the results for the first 3, I need a loop to continuously do this for any number put for x

    Any suggestions ?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You need to retain the last two values (at least).
    The first two values are 2 and 0 (apparently).
    So initialize a couple of variables with those values and print them:
    Code:
        double a = 2, b = 0;
        printf("% f\n% f\n", a, b);
    Then start your loop at 2, calculate the new value, print it, and then shift the two saved values over:
    Code:
        for (int n = 2; n < 100; n++) {
            double c = -x / n * b - 1.0 / n * (n - 1) * a;
            printf("% f\n", c);
            a = b;
            b = c;
        }
    Alternatively, you could store all calculated values in an array.
    Code:
        double a[100] = {2, 0};
        for (int n = 2; n < 100; n++)
            a[n] = -x / n * a[n-1] - 1.0 / n * (n - 1) * a[n-2];
        for (int n = 0; n < 100; n++)
            printf("% f\n", a[n]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formula Problem
    By rayhall in forum C Programming
    Replies: 9
    Last Post: 09-17-2013, 10:28 AM
  2. Formula for Running word problem
    By fren-z-on3 in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2010, 04:42 PM
  3. C program formula code problem
    By parachutes89 in forum C Programming
    Replies: 5
    Last Post: 09-14-2009, 04:13 AM
  4. Problem with a mathematical formula
    By BianConiglio in forum C Programming
    Replies: 13
    Last Post: 04-29-2005, 11:26 PM
  5. Formula problem
    By Cassius in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2002, 04:54 PM

Tags for this Thread