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).