PDA

View Full Version : help in C programming, please help, i'm desperate




howru
Apr 7, 2008, 11:20 PM
Hi, I need help writing a program. I 'm not a computer major, but I have to take this class for graduating requirements. I have never done programming in my life and this is my first program assignment and I was wondering if someone can help me start it and give me adivce on how to continue it or end it.

Problem: Transported back in time (with your computer) you are in charge of harbor defenses for your city. Using the recently develped length standard, the meter, the cannon master wants to now if a shot fired at a given velocity from his cliff-mounted cannon will hit an enemy ship entering the harbor. In addition to the velocity, he gives you the height of the cliff, the distance from the ship, and the width of the ship. You decide to amaze the general by develping a program to solve the problem.

You can assume that the cannon is mounted horizontally. the ship distance is given from the end of the cannon to the middle of the ship.

Picture: there's a picture,its a cliff with a cannon on top of it and a ship on water. the ship is away from the cliff with a distance x.
the coordinates, to make the x and y axis positive, i just flipped the x and y axis with a upside down half parabola in the fourth quadrant. hope that makes sense. my teacher wants it that way.

to make the problem easier, this is what I did and what my teacher also prefers:

To determine the distance of the shot you must dermine how long it takes the cannon ball to fall from the height of the cliff. the initial downward velocity is zero. Use 9.8 m/s to represent gravity.

Note: I also have to make note of the units when I write my program, when i plug in numbers, i have to come out with something meters. I also need help with that.

The equation my teacher provided us is with is:
then, the distane of the shot=velocity*time

Some specification of the program is that all numerical outputs should be done using %f flag, the all calculations should maintain as much precision as is supported by the C double type in the gcc compiler. Assume all inputs will be greater than zero.

This is how I started mine:

/* calculate the distance of the cannon ball */
#include <stdio.h>
#define GRAVITY 9.8

int main(void)
{
double height > 0
double width > 0
double velocity >0
double time >0
/* Get the height of the cliff */
printf("Enter the height of the cliff (meters): ");
scanf("%f", &height);

/* Get the height of cliff from ship */
printf("Enter the distance of the ship from the cliff(meters): ");
scanf("%f", &height);

/*Get the velocity of the cannon ball */
printf("Enter the velocity of the cannon ball (meters/second): ");
scanf(%f", &velociyt);

/* Get the width of ship */
printf("Enter the width of ship (meters): ");
scanf(%f, &width);

/*Calculate the time of it takes */
time=sqrt[height/(GRAVITY/2)];

/* Calculate the distance of ball */
distance=velocity*time;

/* Print the results */
print("The distance of the cannon ball from the ship is %f.\n", distance);

return(0);
}


My questions and doubts:
1) how do i include the units in my program. did i do it right?
2) on my paper, my teacher gave us an example of a sample run:
enter the height of the cliff (meters): 50
enter the distance of the ship from the cliff (meters):180
Enter the width of the ship(meters): 15
Enter the velocity of he cannon ball (meters/second):
Distance of ball: 175.691055 meters
Direct hit!!!!!!

So after i write my program and compile it, i'm guessing my screen will have
enter the...
enter the...
enter the...
then the teacher plugs in numbers and solves the problems



But on the formula given, it doesn't include all width of the ship. should i put all this variable like the width in my program still?

When my teacher tests each program: he's going to have a set of numbers and he going to type in this number and press enter and it's going to tell him the distance.

so how would i do this. please help. I don't want to fail this class.



lee1210
Apr 8, 2008, 12:28 AM
So i've fixed this up, and as long as the math you're using is correct (I think it is, but I'm too tired to verify) it is working.

Here's the quandary I'm in. This is your homework, I don't want to do it for you. Hence, I don't want to just paste the code. i will try to detail some things that were missing...

You need to use %lf to read into doubles. Use floats if you want to use %f.

You need to define two distances, one that you read the distance away the ship is into, and one to calculate the distance the cannonball has travelled.

You need to #include math.h to use sqrt() and fabs() (You don't have to use this, but I would, more details further down).

The [] are for indexing an array. You need to use () after sqrt to invoke a method.

I would have an if statement like:
if(fabs(DistanceBoat - DistanceCannonBall) < BoatWidth/2) {
//there was a hit
} else {
//There wasn't. You were fabs(DistanceBoat - DistanceCannonBall) - BoatWidth/2 away from the ship
}
- - - -
I think those were the major things wrong.

Good luck, keep churning and post your code if you run into problems.

-Lee