473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Sin ()

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

Mar 18 '06 #1
10 2326
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;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 18 '06 #2
"Gregc." <gr*********@bigpond.com> writes:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 18 '06 #3

"Gregc." <gr*********@bigpond.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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

Mar 18 '06 #4
W H G wrote:

...

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

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 18 '06 #5
David Paleino opined:
W H G wrote:

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


It's not.

--
BR, Vladimir

Everything will be just tickety-boo today.

Mar 18 '06 #6
"Gregc." wrote:
I am trying to work out the length of the hypotenuse. Attached is the
code that I am using: <snip> 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
}
Mar 18 '06 #7
"Gregc." wrote:

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:Th*************@gmail.com>

Mar 20 '06 #8

"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:44***************@spamcop.net...
"Gregc." wrote:

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


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.

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:Th*************@gmail.com>

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Mar 20 '06 #9
"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

Mar 21 '06 #10

Gregc. wrote:
"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).

--
BR, Vladimir

Mar 21 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.