473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sine Caculation/Approximation Problem?

I've been given an assignment to approximate the value of sine of an angle in degrees by using the formula: sin(x) = x −x3/3!+x5/5!−x7/7!+x9/9!· · ·

x is an angle in radians. He wants the program outline to be like so:

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 what i wrote:
Expand|Select|Wrap|Line Numbers
  1. #include<math.h>
  2. #include<stdio.h>
  3.  
  4. #define PI 3.141592653589793
  5.  
  6. int main()
  7. {
  8.   float angle,radian,sine,n,x,factorial,s,i;
  9.   x = 1;
  10.  
  11.   printf("Enter angle in degrees (0 to quit)\n");
  12.   scanf("%f",&angle);
  13.  
  14.   while (angle!=0){
  15.       radian=angle*(PI/180);
  16.       sine=0;
  17.       n=1;
  18.       while (n<=15)
  19.     {
  20.       s = (radian + (pow(-1,n+1))+(pow(radian, n)) / (n));
  21.       n++;
  22.     }
  23.  
  24.       printf("sin(%f)=%f\n",angle,s);
  25.  
  26.       printf("Enter angle in degrees (0 to quit): ");
  27.       scanf("%f",&angle);
  28.  
  29.     }
  30.   }
  31.  
  32.   return 0;
  33. }
Im not getting the right value. I think my problem lies in the s variable. how would i write that. I made it so n would increment [n++] and it would switch from positive to negative like the formula [(pow(-1,n+1)]. It looks right. Can someone help me figure that part out? Thanks
Oct 31 '07 #1
7 3084
mattmao
121 100+
Hi.

Just point out one thing, you need to write a method to calculate the factorial value of n.

And the calculation should be something like this

for (i=1; i=7; i++)
temp = temp +pow(-1, n+1)*pow(input, 2*n+1)/factorial(n)

I hope this would solve your problem.
Oct 31 '07 #2
ok I feel kinda dumb now. Its the factorial of the denominator, lol. I changed it a little, but now when i type in an angle, it just says "press any key to continue" then it ends.

Expand|Select|Wrap|Line Numbers
  1. #include<math.h>
  2. #include<stdio.h>
  3.  
  4. #define PI 3.141592653589793
  5.  
  6. int main()
  7. {
  8.   double angle,radian,s;
  9.   int i,n,factorial,x,sin;
  10.   x = 1;
  11.  
  12.   printf("Enter angle in degrees (0 to quit)");
  13.   scanf("%d",&angle);
  14.  
  15.   for (i=1; i<2*n+1; i++)
  16.   {
  17.       factorial=x*i;
  18.  
  19.    while (angle!=0)
  20.    {
  21.       radian=angle*(PI/180);
  22.       sin=0;
  23.       n=1;
  24.       while (n<=15)
  25.      {
  26.       s = radian+pow(-1,n+1)*pow(radian,2*n+1)/factorial*(2*n+1);
  27.       n++;
  28.       }
  29.  
  30.       printf("sin(%d)=%d\n",angle,s);
  31.  
  32.       printf("Enter angle in degrees (0 to quit): ");
  33.       scanf("%d",&angle);
  34.  
  35.     }
  36.   }
Oct 31 '07 #3
im going to mess with the factorial to see if i can get it fixed. Any tips would be appreciated :)
Oct 31 '07 #4
Ganon11
3,652 Expert 2GB
I think you need to invert the for...loop calculating the factorial, and the while loop that continues your program's execution. In other words, your while loop should be the outside loop, and inside this, you should calculate the factorial - for each term.
Oct 31 '07 #5
mattmao
121 100+
Hi.

I would think for the algorithm and architecture of the program before rushing into coding. Your logic looks not good...

This is not a difficult problem to solve and here is my hint for you:

you need to design a "stand-alone" method to calculate the fatorial value of any given integer n, outside the main method.

And a robust method to receive user input, validate it, if it is not a valid input, ask for a new entry; if it is valid, go on to the next step.

When you calculate the value, you can use a loop, since part of the resulting sine value is derived from a sort of series... Roughly as I mentioned before.

Anyway, with a clear logic and a careful implementaion, this problem is not that hard to solve.
Oct 31 '07 #6
I've been trying to figure this out for days. It probably is easy, but im just not getting it. Still cant figure out the factorial part.
Nov 4 '07 #7
You shouldn't calculate the factorial for every term in the series. Think about what is happening --- you are dividing first by 1!, then 3!, then 5!. They differ by only 2 multiplications. Keep a running total of what the denominator should be instead.
Nov 5 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

143
by: suri | last post by:
Hello I downloaded glibc and tried looking for the code that implements the sine function i couldnt find the file. i went to the math directory and found math.h.. i guess that needs to be...
0
by: Pallav | last post by:
i'm trying to build a lookup sine table to run some code in an embedded processor without floating point unit. the table is 256 values for 360 degrees. my code is below. the fixed point base is...
5
by: MathNewbie | last post by:
Hi, I'm trying to do get my head around some, probably basic, math calculations. I checked some .net math libs, but the only thing I learn from studying those is humility ;-). Anyway, i have...
5
by: MandaRonnie | last post by:
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: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.