Thread: writing a formula in C++

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    writing a formula in C++

    How do we allow a program to calculate the the sin of a number input by the user using the formula-- sin(x) = x - X^3/3! + x^5/5! - x^7/7! + x^9/9! ..... to the number of terms also input by the user.

    supposing that the number of terms to which sin(x) has to be calculated is n then how do we write the above formula in our program to do so.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    how do I use the loop ..????

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could read the tutorial on loops in C++.
    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

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    ya i've already read that but still got stuck on this program.....
    Code:
    // This program calculates sin(x) and/or cos(x) of the value input by the user.
    
    
    #include <iostream.h>
    
    void main ()
    
    {
    
    char v,y;
    float r,x,sin,cos;
    int n;
    
    cout<<"Enter value of 'x' "<<endl;
    cin>>x;
    cout<<endl;
    
    cout<<"Value entered in degrees?  Y/N " <<endl;
    cin>>y;
    cout<<endl;
    
    if(y == 'Y')                   //converts x into radians if entered in degrees by the user
    { r = (x * 3.14159265)/180 ;
    else r = x ;
    }
    
    cout<<"Enter 's' to calculate sin ---- Enter 'c' to calculate cos ----  Enter 'b' to calculate both"<<endl;
    cout<<"Enter ---  ";
    cin>>v;
    
    cout<<"Enter the number of terms to evaluate"<<endl;
    cin>>n;
    
    if(v=='s')
    { 
      for (t = 0; t==n; t++)
    { sin = r - (r^3/3*1)
    ---got stuck at this point

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few things to note:

    1. #include <iostream>, not <iostream.h>. You then have to use std::cout, std::cin and std::endl instead of just cout, cin and endl (unless you want to use using directives or declarations).
    2. Use int main(), not void main().
    3. The loop terminates when its condition evaluates to false. In your loop, the loop most likely never runs a single iteration. At best, it only runs exactly once: when the user enters 0 for the number of terms to evaluate.
    4. Use std::pow() from <cmath> instead of ^, which is the bitwise XOR operator.

    I suggest that you just assume that the user input is in radians first, and then hard code some test values. Only after you think you have implemented the formula correctly then do you request for user input and allow input to be in degrees.
    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

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    actually im completely new with C++ and we've not been taught what you just mentioned above so I would prefer to work with program written above but can you please help me with the formula ..I really don't understand how to make it work according to the number of terms entered by the user !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  2. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  3. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  4. help with writing function that multiplys the radius
    By Earl Lee in forum C Programming
    Replies: 6
    Last Post: 10-03-2002, 04:33 PM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 08:28 PM