views:

109

answers:

3

Ok, to start off, I am taking a C programming class this semester, and somehow was allowed to register even though I have not taken the prerequisite course. I thought I would still be able to handle it, but now that I have passed the point of no return for dropping it, I have found myself completely lost.

For my present assignment I am supposed to create a program that does a few simple trig problems and display the results. The main idea is that there is a building, and I am standing a certain distance from it.

For part A, I have to calculate the height of the building assuming I am standing 120 meters from the building and am looking at the top while tilting my head at a 30 degree angle (plus/minus 3 degrees).

Part B, assumes the building is 200ft tall, and I am standing 20ft away. What would be the angle I would have to tilt my head to see the top?

Part C, given the info in part B, how far is the distance (hypotenuse) from my head to the top of the building?

So far, I have written this:

    #include <stdio.h> 
    #include <math.h>
    #define MAX_ANGLE 33
    #define MIN_ANGLE 27
    #define DIST_A 120
    #define DIST_B 20
    #define HEIGHT_B 200
    #define PI 3.14159

    int main()
    (

    double MIN_ANGLE_R, MAX_ANGLE_R;

 MIN_ANGLE_R = MIN_ANGLE * (PI / 180);
 MAX_ANGLE_R = MAX_ANGLE * (PI / 180);
 min_height = DIST_A * tan(MIN_ANGLE);
 max_height = DIST_A * tan(MAX_ANGLE);
 angle = atan(HEIGHT_B/DIST_B)/(PI/180);
 hypotenuse = HEIGHT_B/tan(angle);

 printf ("The minimum height is %6.2f meters.\nThe maximum height is%6.2f meters.\n\n",min_height,max_height);
 printf ("The angle that youw ill tilt your head to see\nthe top of the building is %3.2f feet.\n",angle);
 printf ("The distance from your head to the top of the building is %6.2f feet.\n",hypotenuse);

 return 0;
)

When I try compiling the program, I keep getting errors, that I don't know how to read. IF anyone could read through my program, and tell me what's missing, it would be a huge help.

+6  A: 
  1. Don't confuse () and {}. They mean different things.
  2. Declare your variables.
Carl Norum
Awesome, that worked to compile and run it. Now, I've got some crazy answers popping up: The minimum height is -392.84 meters.The maximum height is-9037.56 meters.The angle that you will tilt your head to see the top of the building is 84.29 feet.The distance from your head to the top of the building is -338.60 feet.
Jesse
@Jesse, sounds like you made a mistake in your calculations. (Hint - what do you calculate the values in radians for if you don't ever use them later in the program?)
Carl Norum
Hmmmm....Does C use radians or degrees?
Jesse
@Jesse - radians. http://linux.die.net/man/3/tan
Carl Norum
Alright, thanks for all the help. I figured it out. I had the wrong variable in the equations, and a "tan" instead of "sin" for part c.
Jesse
Be careful of integer division. Integer division truncates the decimal part of the answer you might expect. `5 / 3` will give you `1`, not `1.6667`. `5.0 / 3.0` will give you `1.666...`.
haydenmuhl
@haydenmuhl: Luckily the only integer division there is `200/20`, which obviously has an exact answer. It seems more like good luck than anything else though...
caf
+2  A: 

You have to open and close main() with "{ ... }" instead of "( ... )". Also, you have to declare all the variables you are using (not just MIN_ANGLE_R and MAX_ANGLE_R).

Jeffrey
+2  A: 

I'm not a C programmer, but I suspect your trig functions work in radians and you seem to be passing degrees.

dty