-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec5.cpp
167 lines (120 loc) · 2.99 KB
/
lec5.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cmath>
using namespace std;
int main(){
cout << "----------------" << endl;
//=================
// Variables
//=================
// Which is valid?
// Integer weight;
int weight; // => only this one is good
// weight int;
// Int weight;
// Are they all valid integers?
int weight2;
// weight2 = 3;
// weight2 = 0;
// weight2 = 1;
// weight2 = -1;
// weight2 = 169;
// weight2 = 1000;
// cout << weight2 << endl;
// => They all are!
// int weight3 = 'too heavy';
// cout << weight3 << endl;
// => warning: multi-character character constant [-Wmultichar]
// => 1700886137
// int weight4 = 1000000000000000000000000000000000;
// cout << weight4 << endl;
// => error: integer literal is too large to be represented in any integer type
// int weight5 = 3.09;
// cout << weight5 << endl;
// => 3
// double weight6 = 3.09;
// cout << weight6 << endl;
// => 3.09
//=================
// Arithmetic
//=================
//
// How can we calculate BMI?
//
int height = 10;
double bmi = weight / height * 1000;
// How can we calculate compound interest?
double balance_0 = 1000;
double rate = 0.05;
// Year 1
double balance_1 = balance_0 * (1 + rate);
// Year 2
double balance_2 = balance_0 * (1 + rate) * (1 + rate);
// Year 3
double balance_3 = balance_0 * (1 + rate) * (1 + rate) * (1 + rate);
// Year N
// double balance_n = balance_0 * (1 + rate) ^ n;
// => c++ does not understand ^
// how can we write to the n'th power?
int n = 20;
double balance_n = balance_0 * pow((1 + rate), n);
// cout << balance_n << endl;
double bal0 = 1000;
double bal1 = bal0 + bal0 * (rate);
double bal2 = bal0 + 2 * bal0 * (rate);
double baln = bal0 + n * bal0 * (rate);
//cout << baln << endl;
// => 2000
//=================
// Math Functions
//=================
// where are the documentation?
// http://cplusplus.com/reference/cmath
// pow()
// how can we write 2*2*2*2*2?
// cout << pow(2, 5) << endl;
// => 32
// exp()
// how can we write 'e to the (0.07 * t) power'?
double t = 10;
double e = 2.71828;
// cout << exp(0.07 * t) << endl;
// cout << pow(e, 0.07*t) << endl;
// => 2.01375
// how can we compute the square root of 12345?
// cout << sqrt(12345) << endl;
// => 111.108
// how can we use PI?
// my_PI = 3.14159;
// => bad, mssing type declaration
// int my_PI = 3.14159;
// => bad, wrong type, rounded to integer
// double my_PI = 3.14159;
// => good
// const double my_PI = 3.14159;
// => good
// what is M_PI?
// cout << M_PI << endl;
// => 3.14159
// how can we compute the square root of PI?
// cout << sqrt(M_PI) << endl;
// => 1.77245
//=================
// Division
//=================
// cout << 7 / 3 << endl;
// => 2
// cout << 7.0 / 3 << endl;
// => 2.33333
// cout << 7 / 3.0 << endl;
// => 2.33333
// cout << 7 / 0 << endl;
// => 73896 (undefined)
//=================
// Input / Output
//=================
int bottles;
// cout << "how many bottles of vokda did you drink?" << endl;
// cin >> bottles;
// cout << bottles << ' bottles' << endl;
cout << "----------------" << endl;
}