Need your input about difficult program?

This is a discussion on Need your input about difficult program? within the C Programming forums, part of the General Programming Boards category; I do not understand how to get started with my program. I need to write a program that calculates the ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    6

    Need your input about difficult program?

    I do not understand how to get started with my program. I need to write a program that calculates the sin of x with a truncation error of less than 0.000001. The truncation error is the absolute value of the difference between the previously calculated sine of x and the current calculated sine of x. For each iteration of the series I need to print out the iteration number, the calculated value, truncation error, and the C math library calculated sine of x. I really need help getting started.

  2. #2
    Unregistered
    Guest
    You say that you need the "calculated value" and the "C Math library value of the sine of x" ?

    Both values will be one and the same won't they ??

    Just trying to help you clarify the problem better.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Im assuming your gonna be using that formula 1 - x/1! + x^2/2! -... er whatever it is. Like I always say, the easiest way to start a project is to just start it (well Ive never really said that before, but whatever). What I mean is dont think too much (especially on somthing this small), just write code to do exactly what the program should do. Im again assuming the user will enter some x, so thats where you start...


    Code:
    main()
    {
        // variable declarations
        ...
    
        // prompt user and input value
        ...
    
        // calc sin x
        ...
    
        return 0
    }
    just fill in the blanks.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The formula you need:

    sin x = x^1/1! - x^3/3! + x^5/5! - x^7/7! + ... + (-1)^n (x^(2n+1)/(2n+1)!)

    A serie is a sum. So you need to setup a variable to contain the sum. Then you need to use iteration to approximate sin x.

    Code:
    x = user input;
    max_iterations = user input;
    
    sinx_approx = 0;
    sinx_library = sin (x);
    
    for n = 0 to max_iterations
        sinx_approx = sinx_approx + (-1)^n (x^(2n+1)/(2n+1!));
        error = abs (sinx_library - sinx_approx);
    Hope this helps you a little further.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 09:27 PM
  2. Replies: 1
    Last Post: 08-11-2006, 05:44 AM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Input via the serial port for a C program
    By Anthony in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 02:19 PM
  5. Program skipping some input
    By Dita in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2004, 03:08 PM

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