I have been trying for a while to figure out how to put my program together where it writes to a file the height of a projectile.
The exact problem is stated as this: Write a program that writes to a file a table that shows the height of a projectile launched straight up for each second from launch time (time zero) until the projectile hits the ground. The last entry in the table should show a projectile height of 0. The height after t seconds is given by: s = (in. velocity)* t - .5gt^2 where g is 9.8m/s^2.
The program should prompt the user for the launch velocity.
I am trying to have it look like this table:
Height Time
0.0 0
55.1 1
... and so fourth |
I understand that I need a loop in my program, but I am not sure how to create it where it depends on what value you input in for launch velocity. Like if you input 60, it shows seconds and heights for something like 16 second intervals. But if you input another number for launch velocity, you receive more or less table data for heights and time.
So in my case, I need height to show only positive integers based on my initial velocity input. I am just lost on how to correlate these things into a loop that writes it to a file since I have two random variables in time and height that correlate of each other.
Here is my program so far:
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
|
#include <iostream>
#include <fstream>
using namespace std;
const double g = 9.8;
int main()
{
ofstream outfil( "table.txt", ios::out);
double launchvelocity, lv;
int time, t;
float height, h;
cout << "Enter speed of launch velocity(m/s) => ";
cin >> lv;
for ( t >= 0 ;t < h; ){
h = ((lv * t) - (.5 * g *(t * t)));
outfil << "Height = " << h << " Time = " << t << endl;
}
outfil.close();
return 0;
}
|
I have been working on this for a couple of days now and now it is due today. So help will be much appreciated.