Homework Help with a Simple Program (I'm new to C++)

This is a discussion on Homework Help with a Simple Program (I'm new to C++) within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a program that I am trying to write. I want it to ask the user for an ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Homework Help with a Simple Program (I'm new to C++)

    Hello, I have a program that I am trying to write. I want it to ask the user for an angle in radians, then the desired accuracy the user wants to use, and then the answer. The angle should appear as the x in the answer sin(x) = # (Demonstrated below and a I especially need help with this part).
    The program uses the Taylor series for sinx (x - x^3/3! + x^5/5! - x^7/7! + ...) to solve. The end product should look like this:

    Enter angle in radians
    1.57
    Enter the desired accuracy
    0.0000001
    sin(1.57) = 1

    Here is what I have so far:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
      float sinx, x, acc, term;
      int n = 1;
    
      cout << "Enter angle in radians " << endl;
      cin >> x; 
      cout << "Enter the desired accuracy " << endl;
      cin >> acc
      
      sinx = 0;
      term = x;
    
      while (abs(term) >= acc)
      {
        sinx = sinx + term;
        n++;
        term = -term*x*x/((2*n-2)*(2*n-1));
      }
    
      cout << "sin(x) = " << sinx << endl;
    
      return 0;
    }

    I keep getting errors and am still new to C++ so I'm not sure what the mean:
    sinx.cpp: In function 'int main()':
    sinx.cpp:15: error: expected ';' before 'sinx'


    Thanks for any advice!!
    Last edited by ambellina; 05-03-2011 at 09:00 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,677
    Code:
    cin >> acc
    What is missing from the end of the above line?
    Hint: Semi-?

    Tim S.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Code:
    cin >> acc
    What is missing from the end of the above line?
    Hint: Semi-?

    Tim S.
    Thank you! I can't believe I overlooked that.

    Now, does anyone know how I would make the final output say the value of the angle that I entered. So it would say sin(1.57) = 1 instead of sin(x) = 1?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    How about printing the value of x like:
    Code:
    cout << "sin(" << x << ") = " << sinx << endl;
    Jim

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Aha! It worked. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help (again)... So simple, its hard!
    By Aslan14 in forum C Programming
    Replies: 14
    Last Post: 12-08-2010, 12:21 PM
  2. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 04:07 PM
  3. My C++ Homework...Simple Question
    By tineras in forum C++ Programming
    Replies: 9
    Last Post: 02-09-2006, 10:22 AM
  4. Help Me For My Homework Program
    By paul16 in forum C Programming
    Replies: 8
    Last Post: 10-10-2001, 06:43 PM

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