writing a formula in C++

This is a discussion on writing a formula in C++ within the C++ Programming forums, part of the General Programming Boards category; How do we allow a program to calculate the the sin of a number input by the user using the ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    using loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    22,264
    You could read the tutorial on loops in C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,264
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:16 PM
  2. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  3. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 10: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, 05:33 PM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 09:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21