Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 15th, 2003, 08:54 PM
 azrael
Junior Member

Join Date: Sep 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation 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
September 15th, 2003, 09:06 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 6 h 2 m 28 sec
Reputation Power: 4106
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]
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Last edited by Scorpions4ever : September 15th, 2003 at 09:14 PM.

#3
September 15th, 2003, 09:08 PM
 azrael
Junior Member

Join Date: Sep 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation 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
September 15th, 2003, 09:13 PM
 azrael
Junior Member

Join Date: Sep 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation 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
September 15th, 2003, 09:17 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 6 h 2 m 28 sec
Reputation Power: 4106
My mistake. Change
#include [itex]
to
#include <cmath>

#6
September 15th, 2003, 09:31 PM
 ClayDowling
Contributing User

Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 12
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.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Last edited by ClayDowling : September 15th, 2003 at 09:34 PM.

#7
September 16th, 2003, 12:30 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
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
September 16th, 2003, 09:17 AM
 azrael
Junior Member

Join Date: Sep 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation 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
September 16th, 2003, 10:12 AM
 damonbrinkley
Contributing User

Join Date: Aug 2002
Location: NC, USA
Posts: 364
Time spent in forums: 26 m 21 sec
Reputation Power: 12
Quote:
 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
September 16th, 2003, 01:03 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 6 h 2 m 28 sec
Reputation Power: 4106
As for your error message, you have a typo -- it should be "endl" not "end1" (that is end-ell instead of end-one)

#11
September 16th, 2003, 06:28 PM
 azrael
Junior Member

Join Date: Sep 2003
Posts: 11
Time spent in forums: < 1 sec
Reputation Power: 0
thanks again...

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

what you guys may have used

#12
September 16th, 2003, 07:41 PM
 JeremyL
Junior Member

Join Date: Jun 2000
Posts: 12
Time spent in forums: < 1 sec
Reputation 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
September 16th, 2003, 09:54 PM
 ClayDowling
Contributing User

Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 12
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
September 16th, 2003, 10:27 PM
 JeremyL
Junior Member

Join Date: Jun 2000
Posts: 12
Time spent in forums: < 1 sec
Reputation 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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Trig Function Program