Thread: problem function Sin()

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    41

    problem function Sin()

    i have wrote function Sin(), didn'd use math in libary, but when it run the result error, can you help me? fix it thanks very much.
    sin(x)=Tn((-1)^k )*(x^2k+1)/(2k+1)!

    Code:
    #include <iostream>
    using namespace std;
    
    double gt(double d){
    	if(d==1||d==0)return 1;
    	return d*gt(d-1);
    }
    //
    double power(double a, int b)
    {
          double c=1;
          for (int h=0; h<b; h++) c*=a;
          return c;
    }
    //
    double Mysin(int x){
    	double Sum=0;
    	for(int i=1;i<=10;i++){
    		double tmp=gt(2*i+1);
    		Sum+=(power(-1,i)*(power(x,2*i+1)))/tmp;
    	}
    	return Sum;
    }
    void main(){
    	//MySin M;
    	int x;
    	cin>>x;
    	cout<<Mysin(x);
    }
    Last edited by llynx; 10-27-2009 at 09:01 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    Note that the return type of the global main function is int, not void.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    it have run, but result error, can you help me ? void ? main<--

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The first term of sin(x) should be x, not -x. The second term of sin(x) should be -x^3/6, not x^3/6. And so on.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks 2 you. but i dont use -x, and -x^3/6, not x^3/6.<---i dont understand
    Last edited by llynx; 10-27-2009 at 11:13 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by llynx View Post
    thanks 2 you. but i dont use -x, and -x^3/6, not x^3/6.<---i dont understand
    Oh, you're right. You have -x^3/6 correct, you just don't have the x term at all. i needs to start at 0, if you're going to use the 2i+1 form.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    when i run result still error, if i =0; you help me? thanks
    Code:
    #include <iostream>
    using namespace std;
    
    double gt(double d){
    	if(d==1||d==0)return 1;
    	return d*gt(d-1);
    }
    //
    double power(double a, int b)
    {
          double c=1;
          for (int h=0; h<b; h++) c*=a;
          return c;
    }
    //
    double Mysin(int x){
    	double Sum=0;
    	for(int i=0;i<10;i++){
    		double tmp=gt(2*i+1);
    		Sum+=(power(-1,i)*(power(x,2*i+1)))/tmp;
    	}
    	return Sum;
    }
    void main(){
    	//MySin M;
    	int x;
    	cin>>x;
    	cout<<Mysin(x);
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, the program you have doesn't compile:
    Code:
    H:\temp.cpp:24: error: `main' must return `int'
    changing that gives something that does what I expect. (Notice that I do not expect to get sin(x) out of this program; 10 terms of the series is just not enough if x is even a little bit large (like, say, 10).)

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    your first term is negative because you start your array at one instead of zero (-1 ^ 1 = -1). so you need to add or subtract 1 from i when you use it as an exponent in that first part of the formula.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    H:\temp.cpp:24: error: `main' must return `int'
    i didn't know u use compler what, but i use visual C++, it still good run, but err result
    i demo 10, realy as 100 .
    your first term is negative because you start your array at one instead of zero (-1 ^ 1 = -1). so you need to add or subtract 1 from i when you use it as an exponent in that first part of the formula.
    but -1^1=-1 yes right, i need it, but result error why ? in where ?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by llynx View Post
    i demo 10, realy as 100 .
    I have no idea what this sentence is supposed to mean. If you want your code to accurately compute sin(x), then you will need in certain circumstances to take more than 10 terms of your series. You should then go until the value added on is smaller than your desired tolerance.
    Quote Originally Posted by llynx View Post
    but -1^1=-1 yes right, i need it, but result error why ? in where ?
    This was from your original code where you started in the wrong place.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    i have add function check(). you can clear it, in -1^1=1. thanks tabstop
    Code:
    #include <iostream>
    using namespace std;
    
    double gt(double d){
    	if(d==1||d==0)return 1;
    	return d*gt(d-1);
    }
    //
    double power(double a, int b)
    {
          double c=1;
          for (int h=0; h<b; h++) c*=a;
          return c;
    }
    int check(int n){
    	return (n%2)==0?1:-1;
    }
    //
    double Mysin(int x){
    	double Sum=0;
    	double tmp=0;
    	for(int i=1;i<10;i++){
    		tmp=gt(2*i+1);
    		Sum+=(check(i+1)*(power(x,2*i+1)))/tmp;
    	}
    	return Sum;
    }
    int main(){
    	//MySin M;
    	int x;
    	cin>>x;
    	cout<<Mysin(x);
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM