Am i using boolean values correctly? our professor wanted boolean value use for the if and else statements. Other comments and suggestions welcome. thanks.
Code:
#include <stdio.h>
#include <math.h>
#define GRAVITY 9.8
#define LIMIT 500
#define SEC 5
double calcTime(double height);
double calcShotDistance(double time, double velocity);
double getDoubleHeight();
double getDoubleShipWidth();
double getDoubleVelocity();
double getDoubleShipDistance();
int i........(double shipWidth, double shipDistance, double distance);
int isDistance(double distance);
int isTime(double time);
int main()
{
double height, velocity, shipWidth, shipDistance, time, distance;
int boom, far, timeToHit;
height = getDoubleHeight();
shipDistance = getDoubleShipDistance();
shipWidth = getDoubleShipWidth();
velocity = getDoubleVelocity();
time = calcTime(height);
distance = calcShotDistance(time, velocity);
boom = i........(shipWidth, shipDistance, distance);
far = isDistance(distance);
timeToHit = isTime(time);
if((boom == 1) && (far == 1))
{
printf("I think you hit China!!!\n");
printf("Distance of ball: %.6f meters\n", distance);
printf("Direct hit!!!\n");
}
else if((boom == 1) && (far == 0) && (timeToHit == 1))
{
printf("Look out beloooooooooooooooooooooooooooow!\n");
printf("Distance of ball: %.6f meters\n", distance);
printf("Direct hit!!!\n");
}
else if((boom == 0) && (far == 1))
{
printf("I think you hit China!!!\n");
printf("Distance of ball: %.6f meters\n", distance);
printf("You missed!\n");
}
else if ((boom == 0) && (far == 0) && (timeToHit == 1))
{
printf("Look out beloooooooooooooooooooooooooooow!\n");
printf("Distance of ball: %.6f meters\n", distance);
printf("You missed!\n");
}
else if((boom == 1) && (far == 0) && (timeToHit == 0))
{
printf("Distance of ball: %.6f meters\n", distance);
printf("Direct hit!!!\n");
}
else
{
printf("Distance of ball: %.6f meters\n", distance);
printf("You missed!\n");
}
return 0;
}
int isTime(double time)
{
if (time > SEC)
{
return 1;
}
else
{
return 0;
}
}
int isDistance(double distance)
{
if (distance > LIMIT)
{
return 1;
}
else
{
return 0;
}
}
int i........(double shipWidth, double shipDistance, double distance)
{
if((distance < shipDistance + (shipWidth / 2.0)) && (distance > shipDistance - (shipWidth / 2.0)))
{
return 1;
}
else
{
return 0;
}
}
double getDoubleHeight()
{
double height;
printf("Enter the height of the cliff (meters): ");
scanf("%lf", &height);
return height;
}
double getDoubleVelocity()
{
double velocity;
printf("Enter the velocity of the cannon ball (meters/second): ");
scanf("%lf", &velocity);
return velocity;
}
double calcShotDistance(double time, double velocity)
{
double distance = time*velocity;
return distance;
}
double getDoubleShipWidth()
{
double shipWidth;
printf("Enter the width of the ship (meters): ");
scanf("%lf", &shipWidth);
return shipWidth;
}
double getDoubleShipDistance()
{
double shipDistance;
printf("Enter the distance of the ship from the cliff (meters): ");
scanf("%lf", &shipDistance);
return shipDistance;
}
double calcTime(double height)
{
double time = sqrt(height / (GRAVITY / 2.0));
return time;
}