Sin ()

G

Gregc.

G'day

I am trying to work out the length of the hypotenuse. Attached is the
code that I am using:

#include <stdio.h>
#include <math.h>

double hypUsingTrig(double e)
{
double x;
x= double sin(37);
return e/x;
}

main() {
printf("The final hypotenuse is:%f\n", hypUsingTrig(3));
}

The answer is coming up as -4.661728 but I don't think it is correct.
Is there anyway method that I could check my answer.

Greg
 
R

Richard Heathfield

Gregc. said:
G'day

I am trying to work out the length of the hypotenuse.

Which hypotenuse?
Attached is the code that I am using:

#include <stdio.h>
#include <math.h>

double hypUsingTrig(double e)
{
double x;
x= double sin(37);
return e/x;
}

main() {
printf("The final hypotenuse is:%f\n", hypUsingTrig(3));
}

The answer is coming up as -4.661728 but I don't think it is correct.

I'm impressed that you're even getting an answer at all. Over here, it
didn't compile:

gcc -W -Wall -ansi -pedantic -O2 -g -pg -c -o foo.o foo.c
foo.c: In function `hypUsingTrig':
foo.c:7: parse error before `double'
foo.c:6: warning: `x' might be used uninitialized in this function
foo.c: At top level:
foo.c:11: warning: return-type defaults to `int'
foo.c: In function `main':
foo.c:13: warning: control reaches end of non-void function
make: *** [foo.o] Error 1

Once you've sorted out that little glitch, you may wish to recall that sin()
takes an angle in radians, not degrees.

#define PI 3.14159 /* adjust to taste */

double deg_to_rad(double deg)
{
double rad = deg * PI / 180.0;
}
 
K

Keith Thompson

Gregc. said:
I am trying to work out the length of the hypotenuse. Attached is the
code that I am using:

#include <stdio.h>
#include <math.h>

double hypUsingTrig(double e)
{
double x;
x= double sin(37);
return e/x;
}

main() {
printf("The final hypotenuse is:%f\n", hypUsingTrig(3));
}

The answer is coming up as -4.661728 but I don't think it is correct.
Is there anyway method that I could check my answer.

If you have a question about your code, post your code. Posting
something similar to your code, as you've done here, is a waste of
everyone's time. When you post code, you need to copy-and-paste the
*exact* code that you compiled. Don't try to re-type it. Don't make
us guess which errors are in your original code, and which errors you
introduced when you posted it.

I tried compiling the code you posted. My compiler reported a syntax
error on the line

x= double sin(37);

I'm guessing that this was supposed to be

x = (double)sin(37);

With that change, the code compiles and produces the same output that
you reported. If I've guessed incorrectly, much of what follows will
be useless.

Any cast should be viewed with suspicion. This cast in particular is
completely unnecessary. The sin function returns a value of type
double; casting it to double serves no purpose.

Your hypUsingTrig can be written more clearly without using an
intermediate variable:

double hypUsingTrig(double e)
{
return e / sin(37);
}

You should change "main()" to "int main(void)", and you should add a
"return 0;" just before the closing brace.

With those changes (and a couple of cosmetic formatting changes), your
program becomes:
================================
#include <stdio.h>
#include <math.h>

double hypUsingTrig(double e)
{
return e / sin(37);
}

int main(void)
{
printf("The final hypotenuse is: %f\n", hypUsingTrig(3));
return 0;
}
================================

It's been a long time since I studied trigonometry, and I don't
remember all the formulas off the top of my head. More descriptive
names would be very helpful. Keep in mind that variable names are not
limited to single letters -- and since "e" the name of is a
mathematical constant, using it as a variable name in a program that
does trigonometry is particularly confusing. Comments can also be
helpful.

You have two "magic numbers" in your code, 37 and 3, and no obvious
indication of what they represent. Possibly hypUsingTrig should take
two arguments, and you should call it as hypUsingTrig(3, 37). Giving
meaningful names to the parameters ("edge" and "angle", maybe?) would
make the code much easier to follow.

I strongly suspect the real source of your problem is that the sin()
function's argument is expressed in radians, not degrees. I doubt
that you're interested in a right triangle with a 37-radian vertex
(that's about 2220 degrees).
 
W

W H G

Gregc. said:
G'day

I am trying to work out the length of the hypotenuse. Attached is the
code that I am using:

#include <stdio.h>
#include <math.h>

double hypUsingTrig(double e)
{
double x;
x= double sin(37);

The sine function takes argument in radians. You probably
wanted 37 degrees.

angle in radians = angle in degrees * PI / 180
where PI is 3.14159 etc. or

double PI = 4 * atan(1.0);


---- W H G
 
D

David Paleino

W said:
...
>
where PI is 3.14159 etc. or

double PI = 4 * atan(1.0);

Or he could use the M_PI constant defined in math.h (at least, on my GCC
implementation, I don't really know if it's standard C...)

Cheers,
David
 
V

Vladimir S. Oka

David Paleino opined:
Or he could use the M_PI constant defined in math.h (at least,
on my GCC implementation, I don't really know if it's standard
C...)

It's not.
 
O

osmium

Gregc. said:
I am trying to work out the length of the hypotenuse. Attached is the
code that I am using:
The answer is coming up as -4.661728 but I don't think it is correct.
Is there anyway method that I could check my answer.

Since there is no such thing as a negative length, the answer is clearly
wrong, no doubt there. I can think of two ways to check it, a pencil and
paper and a protractor, or a pocket calculator.

There are three main reasons for using a function that occur to me
o it is hoped that the same function can be used elsewhere in the program
o to clarify (for a human) the relationships that exist
o to hide distracting clutter from a human.

Of course there are more reasons but I think they tend towards the esoteric
end of the spectrum. I don't see any of the above purposes are served by
your function. Unless there are properties or qualities of the number 37
that I don't know about, that is.

Does this come close to what you are trying to do? It is designed for
clarity rather than conciseness.

#include <stdio.h>
#include <math.h>

/* given an included angle in degrees, angle, and
the length of the side opposite the angle,
returns the hypotenuse of a right triangle. */
double hyp(double angle, double opp)
{
double temp;
const double dtor = 3.1416/180.; // degrees to radians
temp = opp/sin(angle*dtor);
return temp;
}
//=================
int main()
{
double x;
x = hyp(37, 3);
printf("The hypotenuse is %f.\n"
"The units are the same as the units "
"used for the length, 3.\n", x);
/*getchar();*/
return 0; // success
}
 
K

Kenneth Brody

Gregc. said:
G'day

I am trying to work out the length of the hypotenuse. Attached is the
code that I am using:

#include <stdio.h>
#include <math.h>

double hypUsingTrig(double e)
{
double x;
x= double sin(37);
return e/x;
}

main() {
printf("The final hypotenuse is:%f\n", hypUsingTrig(3));
}

The answer is coming up as -4.661728 but I don't think it is correct.
Is there anyway method that I could check my answer.

Well, fixing the typo in line 7:

x= double sin(37);
to
x= (double)sin(37);

Results in the number you see because:

sin(37) is approximately -0.643538
and
(3 / -0.643538) is approximately -4.661728

Now, what is it you are actually trying to do?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
F

Fred Kleinschmidt

Kenneth Brody said:
Well, fixing the typo in line 7:

x= double sin(37);
to
x= (double)sin(37);

Why would you cast sin() to a double? It already returns a double.

I don't think the OP really understands how to obtain a hypotenuse,
or else he hasn't shown his real code.
To computre a hypotenuse, you either need to know both of the other two
sides,
or one side and one angle, and the knowledge of which side and
which angle those are. The OP's function has only one argument,
so it can't possibly compute a hypotenuse.

Possibly the OP's main problem is that he mistakenly thinks the
argument of sin(x) is in degrees rather than radians.
 
G

Gregc.

"Possibly the OP's main problem is that he mistakenly thinks the
argument of sin(x) is in degrees rather than radians."

Yep your correct. I'm new to C, and still trying to get my head around
it.

Thanks, it has been most helpful.

Regards
Greg
 
V

Vladimir S. Oka

Gregc. said:
"Possibly the OP's main problem is that he mistakenly thinks the
argument of sin(x) is in degrees rather than radians."

Yep your correct. I'm new to C, and still trying to get my head around
it.

Thanks for the effort to quote. However, to do it properly, you should
use the style above, also preserving the attribution lines (e.g.
"Gregc. wrote:"). If you're using Google, read and follow instructions
at <http://cfaj.freeshell.org/google/>. If you're using a standalone
news client (preferrable) you should be able to configure it like this
(it's usually the default, although YMMV).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top