Value of sin problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spidey4549
    New Member
    • Oct 2006
    • 11

    #1

    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:
    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    #define PI 3.14
    
    int main()
    {
    	float angle,radian,sine,n,x,factorial;
    
    
    	cout<<"Enter angle in degrees (0 to quit): ";
    	cin>>angle;
    
    	while (angle!=0){
    		radian=angle*(PI/180);
    		sine=0;
    		n=1;
    		while (n<=15){
    			sine=(x-(pow(x,n))/(factorial(n)));
    			n++;
    	}
    	cout<<"Sine equals: "<<sine<<endl;
    	return 0;
    }
    }
  • hapa
    New Member
    • Oct 2006
    • 31

    #2
    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

    Comment

    • spidey4549
      New Member
      • Oct 2006
      • 11

      #3
      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.

      Comment

      • hapa
        New Member
        • Oct 2006
        • 31

        #4
        ok so lets work on the factorial thing
        first of all does the question says you need to use the while fuctions for factorials

        Comment

        • spidey4549
          New Member
          • Oct 2006
          • 11

          #5
          No. I can use any function for the factorial.

          Comment

          • hapa
            New Member
            • Oct 2006
            • 31

            #6
            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

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              also in the calculation line sine is calculated from x and n but the variable x never has any value assigned to it.

              Comment

              • spidey4549
                New Member
                • Oct 2006
                • 11

                #8
                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

                Comment

                • spidey4549
                  New Member
                  • Oct 2006
                  • 11

                  #9
                  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:
                  Code:
                  #include <iostream>
                  #include <math.h>
                  
                  using namespace std;
                  
                  #define PI 3.14
                  
                  int main()
                  {
                  	float angle,radian,sine,n,x,factorial;
                  	x=1;
                  
                  
                  	cout<<"Enter angle in degrees (0 to quit): ";
                  	cin>>angle;
                  
                  	while (angle!=0){
                  		radian=angle*(PI/180);
                  		sine=0;
                  		n=1;
                  		while (n<=15){
                  			factorial=(x-(pow(x,n))/(n));
                  			n++;
                  	}
                  	cout<<"sin("<<angle<<"): "<<sine<<endl;
                  	return 0;
                  }
                  }

                  Comment

                  • hapa
                    New Member
                    • Oct 2006
                    • 31

                    #10
                    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

                    Comment

                    • spidey4549
                      New Member
                      • Oct 2006
                      • 11

                      #11
                      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:
                      Code:
                      #include <iostream>
                      #include <math.h>
                      
                      using namespace std;
                      
                      #define PI 3.14
                      
                      int main()
                      {
                      	float angle,radian,sine,n,x,factorial,s,i;
                      	x=1;
                      	for (i=1;i<=15;i++){
                      		factorial=factorial*1;
                      	}
                      
                      
                      	cout<<"Enter angle in degrees (0 to quit): ";
                      	cin>>angle;
                      
                      	while (angle!=0){
                      		radian=angle*(PI/180);
                      		sine=0;
                      		n=1;
                      		while (n<=15){
                      			s=(x-(pow(x,n))/(factorial));
                      			n++;
                      	}
                      	cout<<"sin("<<angle<<"): "<<s<<endl;
                      	return 0;
                      }
                      }

                      Comment

                      • hapa
                        New Member
                        • Oct 2006
                        • 31

                        #12
                        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

                        Comment

                        • spidey4549
                          New Member
                          • Oct 2006
                          • 11

                          #13
                          Ok I understand that now.

                          Comment

                          • hapa
                            New Member
                            • Oct 2006
                            • 31

                            #14
                            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

                            Comment

                            • spidey4549
                              New Member
                              • Oct 2006
                              • 11

                              #15
                              Both ends are the same now. I'm still having trouble getting the right number though. It always gives me 1 as my sine.
                              Code:
                              #include <iostream>
                              #include <math.h>
                              
                              using namespace std;
                              
                              #define PI 3.14
                              
                              int main()
                              {
                              	float angle,radian,sine,n,x,factorial,s,i;
                              	x=1;
                              	for (i=1;i<=15;i++){
                              		factorial=factorial*1;
                              	}
                              
                              
                              
                              	cout<<"Enter angle in degrees (0 to quit): ";
                              	cin>>angle;
                              
                              	while (angle!=0){
                              		radian=angle*(PI/180);
                              		sine=0;
                              		n=1;
                              		while (n<=15){
                              			s=(x-(pow(x,n))/(factorial));
                              			n++;
                              
                              	}
                              
                              	cout<<"sin("<<angle<<") = "<<s<<endl;
                              	cout<<"Enter angle in degrees (0 to quit): ";
                              	cin>>angle;
                              	}
                              
                              	return 0;
                              }

                              Comment

                              Working...