177k views
2 votes
Suppose Dave drops a watermelon off a high bridge and lets it fall until it hits the water. If we neglect air resistance, then the distance d in meters fallen by the watermelon after t seconds is d = 0.5 * g * t 2 , where the acceleration of gravity g = 9.8 meters/second2 . Write a program that asks the user to input the number of seconds that the watermelon falls and the height h of the bridge above the water. The program should then calculate the distance fallen for each second from t = 0 until the value of t input by the user. If the total distance fallen is greater than the height of the bridge, then the program should tell the user that the distance fallen is not valid.

1 Answer

4 votes

Answer:

#include <bits/stdc++.h>

using namespace std;

// driver function

int main()

{

// variables

const double g = 9.8;

int sec;

double height;

int t = 0;

double d = 0;

bool flag = false;

// ask to enter time

cout << "Please enter the time of fall(in seconds):";

// read time

cin >> sec;

//ask to enter height

cout << "Please enter the height (in meters):";

//read height

cin >> height;

cout << '\\'

<< "Time(seconds) Distance(meters)\\"

"********************************\\";

// calculate height after each seconds

while ( t <= sec) {

cout << setw(23) << left << t

<< setw(24) << left << d << '\\';

// if input height is less

if ( d > height ) {

flag = true;

}

// increament the time

t += 1;

// find distance

d = 0.5 * g * (t) * (t);

}

cout << '\\';

// if input height is less then print the Warning

if ( flag ) {

cout << "Warning - Bad Data: The distance fallen exceeds the height"

" of the bridge.\\";

}

return 0;

}

Step-by-step explanation:

Read the time in seconds from user and assign it to "sec" then read height and assign it to "height".After this find the distance after each second.If the input height is less than the distance at any second, it will print a Warning message. otherwise it will print the distance after each second.

Output:

Please enter the time of fall(in seconds):11

Please enter the height (in meters):1111

Time(seconds) Distance(meters)

********************************

0 0

1 4.9

2 19.6

3 44.1

4 78.4

5 122.5

6 176.4

7 240.1

8 313.6

9 396.9

10 490

11 592.9

User Afaolek
by
6.4k points