473,383 Members | 1,868 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,383 software developers and data experts.

Value of sin problem

I am trying to get the value of sin based on an angle input from the user. The program changes the angle from degrees to radians. Then uses the formula:
sin(x) = x − x^3/3! + x^5/5! - x^7/7! + x^9/9!...
It does this all the way to 15. When I try to compile my program it says it doesn't evaluate to a function. Here is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. #define PI 3.14
  7.  
  8. int main()
  9. {
  10.     float angle,radian,sine,n,x,factorial;
  11.  
  12.  
  13.     cout<<"Enter angle in degrees (0 to quit): ";
  14.     cin>>angle;
  15.  
  16.     while (angle!=0){
  17.         radian=angle*(PI/180);
  18.         sine=0;
  19.         n=1;
  20.         while (n<=15){
  21.             sine=(x-(pow(x,n))/(factorial(n)));
  22.             n++;
  23.     }
  24.     cout<<"Sine equals: "<<sine<<endl;
  25.     return 0;
  26. }
  27. }
  28.  
Oct 24 '06 #1
23 8924
hapa
31
I am new to this but let me see
are you sure the factorial works that way i have never tried to do that i remember writting a program once but didnt use it that way i think you will need to use a loop here for factorial
i am not sure
cause the error u get says it take only one argument
also try changing float to double since you are using power fucntions
and if you are sure factorial fuction works than do inform me i will check it for my self and let you know
Oct 24 '06 #2
I'm not really sure if the factorial works. That was one part that I thought might be wrong. I have been drawing a blank on how to make the factorial work for a while, even though I did one like a month ago.
Oct 24 '06 #3
hapa
31
ok so lets work on the factorial thing
first of all does the question says you need to use the while fuctions for factorials
Oct 24 '06 #4
No. I can use any function for the factorial.
Oct 24 '06 #5
hapa
31
not to sound rude but i think the equation that you have written is also wrong since you are suppose to add the previous one and you are not doing so for instance when you are adding previous one with the new number
you type

sum= sum + some new number

and also did you notice in the question sign are alternative one is +ve and the other is -ve
yo will have to deal with that too
Oct 24 '06 #6
Banfa
9,065 Expert Mod 8TB
also in the calculation line sine is calculated from x and n but the variable x never has any value assigned to it.
Oct 25 '06 #7
Ok I will look at that stuff. In case this helps, this is the actual problem that I'm using and trying to solve.
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
Oct 25 '06 #8
I finally have the program able to read out what sin equals. The problem is the it is giving me the wrong number. I have a feeling it is because of the "n" in the equation needs to be the factorial here. Here is what I have now:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. #define PI 3.14
  7.  
  8. int main()
  9. {
  10.     float angle,radian,sine,n,x,factorial;
  11.     x=1;
  12.  
  13.  
  14.     cout<<"Enter angle in degrees (0 to quit): ";
  15.     cin>>angle;
  16.  
  17.     while (angle!=0){
  18.         radian=angle*(PI/180);
  19.         sine=0;
  20.         n=1;
  21.         while (n<=15){
  22.             factorial=(x-(pow(x,n))/(n));
  23.             n++;
  24.     }
  25.     cout<<"sin("<<angle<<"): "<<sine<<endl;
  26.     return 0;
  27. }
  28. }
  29.  
Oct 25 '06 #9
hapa
31
ok first things first why dont you try writing the program only first for only one sine data once you get the correct answer than we can include the while loop that says while(x !=0}
ok try only for one sine value
and also u are making a mistake in this form

//priming
...............
while{x !=0)



//priming
...............
}
the dotted things must be same which is not in your program
Oct 25 '06 #10
I don't understand what you mean with the //priming thing. When I try to compile this one it only lets me find one sine, instead of going until 0 is entered like it is supposed to. Here is what I have now though:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. #define PI 3.14
  7.  
  8. int main()
  9. {
  10.     float angle,radian,sine,n,x,factorial,s,i;
  11.     x=1;
  12.     for (i=1;i<=15;i++){
  13.         factorial=factorial*1;
  14.     }
  15.  
  16.  
  17.     cout<<"Enter angle in degrees (0 to quit): ";
  18.     cin>>angle;
  19.  
  20.     while (angle!=0){
  21.         radian=angle*(PI/180);
  22.         sine=0;
  23.         n=1;
  24.         while (n<=15){
  25.             s=(x-(pow(x,n))/(factorial));
  26.             n++;
  27.     }
  28.     cout<<"sin("<<angle<<"): "<<s<<endl;
  29.     return 0;
  30. }
  31. }
  32.  
Oct 25 '06 #11
hapa
31
BY priming i mean that what ever you type for getting the value for of sine

for instance

cin>>x
while(cin !=0)
{




cin>>x
}
this is what i mean tell me if you dont understand it
i will explaing it in detail
Oct 25 '06 #12
Ok I understand that now.
Oct 25 '06 #13
hapa
31
in the question that you have posted you are required to use to while statement
one you have used but the other one that requires you to use priming


cin>>x
while(cin !=0)
{




cin>>x
}

the underline must be same always what statement you write before while should be the same that you write before ending that while
Oct 25 '06 #14
Both ends are the same now. I'm still having trouble getting the right number though. It always gives me 1 as my sine.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. #define PI 3.14
  7.  
  8. int main()
  9. {
  10.     float angle,radian,sine,n,x,factorial,s,i;
  11.     x=1;
  12.     for (i=1;i<=15;i++){
  13.         factorial=factorial*1;
  14.     }
  15.  
  16.  
  17.  
  18.     cout<<"Enter angle in degrees (0 to quit): ";
  19.     cin>>angle;
  20.  
  21.     while (angle!=0){
  22.         radian=angle*(PI/180);
  23.         sine=0;
  24.         n=1;
  25.         while (n<=15){
  26.             s=(x-(pow(x,n))/(factorial));
  27.             n++;
  28.  
  29.     }
  30.  
  31.     cout<<"sin("<<angle<<") = "<<s<<endl;
  32.     cout<<"Enter angle in degrees (0 to quit): ";
  33.     cin>>angle;
  34.     }
  35.  
  36.     return 0;
  37. }
  38.  
Oct 25 '06 #15
hapa
31
tell me one thing when you compile this code of yours and run it without debugging
do you get a runtime error
because no where have you defined factorails value it is undefined
Oct 25 '06 #16
No, I don't get a runtime error. No matter what number I type in when I run it, it returns the value 1 for what sine equals. Unless I type 0 then it stops.
Oct 25 '06 #17
hapa
31
the reason is because you are closing your
FOR statement before that means by the time the factorial comes out of the loop it is already 15
but remember you are suppose to use each factorial like 3! should be 1*2*3
4!=1*2*3*4

but your for statement makes factorial already 15 before using it
and also dont make guesses use debugger to find the mistake that you are making in your program what goes wrong where using debugger and checking you local or auto windows
Oct 25 '06 #18
hapa
31
close your for statement where you close our while statement and also since you are not getting any result something is wrong with your equation

i will tell you one good trick
write the program again but this time only and only consider that you are finding the value of one sine for instance only with degree 30 only
since now you know how to do priming that you can do it in the end to enter multiple data until user enters 0
ok do it with only one dgree that is 30only
Oct 25 '06 #19
I changed the for loop to end at the end of the program, but it's still doing the same thing. When I compile and build it shows no errors or warnings. Let me know if I'm missing something about the debugging. I will try to do another program with only one.
Oct 25 '06 #20
hapa
31
once this work you can write add the
//priming
..........
while(cin !=0)
{

//priming
............
}

in the end once you do it it will be easy to add this and this thing makes your program get many data from the users unless he enters 0


you can mail me you program too if you want me to look at it
my e- mail is
xxx@xxx.com (E-mail removed by moderator)
Oct 25 '06 #21
hapa
31
i can write the program for you but you will learn more if you write it own your own it helps a lot and clear a lot of doubts and also it makes you learn the types of error or mistakes that you have done

its 6:12 AM here
i have to go to university at 8:00AM and i havent taken a nap so forgive me if i wasnt a help you can mail me if you want to with your doubt in this question bye
Oct 25 '06 #22
Yeah, I prefer to do the programs on my own and try to figure them out, but this one had stumpped me for a few days. I have been slowly realizing stuff and piecing them together. The help you have provided has been good also. I figured out one problem as to why I wasn't getting the right number. I plugged a wrong variable into the equation. The number is a little off but I think it has to do with how many digits I defined PI to. Thanks for all the help. I believe I can fine tune it and finish up from here hopefully. Here is the program now:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. #define PI 3.14
  7.  
  8. int main()
  9. {
  10.     float angle,radian,sine,n,x,factorial,s,i;
  11.     x=1;
  12.  
  13.  
  14.     cout<<"Enter angle in degrees (0 to quit): ";
  15.     cin>>angle;
  16.  
  17.     for (i=1;i<=15;i++){
  18.         factorial=factorial*n;
  19.  
  20.     while (angle!=0){
  21.         radian=angle*(PI/180);
  22.         sine=0;
  23.         n=1;
  24.         while (n<=15){
  25.             s=(radian-(pow(radian,n))/(factorial));
  26.             n++;
  27.  
  28.     }
  29.  
  30.     cout<<"sin("<<angle<<") = "<<s<<endl;
  31.     cout<<"Enter angle in degrees (0 to quit): ";
  32.     cin>>angle;
  33.     }
  34.     }
  35.  
  36.     return 0;
  37. }
  38.  
Oct 25 '06 #23
The problem lies in how you're defining the factorial function, or in this case not definining it. If you define the factorial function as a recursive function and write a loop to compute the sum over the odd powers of x as such then you're problem is finished.

#include <cstdlib>
#include <iostream>
#include <cmath>

#define PI 4.0*atan(1)

using namespace std;

//Define a function to recursively compute factorial n
int factorial(int);
//Since sin(x) is an infinite series define a function which computes a partial sum approximation to x
void Sin(double &x,int n);

int main(int argc, char *argv[])
{
double angle=0;
double x=0;
cout<<"Input an angle and I will compute the sine of that angle\n";
cin>>angle;

x=angle*(PI/180);

cout<<"sin("<<x<<") = ";
Sin(x,15);

system("PAUSE");
return EXIT_SUCCESS;
}
int factorial(int n)
{
if(n==0||n==1){
return 1;
}
else
return (n*factorial(n-1));
}
void Sin(double &x,int n)
{
double temp=0;
for(int i=0;i<n;i++){
temp+=(pow((double)(-1),(double)(i))*pow((double)(x),(double)(2*i+1)))/(double)(factorial(2*i+1));
}
cout<<temp<<endl;
}
Oct 25 '06 #24

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

Similar topics

1
by: Jacob | last post by:
I just wrote a function that draws a fractal by recursively calling itself, and ran into an odd (odd from my point of view anyway) problem. I've managed to solve (or rather, circumnavigate) the...
1
by: Antonio D'Ottavio | last post by:
Good morning, I've a problem with a dropdownlist located inside any row of a datalist, I fill both datalist and dropdownlist at runtime, the problem is with the dropdownlist infact using the event...
5
by: Drew | last post by:
I am building an application for inserting and updating one field in a database. The database is in SQL Server and I am using Classic ASP. I have a dropdown listbox that is populated with names,...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
4
by: Jon Paal | last post by:
I captured an arraylist into a session value but to display the current values, the page requires a postback or else it displays the prior version of the value. how do I force a post back...
7
by: ravi | last post by:
Hi, I am a newbie to javascript. Iam working on a page that has two radio buttons and a tree. I do not have my tree nodes to be selectable. Iam working on Firefox. I have the firebug...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
1
by: ahmurad | last post by:
Dear all, I am new group user, computer science graduate; just have joined this established group and thanks to all. I am working in network field but so much interested in web (PHP) field. Recently...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.