To determine the falling distance (dist) of an object to the ground due to the gravity (g), the following formula is used, dist = 0.5* g *time? Where the variables: dist: the distance in meters to be calculated g: is set to 8.9 time: time in seconds that the object has been falling Write a program to determine distance of the falling object. The program should contain a two functions calcDist() and getTime() beside the main function. The variable time is defined as a global variable while g is a constant global variable. The function calcDist() calls the function getTime() that prompts the user to enter a value for time. The function calcDist() then should calculate and display the value of dist. From the main function, main(), calls the function calcDist(). Write your program based on the following output Sample: Enter the value of time: 4 [ENTER] The distance is that the object travelled is: 78.4 meters Guide to your coding design: - define the two global variables and the two functions prototypes. - create the function getTime() to read value of time from the user - create the function calcDist() and call getTime() inside it. - calculate dist inside calcDist() using the above formula and display its value. - call the function calcDist() from the main function main().
C++
Note:
- The Program is written based on the requirement with g = 8.9.
- But the sample output is based on the g=9.8.
PROGRAM:
//Header files
#include <iostream>
#include <math.h>
//Using namespace
using namespace std;
//Declaring the global variables
int t = 0;
const double g = 8.9;
//Declaring the functions
int getTime();
double calcDist();
//main() function
int main() {
//Calling the calcDist()
calcDist();
}
//getTime() function
int getTime()
{
//Getting the input from the user
cout<<"Enter the value of time:";
cin>>t;
//Returning the input to calcDist
return t;
}
//calcDist() function
double calcDist() {
//Calling the getTime() function
int x= getTime();
double d = 0;
//calculating the distance
d = (0.5 * g) * (pow(x, 2));
cout<<"The distance that the object travelled is:"<<d;
//returning the value to main()
return 0;
}
SCREENSHOT OF THE PROGRAM:
Step by step
Solved in 3 steps with 2 images