TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
area.cpp
Go to the documentation of this file.
1
11#define _USE_MATH_DEFINES
12#include <cassert>
13#include <cmath>
14#include <cmath>
15#include <cstdint>
16#include <iostream>
17
22namespace math {
28template <typename T>
29T square_area(T length) {
30 return length * length;
31}
32
39template <typename T>
40T rect_area(T length, T width) {
41 return length * width;
42}
43
51template <typename T>
52T triangle_area(T base, T height) {
53 return base * height / 2;
54}
55
62template <typename T>
63T circle_area(T radius) {
64 return M_PI * pow(radius, 2);
65}
66
74template <typename T>
76 return base * height;
77}
78
85template <typename T>
86T cube_surface_area(T length) {
87 return 6 * length * length;
88}
89
96template <typename T>
98 return 4 * M_PI * pow(radius, 2);
99}
100
108template <typename T>
110 return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2);
111}
112
120template <typename T>
122 return 3 * M_PI * pow(radius, 2);
123}
124} // namespace math
125
130static void test() {
131 // I/O variables for testing
132 uint16_t int_length = 0; // 16 bit integer length input
133 uint16_t int_width = 0; // 16 bit integer width input
134 uint16_t int_base = 0; // 16 bit integer base input
135 uint16_t int_height = 0; // 16 bit integer height input
136 uint16_t int_expected = 0; // 16 bit integer expected output
137 uint16_t int_area = 0; // 16 bit integer output
138
139 float float_length = NAN; // float length input
140 float float_expected = NAN; // float expected output
141 float float_area = NAN; // float output
142
143 double double_length = NAN; // double length input
144 double double_width = NAN; // double width input
145 double double_radius = NAN; // double radius input
146 double double_height = NAN; // double height input
147 double double_expected = NAN; // double expected output
148 double double_area = NAN; // double output
149
150 // 1st test
151 int_length = 5;
152 int_expected = 25;
153 int_area = math::square_area(int_length);
154
155 std::cout << "AREA OF A SQUARE (int)" << std::endl;
156 std::cout << "Input Length: " << int_length << std::endl;
157 std::cout << "Expected Output: " << int_expected << std::endl;
158 std::cout << "Output: " << int_area << std::endl;
159 assert(int_area == int_expected);
160 std::cout << "TEST PASSED" << std::endl << std::endl;
161
162 // 2nd test
163 float_length = 2.5;
164 float_expected = 6.25;
165 float_area = math::square_area(float_length);
166
167 std::cout << "AREA OF A SQUARE (float)" << std::endl;
168 std::cout << "Input Length: " << float_length << std::endl;
169 std::cout << "Expected Output: " << float_expected << std::endl;
170 std::cout << "Output: " << float_area << std::endl;
171 assert(float_area == float_expected);
172 std::cout << "TEST PASSED" << std::endl << std::endl;
173
174 // 3rd test
175 int_length = 4;
176 int_width = 7;
177 int_expected = 28;
178 int_area = math::rect_area(int_length, int_width);
179
180 std::cout << "AREA OF A RECTANGLE (int)" << std::endl;
181 std::cout << "Input Length: " << int_length << std::endl;
182 std::cout << "Input Width: " << int_width << std::endl;
183 std::cout << "Expected Output: " << int_expected << std::endl;
184 std::cout << "Output: " << int_area << std::endl;
185 assert(int_area == int_expected);
186 std::cout << "TEST PASSED" << std::endl << std::endl;
187
188 // 4th test
189 double_length = 2.5;
190 double_width = 5.7;
191 double_expected = 14.25;
192 double_area = math::rect_area(double_length, double_width);
193
194 std::cout << "AREA OF A RECTANGLE (double)" << std::endl;
195 std::cout << "Input Length: " << double_length << std::endl;
196 std::cout << "Input Width: " << double_width << std::endl;
197 std::cout << "Expected Output: " << double_expected << std::endl;
198 std::cout << "Output: " << double_area << std::endl;
199 assert(double_area == double_expected);
200 std::cout << "TEST PASSED" << std::endl << std::endl;
201
202 // 5th test
203 int_base = 10;
204 int_height = 3;
205 int_expected = 15;
206 int_area = math::triangle_area(int_base, int_height);
207
208 std::cout << "AREA OF A TRIANGLE" << std::endl;
209 std::cout << "Input Base: " << int_base << std::endl;
210 std::cout << "Input Height: " << int_height << std::endl;
211 std::cout << "Expected Output: " << int_expected << std::endl;
212 std::cout << "Output: " << int_area << std::endl;
213 assert(int_area == int_expected);
214 std::cout << "TEST PASSED" << std::endl << std::endl;
215
216 // 6th test
217 double_radius = 6;
218 double_expected =
219 113.09733552923255; // rounded down because the double datatype
220 // truncates after 14 decimal places
221 double_area = math::circle_area(double_radius);
222
223 std::cout << "AREA OF A CIRCLE" << std::endl;
224 std::cout << "Input Radius: " << double_radius << std::endl;
225 std::cout << "Expected Output: " << double_expected << std::endl;
226 std::cout << "Output: " << double_area << std::endl;
227 assert(double_area == double_expected);
228 std::cout << "TEST PASSED" << std::endl << std::endl;
229
230 // 7th test
231 int_base = 6;
232 int_height = 7;
233 int_expected = 42;
234 int_area = math::parallelogram_area(int_base, int_height);
235
236 std::cout << "AREA OF A PARALLELOGRAM" << std::endl;
237 std::cout << "Input Base: " << int_base << std::endl;
238 std::cout << "Input Height: " << int_height << std::endl;
239 std::cout << "Expected Output: " << int_expected << std::endl;
240 std::cout << "Output: " << int_area << std::endl;
241 assert(int_area == int_expected);
242 std::cout << "TEST PASSED" << std::endl << std::endl;
243
244 // 8th test
245 double_length = 5.5;
246 double_expected = 181.5;
247 double_area = math::cube_surface_area(double_length);
248
249 std::cout << "SURFACE AREA OF A CUBE" << std::endl;
250 std::cout << "Input Length: " << double_length << std::endl;
251 std::cout << "Expected Output: " << double_expected << std::endl;
252 std::cout << "Output: " << double_area << std::endl;
253 assert(double_area == double_expected);
254 std::cout << "TEST PASSED" << std::endl << std::endl;
255
256 // 9th test
257 double_radius = 10.0;
258 double_expected = 1256.6370614359172; // rounded down because the whole
259 // value gets truncated
260 double_area = math::sphere_surface_area(double_radius);
261
262 std::cout << "SURFACE AREA OF A SPHERE" << std::endl;
263 std::cout << "Input Radius: " << double_radius << std::endl;
264 std::cout << "Expected Output: " << double_expected << std::endl;
265 std::cout << "Output: " << double_area << std::endl;
266 assert(double_area == double_expected);
267 std::cout << "TEST PASSED" << std::endl << std::endl;
268
269 // 10th test
270 double_radius = 4.0;
271 double_height = 7.0;
272 double_expected = 276.46015351590177;
273 double_area = math::cylinder_surface_area(double_radius, double_height);
274
275 std::cout << "SURFACE AREA OF A CYLINDER" << std::endl;
276 std::cout << "Input Radius: " << double_radius << std::endl;
277 std::cout << "Input Height: " << double_height << std::endl;
278 std::cout << "Expected Output: " << double_expected << std::endl;
279 std::cout << "Output: " << double_area << std::endl;
280 assert(double_area == double_expected);
281 std::cout << "TEST PASSED" << std::endl << std::endl;
282
283 // 11th test
284 double_radius = 10.0;
285 double_expected = 942.4777960769379;
286 double_area = math::hemi_sphere_surface_area(double_radius);
287
288 std::cout << "SURFACE AREA OF A HEMI-SPHERE" << std::endl;
289 std::cout << "Input Radius: " << double_radius << std::endl;
290 std::cout << "Expected Output: " << double_expected << std::endl;
291 std::cout << "Output: " << double_area << std::endl;
292 assert(double_area == double_expected);
293 std::cout << "TEST PASSED" << std::endl << std::endl;
294}
295
300int main() {
301 test(); // run self-test implementations
302 return 0;
303}
static void test()
Self-test implementations.
Definition area.cpp:130
int main()
Main function.
Definition area.cpp:300
int height(node *root)
Definition avltree.cpp:38
for assert
T hemi_sphere_surface_area(T radius)
surface area of a hemi-sphere ( 3 * pi * r^2)
Definition area.cpp:121
T circle_area(T radius)
area of a circle (pi
Definition area.cpp:63
T parallelogram_area(T base, T height)
area of a parallelogram (b * h)
Definition area.cpp:75
T square_area(T length)
area of a square (l * l)
Definition area.cpp:29
T rect_area(T length, T width)
area of a rectangle (l * w)
Definition area.cpp:40
T triangle_area(T base, T height)
area of a triangle (b * h / 2)
Definition area.cpp:52
T sphere_surface_area(T radius)
surface area of a sphere ( 4 * pi * r^2)
Definition area.cpp:97
T cube_surface_area(T length)
surface area of a cube ( 6 * (l
Definition area.cpp:86
T cylinder_surface_area(T radius, T height)
surface area of a cylinder (2 * pi * r * h + 2 * pi * r^2)
Definition area.cpp:109