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: - #include<math.h>
-
#include<stdio.h>
-
-
#define PI 3.141592653589793
-
-
int main()
-
{
-
float angle,radian,sine,n,x,factorial,s,i;
-
x = 1;
-
-
printf("Enter angle in degrees (0 to quit)\n");
-
scanf("%f",&angle);
-
-
while (angle!=0){
-
radian=angle*(PI/180);
-
sine=0;
-
n=1;
-
while (n<=15)
-
{
-
s = (radian + (pow(-1,n+1))+(pow(radian, n)) / (n));
-
n++;
-
}
-
-
printf("sin(%f)=%f\n",angle,s);
-
-
printf("Enter angle in degrees (0 to quit): ");
-
scanf("%f",&angle);
-
-
}
-
}
-
-
return 0;
-
}
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
7 3084
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.
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. - #include<math.h>
-
#include<stdio.h>
-
-
#define PI 3.141592653589793
-
-
int main()
-
{
-
double angle,radian,s;
-
int i,n,factorial,x,sin;
-
x = 1;
-
-
printf("Enter angle in degrees (0 to quit)");
-
scanf("%d",&angle);
-
-
for (i=1; i<2*n+1; i++)
-
{
-
factorial=x*i;
-
-
while (angle!=0)
-
{
-
radian=angle*(PI/180);
-
sin=0;
-
n=1;
-
while (n<=15)
-
{
-
s = radian+pow(-1,n+1)*pow(radian,2*n+1)/factorial*(2*n+1);
-
n++;
-
}
-
-
printf("sin(%d)=%d\n",angle,s);
-
-
printf("Enter angle in degrees (0 to quit): ");
-
scanf("%d",&angle);
-
-
}
-
}
im going to mess with the factorial to see if i can get it fixed. Any tips would be appreciated :)
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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: ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |