-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter_5.txt
140 lines (110 loc) · 3.1 KB
/
Chapter_5.txt
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(QUESTION 1)
// header defintion
#ifndef Header_h
#define Header_h
namespace myConstants
{
const double gravity(9.8); // in meters/second squared
}
#endif /* Header_h */
In your main code file:
# include <math.h>
#include <iostream>
#include "Header.h"
// gets initial height from user and returns it
double getInitialHeight()
{
std::cout << "Enter the height of the tower in meters: ";
double initialHeight;
std::cin >> initialHeight;
return initialHeight;
}
// Returns height from ground after "seconds" seconds
double calculateHeight(double initialHeight, int seconds)
{
// Using formula: [ s = u * t + (a * t^2) / 2 ], here u(initial velocity) = 0
double distanceFallen = (myConstants::gravity * seconds * seconds) / 2;
double currentHeight = initialHeight - 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:\t" << height <<
" meters\n";
}
else
std::cout << "At " << seconds << " seconds, the ball is on the ground.\n";
}
void calculateAndPrintHeight(double initialHeight, int seconds)
{
double height = calculateHeight(initialHeight, seconds);
printHeight(height, seconds);
}
int getTimeRequired(double d){
// using the formula x= x0 + (v0)t + 0.5* gravityt^2
// this calculates the time required for the entered height to hit the ground and rounds it up.
int required_time;
required_time = ceil(sqrt((2*d)/myConstants::gravity));
return required_time;
}
int main()
{
const double initialHeight = getInitialHeight();
const int time = getTimeRequired(initialHeight);
// once we know both the height and required time we can use the simple for loop bellow to print out the required output.
for (int i =0; i<= time; i++){
calculateAndPrintHeight(initialHeight, i);
}
return 0;
}
(QUESTION 2)
// This method is my adaptation of the game.
#include <cstdlib>
#include <iostream>
void IsRightValue(int right_val) {
int billy;
std::cout << "Enter your first guess:";
std::cin >> billy;
for (int i = 0; i<7; i++) {
int attempt_num = i + 1;
std::cout << "Attempt Number " << attempt_num ;
if (billy == right_val) {
std::cout << " ~ You're right " << right_val << " is the right number!!!! \n";
break;
}
else if (billy > right_val) {
std::cout << " ~ Your guess is higher than actual value\n";
}
else if (billy <right_val){
std::cout << " ~ Your guess is lower than actual value\n";
}
if (i != 6) {
std::cout << " \n Enter your next attempt : ";
std::cin >> billy;
}
}
}
int main() {
int b;
b = 1;
while (b == 1) {
int random_num = rand() % 100 + 1;
IsRightValue(random_num);
int c = 1;
while (c == 1) {
char another_round;
std::cout << "\n Do you want to play another game ? (enter Y or N)\n => ";
std::cin >> another_round;
if (another_round == 'Y') {
break;
}
else if (another_round == 'N')
c = 0;
b = 0;
}
}
return 0;
}