Sine Code Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MandaRonnie
    New Member
    • Oct 2007
    • 4

    #1

    Sine Code Problem

    I have an assignment im working on for my computer science class (c programming). My program compiles right, but doesn't run like its suppose to. Could someone help me?

    Here is the assignment:

    The value of sin(x) can be approximated by using the formula:
    sin(x) = x −x3/3!+x5/5!−x7/7!+x9/9!· · ·

    where x is an angle in radians. Using the formula, approximate the value of the
    sine of an angle (in degrees) entered by the user by calculating the first 15 terms of the series. The program should allow the user to repeatedly enter angles and then display the sine, until the value 0 is entered. Here is the general outline for the program:
    1 read in a new value for angle
    2 while(angle != 0){
    3 convert angle to radians (multiply angle by pi/ 180 )
    4 sine = 0
    5 n = 1
    6 while(n ≤ 15) {
    7 calculate next term of series and add it to/subtract it from sine
    8 increment n
    9 }
    10 display sine
    11 read in a new value for angle
    12 }
    Here is an example run of the program:
    Enter an angle in degrees (0 quits): 30
    sin(30) = 0.5
    Enter an angle in degrees (0 quits): 45
    sin(45) = 0.707106
    Enter an angle in degrees (0 quits): 90
    sin(30) = 1
    Enter an angle in degrees (0 quits): 15
    sin(30) = 0.258819
    Enter an angle in degrees (0 quits): 60
    sin(30) = 0.866025
    Enter an angle in degrees (0 quits): 0
    Hint: to get the value of pi, you must have the following statement at the
    begining of your code:

    #define PI 3.1415926535897 93

    save as sine.c


    Here is what i wrote:
    [CODE=c]#include<math.h >
    #include<stdio. h>

    #define PI 3.1415926535897 93

    int main()
    {
    double angle,radian,si ne,n,x,factoria l,s,i;
    x = 1;
    printf("Enter angle in degrees (0 to quit)\n");
    scanf("%f",&ang le);

    for (i=1;i<=15;i++) {
    factorial=facto rial*n;

    while (angle!=0){
    radian=angle*(P I/180);
    sine=0;
    n=1;
    while (n<=15)
    {
    s = (radian - ( pow(radian, n) ) / (factorial));
    n++;
    }

    printf("sin(%f) =%f\n",angle,s) ;

    printf("Enter angle in degrees (0 to quit): ");
    scanf("%f",&ang le);

    }
    }

    return 0;
    }[/CODE]
    Last edited by Ganon11; Oct 21 '07, 09:43 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You have two loops, a for loop and a while loop. Both seem to be trying to do the same thing...reconsi der how you want these loops placed, and what you want each one to do.

    Comment

    • MandaRonnie
      New Member
      • Oct 2007
      • 4

      #3
      when i run it. It prints: "Enter angle in degrees <0 to quit>" if i type in an angle, say "30" it prints "sin<-925596047485475 840.00000>=1.#I " /n "NF00"

      I know that I need to alternate from + to -, but not sure how to go about it.

      Comment

      • MandaRonnie
        New Member
        • Oct 2007
        • 4

        #4
        ok i got the weird stuff fixed, now when i type in an angle, it give a close result but a little off. Like the sine of 90, it gives 1.570000 and its suppose to be 1. Also the numbers are 6 decimal places.

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Floats are designed for 6 digits, if you want more, use a long double or double.

          Apart from the loop issues Ganon mentioned, you've got one other major issue. Note how every power and factorial is odd. Compare that to the values you're using for each in your implementation.

          Also, sign alternation can be taken care of with (-1)^(n+1) if you start from an odd index, or (-1)^n if even.

          Comment

          • MandaRonnie
            New Member
            • Oct 2007
            • 4

            #6
            thanks for all the help.

            Comment

            Working...