Menu driven program using my own Sin,Cos,Tan

Pages: 12
I am having trouble properly defining the functions i need and getting them to work right I'm not sure what i really need to be doing. i have the menu down but not the functions.
Last edited on
Show specifically, including code, what you're having trouble with and we will try to help.
#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;
void main()
{
double deg;
char select;
double mySin(int deg);
void myCos(int deg);
void myTan();

do{
cout<<" Please select a trigometric function. \n ";

cout << "S or s for Sin, C or c for Cos, T or t for Tan or press q to quit. \n Please enter your selection: ";
cin >> select;

cout << "Please enter the last two digits of your Student ID + 50: ";
cin >> deg;

switch (select)
{


case 'S':
case 's':
cout << "Sin(" << deg << " degrees): " << setw(8) << fixed << setprecision(4) << mySin() << endl;
break;



case 'C':
case 'c':
cout << "Cos(" << deg << " degrees): " << setw(8) << fixed << setprecision(4) << myCos() << endl;
break;



case 'T':
case 't':
cout << "Tan(" << deg << " degrees): " << setw(8) << fixed << setprecision(4) << myTan() << endl;
break;

case 'q':
case 'Q':
cout << " Program has been terminated. That's all folks. " << endl;
break;

default:
cout << "You typed in an invalid code." << endl;
break;
}
mySin(deg/180*3.141592);
myCos(deg/180*3.141592);

} while( !(select=='q' || select== 'Q'));

}
this is the code im working on not really sure how functions work in general so im just guessing at what i need to do
It looks to me like you have misunderstood your assignment. You don't have to write your own trigonometric functions. You only need to be able to use them from your menu.

I could be wrong, but i not so humbly doubt it.

Hope this helps.
But he specifically tells me to create these functions

Section 1 Create a function “double mySin(int deg)”.

Section 2 Create a function “void myCos(int deg)”.

Section 3 Create a function “void myTan()”.
Sorry was busy.

Either way, I think it would help if you read the tutorial on functions: http://www.cplusplus.com/doc/tutorial/functions/

cout << "Tan(" << deg << " degrees): " << setw(8) << fixed << setprecision(4) << myTan() << endl;
Whether you're using a function called "myTan" or the standard "tan", the way you call it here doesn't make sense: the tan(x) function takes in one argument, x, in radians. You are currently calling it here with 0 arguments.

1
2
mySin(deg/180*3.141592);
myCos(deg/180*3.141592);

This is okay because you are calling the sin or cos function with an actual number, but the problem is that you do nothing with the value from this function. You should assign it to something
double my_value = mySin(my_number);

______________________________
Edit: Just saw your post above.
The assignment doesn't make sense, I don't know why your professor wants the return value of your cos and tan function to be void, and for the tan function to not take any arguments...

As well as the above advice, I would suggest making your own trig functions by using taylor series approximations.
http://en.wikipedia.org/wiki/Taylor_series#Approximation_and_convergence
The above link shows the series that equates to the sin(x) function.
You could also try googling a taylor series trig implementation in C++ on google.

The cos(x) function is similar, but you can use the identity cos(x) = sin(Pi / 2 - x).
And finally, you can define MyTan(x) as MySin(x) / MyCos(x).

If you're working with degrees, remember that you'll have to convert the degree value into radians inside the function.
Last edited on
I think his functions are expected to both calculate and print the result. So your switch cases should look something like:

case 'S':
case 's':
mySin();
break;

???

You may have to ask your professor for clarification. Or, if there is a link to your assignment we could access it would help.
so for each of my functions would i have to create a separate set of brackets that would calculate them? like

double mySin (int deg)
{
in here would be my taylor series stuff right?

}
Im sorry but my professor never mentioned anything about using a taylor series as far as i can remember
Then i could use the mySin to do the cos/tan right?
Link can't be accessed.

1
2
3
4
double mySin (int deg)
{
in here would be my taylor series stuff right?
}

Yes, so you would have something like:

1
2
3
4
5
6
7
double mySin(int deg)
{
    double x = deg *  3.1415926535 / 180.0;

    // return number through Taylor series equation given by the earlier link
    // http://en.wikipedia.org/wiki/Taylor_series#Approximation_and_convergence
}


1
2
3
4
5
void myCos(int deg)
{
    double cos_x = mySin(90 - deg);
    // do something with your cos_x variable, not sure what your professor wants since it's a void function
}
Last edited on
heres what he wants basically

1. Flowchart of your program with the expected output using a calculator for “sin”, “cos”,
and “tan”. (4 points)
2. Printout of your C++ program with a heading comment (Do not forget to add proper
indentation and spacing as well as comments within your program). You must create
three different styles of functions (See Section 1 – 3) and invoke them from the main
program in order to receive any credit. (4 points)
3. Copy of a screenshot after your program is executed. (First: “sin” with input of the last
two digits of your student ID + 50. Second: “cos” with the same number. Third: “tan”
with the same number. Forth: Invalid selection. Assume inputs are in degree.)(2 points)
/*
ELEN 1301-01 Programming Assignment #8.
Name : Your name.
Student ID : Your student ID #.
Due date : April 1, 2015
Purpose of the program :
Create different styles of trigonimetric functions.
Create a menu driven main program that invokes mySin, myCos, and
myTan functions.

Section 1 Create a function “double mySin(int deg)”.
Section 2 Create a function “void myCos(int deg)”.
Section 3 Create a function “void myTan()”.
Section 4 Show a menu.
Section 5 If a user enters ‘S’ or ‘s’, calculate “sin”.
If a user enters ‘C’ or ‘c’, calculate “cos”.
If a user enters ‘T’ or ‘t’, calculate “tan”.
Assume the entered number is in degree.
If a user enters ‘Q’ or ‘q’, the program terminates.
Section 6 Show the result with 4 digits below the decimal point.
Section 7 Repeat Section 4 through 6, unless a user chooses to q
Okay, so it sounds like he wants the inputs to be the last two digits of your student ID + 50. The assignment is still badly worded in my opinion, but I guess in your myTan() function, he simply wants you to assume that
deg = (ID_number_digit) + 50;

Everything else still applies, note that:
tan(x) => sin(x) / cos(x) => sin(x) / sin(90 degrees - x)

So first write your mySin() function through that taylor series equation given, and then use that in your cos() and tan() functions.

Use print statements (cout) inside your my__() functions to display the calculated number.
Last edited on
yeah english isnt his native tongue but thanks ill try in a little bit and let you know if i can get it to work but so far i havent been able to figure out whats going on
sorry im not the best since we started working with these functions
what would a taylor series look like inside a function? i ahvent had much luck trying to find one
By that I mean, just copy sin(x) equation found at
http://en.wikipedia.org/wiki/Taylor_series#Approximation_and_convergence
the first equation in the Approximation and Convergence section,

into a form that C++ understands.

3! (3 factorial) is 6.
5! is 120.
7! is 5040.

use the pow functions to calculate, for example, x^7 => pow(x, 7.0), or you could just do it manually by typing x*x*x*x*x*x*x.

If your answer is not accurate enough for larger degrees, add more terms to your series; you should be able to notice the pattern of sign changes and odd powers / factorials.

(Note that if your degree is greater than 360 degrees, you should subtract 360 and then convert to radians and do the calculation.)
Last edited on
Hi tjlandry34
I have the same Homework
I did the flowchart
but I couldn't do the codes
if u got any thing can help me with
please contact me at 8322828144
or E mail S1z@live.com
Last edited on
I really don't think you all need to actually compute the sine, cosine, or tangent. This looks like a very introductory-level homework, dealing with different function calling conventions.

Section 1 Create a function “double mySin(int deg)”.
1
2
3
4
5
6
7
double mySin(int deg)
{
  // Compute and return the value of the sine of the argument degrees. 
  // Does nothing else!
  //
  return sin( deg * M_PI / 360 );
}

Section 2 Create a function “void myCos(int deg)”.
1
2
3
4
5
void myCos(int deg)
{
  // Compute and print the value of the cosine of the argument.
  ...
}

Section 3 Create a function “void myTan()”.
1
2
3
4
5
6
7
void myTan()
{
  // 1. Get the degrees to use from the user
  ...
  // 2. Compute and print the value of the tangent of those degrees.
  ...
}

The remainder is putting it all in a menu. You'll need some extra functions to help you.

1
2
3
4
5
6
int getDegreesFromUser()
{
  // 1. Ask user to enter angular degrees (as an integer value) [using cout]
  // 2. Get the value from the user [using cin]
  // 3. Return the value the user entered
}
1
2
3
4
5
6
char menu()
{
  // 1. Show the menu [using cout]
  // 2. Get the user's selection [using cin]
  // 3. Return the user's selection
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
  cout << "TJ's menu-based trigonometry homework\n";

  bool done = false;
  while (!done)
  {
    switch (menu())
    {
      case 'S': case 's': cout << mySin( getDegreesFromUser() ) << "\n"; break;
      case 'C': case 'c': myCos( getDegreesFromUser() ); break;
      case 'T': case 't': myTan(); break;
      case 'Q': case 'q': done = true; break;
      default: cout << "Invalid selection.\n";
    }
  }

  cout << "Bye!\n";
}

Notice how the functions are all used differently, and how they each have different effects. Each one calculates something, but the way that input and output are managed are different each time.

Hope this helps.
Yeah you're probably right Duoas, hope I didn't confuse them :/
Pages: 12