Value of sin problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hapa
    New Member
    • Oct 2006
    • 31

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

    Comment

    • spidey4549
      New Member
      • Oct 2006
      • 11

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

      Comment

      • hapa
        New Member
        • Oct 2006
        • 31

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

        Comment

        • hapa
          New Member
          • Oct 2006
          • 31

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

          Comment

          • spidey4549
            New Member
            • Oct 2006
            • 11

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

            Comment

            • hapa
              New Member
              • Oct 2006
              • 31

              #21
              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)
              Last edited by Ganon11; Feb 17 '07, 10:14 PM. Reason: E-mail removed

              Comment

              • hapa
                New Member
                • Oct 2006
                • 31

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

                Comment

                • spidey4549
                  New Member
                  • Oct 2006
                  • 11

                  #23
                  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:
                  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;
                  
                  
                  	cout<<"Enter angle in degrees (0 to quit): ";
                  	cin>>angle;
                  
                  	for (i=1;i<=15;i++){
                  		factorial=factorial*n;
                  
                  	while (angle!=0){
                  		radian=angle*(PI/180);
                  		sine=0;
                  		n=1;
                  		while (n<=15){
                  			s=(radian-(pow(radian,n))/(factorial));
                  			n++;
                  
                  	}
                  
                  	cout<<"sin("<<angle<<") = "<<s<<endl;
                  	cout<<"Enter angle in degrees (0 to quit): ";
                  	cin>>angle;
                  	}
                  	}
                  
                  	return 0;
                  }

                  Comment

                  • emaghero
                    New Member
                    • Oct 2006
                    • 85

                    #24
                    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((dou ble)(-1),(double)(i)) *pow((double)(x ),(double)(2*i+ 1)))/(double)(factor ial(2*i+1));
                    }
                    cout<<temp<<end l;
                    }

                    Comment

                    Working...