1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
11
Rep Power
0

#### Trig Function Program

Hi, Im a programmer as of 7:30pm EST today. I have had short and quick run-ins with programming but this is my first actual attempt to learn(C/C++).

I am enrolled in a trig class and i would like some help on how to use the sin,cos,tan functions as i have had no luck.

i tried

#include <iostream.h>
#include <math.h>
int main()

{
int number;
cout<<"Input a number: ";
cin>>number;
sin(number);
cout<<"Sin: "<<number;

return 0;
}

if i enter, 45 i will get 45 and not .707 or something like that.

if this sin function is not for such a purpose how can i go about calculating sin and the other functions?
2. Better to use double or float instead of int. Also, sin() returns a value, which you're not assigning to any variable. Thirdly, sin takes its values in radians (not degrees), so you might need to convert the user input into radians. Finally, including .h (as in #include <iostream.h>) is old school -- the new standard is to #include <iostream> and put a using namespace std;. I won't go into the namespace details here, since you'll learn the details yourself as you progress through learning C++, but I just wanted to point you in the right direction, before you develop too many bad habits :)

With that said, here's your corrected code (my changes in bold).
Code:
#include <iostream>
#include [itex]
using namespace std;

int main()
{
double number;
double deg_to_rad = 3.1415926536/180; // pi/180
double sin_val;

cout<<"Input a number: ";
cin>>number;
sin_val = sin(number * deg_to_rad); // convert to radians before passing to sin()
cout<<"Sin: "<< sin_val;

return 0;
}
Corrected some bolding in code above[/edit]
Last edited by Scorpions4ever; September 15th, 2003 at 09:14 PM.
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
11
Rep Power
0
Thanks a lot. Im glad i took the 12 seconds of my life and signed up for the forms. Time well spent.
4. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
11
Rep Power
0
Uh oh. Now the compiler gives me an error.

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\J_****\sin.cpp" -o "C:\Dev-Cpp\J_****\sin.exe" -I"C:\DEV-CPP\include\c++" -I"C:\DEV-CPP\include\c++\mingw32" -I"C:\DEV-CPP\include\c++\backward" -I"C:\DEV-CPP\include" -L"C:\DEV-CPP\lib"
C:/Dev-Cpp/J_****/sin.cpp:2:17: math: No such file or directory

btw, i am currently using Dev-C++ compiler for windows
5. My mistake. Change
#include [itex]
to
#include <cmath>
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2002
Location
Flint, MI
Posts
328
Rep Power
14
The correct code that will get you what you're looking for is:
Code:
#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])
{

double degrees;

cout << "Angle in degrees: ";
cin >> degrees;

radians = 2 * M_PI * degrees / 360.0;
cout << degrees << " degrees is " << radians << " radians" << endl;
cout << "Sine: " << sin(radians) << endl;
cout << "Cosine: " << cos(radians) << endl;

system("PAUSE");
return 0;
}
This was developed on Dev-Cpp for windows, so you're doing just fine. BTW, you might want to make sure you've got the latest version. Your error output didn't look quite right. Try using the compile features that come with the IDE. You'll get some nice debugging features by doing that.

It's also worth noting that you need to do some rounding on occasion, when special cases arise. In particular, take the cosine of 90 degrees (pi/2). By definition it is zero. Because the computer is doing a numerical approximation to both the angle and the cosine, you get an answer like 6 * 10^-17. Not what you expect to see, but if you round it off to an acceptable significant digit (which in this case is 2 digits), you get 0.
Last edited by ClayDowling; September 15th, 2003 at 09:34 PM.
7. Also, you might need to explicitly include the math library. I normally use gcc/g++ from the command line instead of using the IDE and I'm sure that I've had be explicit about linking in the math library, though I'm not sure about the IDE.

The option is -lm ("minus ell emm") for command line. It might be the same for the IDE.
8. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
11
Rep Power
0
Could you explain this line.

int main(int argc, char *argv[])

cout << "Tan: " << tan(radians) << end1;

yet i recieve the follow error when i compile

C:\Dev-Cpp\Examples\progs\trig.cpp
[Warning] In function int main(int, char**)':

and

20 C:\Dev-Cpp\Examples\progs\trig.cpp
end1' undeclared (first use this

and

20 C:\Dev-Cpp\Examples\progs\trig.cpp
(Each undeclared identifier is reported
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2002
Location
NC, USA
Posts
364
Rep Power
14
Originally posted by azrael
Could you explain this line.

int main(int argc, char *argv[])
argc is the number of command line parameters you pass to the program and argv[] is an array of the parameters.

Example:

myprog param1 param2

argc would equal 3 since you passed 3 parameters including the program name. argv[0] would equal myprog, argv[1] would equal param1 and argv[2] would equal param2.
10. As for your error message, you have a typo -- it should be "endl" not "end1" (that is end-ell instead of end-one)
11. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
11
Rep Power
0
thanks again...

could you also recommend C/C++ learning resources?

what you guys may have used
12. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2000
Posts
12
Rep Power
0
Both of the code snippets assumes he is passing something to the main function and wan't to return something. At this point you are just as well off using "void main()" and not returning anything.

PHP Code:
 int main(int argc, char *argv[])  
PHP Code:
 return 0;  
13. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2002
Location
Flint, MI
Posts
328
Rep Power
14
Jeremy,

You're correct in that assumption. I write the line that way by force of habit. It's worth noting though that the runtime startup code from libc will look for that function rather than what you suggest. Obviously, the linker copes just fine, but I thought that it might be a good idea to make my function match what it's looking for.

Curious thing about the -lm that I should have needed. Dev-Cpp didn't bat an eyelash when I didn't include it. What I think is more likely is that the functions actually reside in libc in this particular implementation, and libm is just a dummy stub. That or the compiler figures it out all on it's little lonesome and links to libm for us.

You're right though, everything in my past has suggested that it's a good idea to link against libm. I didn't even notice this anomaly until you pointed it out.

As for learning resources, I learned this language by reading The C++ Programming Language by Bjarne Stroustrup. I'm told that it's a little too challenging for the begginer, but first year CS students that I've recommended it to thought it was a great book to learn from. Probably indicates that it's good for some people, and a bad choice for others.
14. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2000
Posts
12
Rep Power
0
You guys are allot smarter then I as a CS major in his second semester of classes. I just didn't want azrael to get confused, or worry about what those lines were doing and think it had an effect on his current issue.