Hello, now I have completed gave up on this one.
My math understanding is at low of low. This is the answer from quiz. I tried to write it myself. But I realized this is too complicated. There are something that I don't understand.
Write a short program to simulate a ball being dropped off of a tower. To start, the user should be asked for the height of the tower in meters. Assume normal gravity (9.8 m/s2), and that the ball has no initial velocity (the ball is not moving to start). Have the program output the height of the ball above the ground after 0, 1, 2, 3, 4, and 5 seconds. The ball should not go underneath the ground (height 0).
Your program should include a header file named constants.h that contains a symbolic constant to hold the value of gravity (9.8).
Use a function to calculate the height of the ball after x seconds. The function can calculate how far the ball has fallen after x seconds using the following formula: distance fallen = gravity_constant * x_seconds2 / 2
Sample output:
Enter the height of the tower in meters: 100
At 0 seconds, the ball is at height: 100 meters
At 1 seconds, the ball is at height: 95.1 meters
At 2 seconds, the ball is at height: 80.4 meters
At 3 seconds, the ball is at height: 55.9 meters
At 4 seconds, the ball is at height: 21.6 meters
At 5 seconds, the ball is on the ground.
Note: Depending on the height of the tower, the ball may not reach the ground in 5 seconds -- that’s okay. We’ll improve this program once we’ve covered loops.
Note: The ^ symbol isn’t an exponent in C++. Implement the formula using multiplication instead of exponentiation.
Note: Remember to use double literals for doubles, eg. 2.0 rather than 2.
This is answer which is what I'm suppose to do.
To start with.
line 18. towerheight - distancefallen what exact does that do? where did it lead to or come from?
line 14 and 20. I didnt know you could have parameter/argument and return too.
line 20 the return, where does return goes to? I know parameter/argument return to line 34. But return??
also direction said to make sure it start at 0?
Im confused...
this one is header
1 2 3 4 5 6
|
#ifndef CONSTANTS_H
#define CONSTANTS_H
constexpr double gravity { 9.8 }; // in meters/second squared
#endif
|
this is main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include "constants.h"
// gets height from user and returns it
double getTowerHeight()
{
std::cout << "Enter the height of the tower in meters: ";
double towerHeight{};
std::cin >> towerHeight;
return towerHeight;
}
// Returns height from ground after "seconds" seconds
double calculateHeight(double towerHeight, int seconds)
{
// Using formula: [ s = u * t + (a * t^2) / 2 ], here u(initial velocity) = 0
double distanceFallen { (gravity * (seconds * seconds)) / 2.0 };
double currentHeight { towerHeight - distanceFallen };
return currentHeight;
}
// Prints height every second till ball has reached the ground
void printHeight(double height, int seconds)
{
if (height > 0.0)
std::cout << "At " << seconds << " seconds, the ball is at height: " << height << " meters\n";
else
std::cout << "At " << seconds << " seconds, the ball is on the ground.\n";
}
void calculateAndPrintHeight(double towerHeight, int seconds)
{
double height { calculateHeight(towerHeight, seconds) };
printHeight(height, seconds);
}
int main()
{
const double towerHeight { getTowerHeight() };
calculateAndPrintHeight(towerHeight, 0);
calculateAndPrintHeight(towerHeight, 1);
calculateAndPrintHeight(towerHeight, 2);
calculateAndPrintHeight(towerHeight, 3);
calculateAndPrintHeight(towerHeight, 4);
calculateAndPrintHeight(towerHeight, 5);
return 0;
}
|