-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (71 loc) · 1.46 KB
/
main.cpp
File metadata and controls
89 lines (71 loc) · 1.46 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/************************
* A#: A02244600
* Course: CS1400
* HW#: 4
* Fall 2017
***********************/
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Define functions
float GetT(float v, float d)
{
//define variable
float t;
//if v is 0, we will get a distance of 0 and a time of 0
//because we cannot devide by 0
//Calculate t
if(v != 0)
{
t = d/v;
}
else
{
t = 0;
}
return t;
}
float GetD(float v, float h)
{
//Define constant
const float g = 9.8;
//define variable
float d;
//calculate d
d = v * sqrt(2.0 * (h/g));
return d;
}
void Report(int line, float v, float h, float d, float t)
{
cout << "On line " << right << setw(2) << line
<< ", v = " << setw(3) << v
<< ", h = " << setw(5) << h << setprecision(5)
<< ", d = " << setw(6) << d << "m"
<< ", t = " << setw(6) << t << "s" << endl;
}
int main()
{
//define variables
float v;
float h;
//Open input.txt for reading
fstream file;
file.open("input.txt");
//set a counter to help keep track of lines read from the txt file
int counter = 1;
//Read a pair of (v, h) values from the file
while(file >> v >> h)
{
//Compute the value for d with GetD() and t with GetT()
GetT(v,GetD(v,h));
//Call the function Report()
Report(counter,v,h,GetD(v,h),GetT(v,GetD(v,h)));
//Keep track of which line of the file we've read
counter ++;
}
// TODO: Close input.txt
file.close();
return 0;
}