Skip to content

Commit f3b59d1

Browse files
Focusucofgithub-actionsPanquesito7
authored
feat: added math/area.cpp (#1771)
* feat: added math/area.cpp * feat: added surface area of sphere, cube and cylinder * docs: modified @brief * feat: changed to template functions * test: added 2 test cases for square_area() * test: added 3rd test case * [test/docs] added more test cases and docs for them * docs: added @details * updating DIRECTORY.md * fix: changed from math.h to cmath Co-authored-by: David Leal <[email protected]> * feat: added algorithms to the math namespace Co-authored-by: David Leal <[email protected]> * fix: call functions from the math namespace for testing * fix: style indentation Co-authored-by: David Leal <[email protected]> * fix: style indentation Co-authored-by: David Leal <[email protected]> * feat: uses uint16_t instead of int for testing * docs: added comments for testing variables * fix: style guide Co-authored-by: David Leal <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <[email protected]>
1 parent f821fae commit f3b59d1

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
* [Vector Ops](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/machine_learning/vector_ops.hpp)
166166

167167
## Math
168+
* [Area](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/area.cpp)
168169
* [Armstrong Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/armstrong_number.cpp)
169170
* [Binary Exponent](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/binary_exponent.cpp)
170171
* [Binomial Calculate](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/binomial_calculate.cpp)

math/area.cpp

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/**
2+
* @file
3+
* @brief Implementations for the [area](https://en.wikipedia.org/wiki/Area) of various shapes
4+
* @details The area of a shape is the amount of 2D space it takes up.
5+
* All shapes have a formula to get the area of any given shape.
6+
* These implementations support multiple return types.
7+
*
8+
* @author [Focusucof](https://github.com/Focusucof)
9+
*/
10+
#define _USE_MATH_DEFINES
11+
#include <cmath> /// for M_PI definition and pow()
12+
#include <cstdint> /// for uint16_t datatype
13+
#include <iostream> /// for IO operations
14+
#include <cassert> /// for assert
15+
16+
/**
17+
* @namespace math
18+
* @brief Mathematical algorithms
19+
*/
20+
namespace math {
21+
/**
22+
* @brief area of a [square](https://en.wikipedia.org/wiki/Square) (l * l)
23+
* @param length is the length of the square
24+
* @returns area of square
25+
*/
26+
template <typename T>
27+
T square_area(T length) {
28+
return length * length;
29+
}
30+
31+
/**
32+
* @brief area of a [rectangle](https://en.wikipedia.org/wiki/Rectangle) (l * w)
33+
* @param length is the length of the rectangle
34+
* @param width is the width of the rectangle
35+
* @returns area of the rectangle
36+
*/
37+
template <typename T>
38+
T rect_area(T length, T width) {
39+
return length * width;
40+
}
41+
42+
/**
43+
* @brief area of a [triangle](https://en.wikipedia.org/wiki/Triangle) (b * h /
44+
* 2)
45+
* @param base is the length of the bottom side of the triangle
46+
* @param height is the length of the tallest point in the triangle
47+
* @returns area of the triangle
48+
*/
49+
template <typename T>
50+
T triangle_area(T base, T height) {
51+
return base * height / 2;
52+
}
53+
54+
/**
55+
* @brief area of a [circle](https://en.wikipedia.org/wiki/Area_of_a_circle) (pi
56+
* * r^2)
57+
* @param radius is the radius of the circle
58+
* @returns area of the circle
59+
*/
60+
template <typename T>
61+
T circle_area(T radius) {
62+
return M_PI * pow(radius, 2);
63+
}
64+
65+
/**
66+
* @brief area of a [parallelogram](https://en.wikipedia.org/wiki/Parallelogram)
67+
* (b * h)
68+
* @param base is the length of the bottom side of the parallelogram
69+
* @param height is the length of the tallest point in the parallelogram
70+
* @returns area of the parallelogram
71+
*/
72+
template <typename T>
73+
T parallelogram_area(T base, T height) {
74+
return base * height;
75+
}
76+
77+
/**
78+
* @brief surface area of a [cube](https://en.wikipedia.org/wiki/Cube) ( 6 * (l
79+
* * l))
80+
* @param length is the length of the cube
81+
* @returns surface area of the cube
82+
*/
83+
template <typename T>
84+
T cube_surface_area(T length) {
85+
return 6 * length * length;
86+
}
87+
88+
/**
89+
* @brief surface area of a [sphere](https://en.wikipedia.org/wiki/Sphere) ( 4 *
90+
* pi * r^2)
91+
* @param radius is the radius of the sphere
92+
* @returns surface area of the sphere
93+
*/
94+
template <typename T>
95+
T sphere_surface_area(T radius) {
96+
return 4 * M_PI * pow(radius, 2);
97+
}
98+
99+
/**
100+
* @brief surface area of a [cylinder](https://en.wikipedia.org/wiki/Cylinder)
101+
* (2 * pi * r * h + 2 * pi * r^2)
102+
* @param radius is the radius of the cylinder
103+
* @param height is the height of the cylinder
104+
* @returns surface area of the cylinder
105+
*/
106+
template <typename T>
107+
T cylinder_surface_area(T radius, T height) {
108+
return 2 * M_PI * radius * height + 2 * M_PI * pow(radius, 2);
109+
}
110+
} // namespace math
111+
112+
/**
113+
* @brief Self-test implementations
114+
* @returns void
115+
*/
116+
static void test() {
117+
// I/O variables for testing
118+
uint16_t int_length; // 16 bit integer length input
119+
uint16_t int_width; // 16 bit integer width input
120+
uint16_t int_base; // 16 bit integer base input
121+
uint16_t int_height; // 16 bit integer height input
122+
uint16_t int_expected; // 16 bit integer expected output
123+
uint16_t int_area; // 16 bit integer output
124+
125+
float float_length; // float length input
126+
float float_expected; // float expected output
127+
float float_area; // float output
128+
129+
double double_length; // double length input
130+
double double_width; // double width input
131+
double double_radius; // double radius input
132+
double double_height; // double height input
133+
double double_expected; // double expected output
134+
double double_area; // double output
135+
136+
// 1st test
137+
int_length = 5;
138+
int_expected = 25;
139+
int_area = math::square_area(int_length);
140+
141+
std::cout << "AREA OF A SQUARE (int)" << std::endl;
142+
std::cout << "Input Length: " << int_length << std::endl;
143+
std::cout << "Expected Output: " << int_expected << std::endl;
144+
std::cout << "Output: " << int_area << std::endl;
145+
assert(int_area == int_expected);
146+
std::cout << "TEST PASSED" << std::endl << std::endl;
147+
148+
// 2nd test
149+
float_length = 2.5;
150+
float_expected = 6.25;
151+
float_area = math::square_area(float_length);
152+
153+
std::cout << "AREA OF A SQUARE (float)" << std::endl;
154+
std::cout << "Input Length: " << float_length << std::endl;
155+
std::cout << "Expected Output: " << float_expected << std::endl;
156+
std::cout << "Output: " << float_area << std::endl;
157+
assert(float_area == float_expected);
158+
std::cout << "TEST PASSED" << std::endl << std::endl;
159+
160+
// 3rd test
161+
int_length = 4;
162+
int_width = 7;
163+
int_expected = 28;
164+
int_area = math::rect_area(int_length, int_width);
165+
166+
std::cout << "AREA OF A RECTANGLE (int)" << std::endl;
167+
std::cout << "Input Length: " << int_length << std::endl;
168+
std::cout << "Input Width: " << int_width << std::endl;
169+
std::cout << "Expected Output: " << int_expected << std::endl;
170+
std::cout << "Output: " << int_area << std::endl;
171+
assert(int_area == int_expected);
172+
std::cout << "TEST PASSED" << std::endl << std::endl;
173+
174+
// 4th test
175+
double_length = 2.5;
176+
double_width = 5.7;
177+
double_expected = 14.25;
178+
double_area = math::rect_area(double_length, double_width);
179+
180+
std::cout << "AREA OF A RECTANGLE (double)" << std::endl;
181+
std::cout << "Input Length: " << double_length << std::endl;
182+
std::cout << "Input Width: " << double_width << std::endl;
183+
std::cout << "Expected Output: " << double_expected << std::endl;
184+
std::cout << "Output: " << double_area << std::endl;
185+
assert(double_area == double_expected);
186+
std::cout << "TEST PASSED" << std::endl << std::endl;
187+
188+
// 5th test
189+
int_base = 10;
190+
int_height = 3;
191+
int_expected = 15;
192+
int_area = math::triangle_area(int_base, int_height);
193+
194+
std::cout << "AREA OF A TRIANGLE" << std::endl;
195+
std::cout << "Input Base: " << int_base << std::endl;
196+
std::cout << "Input Height: " << int_height << std::endl;
197+
std::cout << "Expected Output: " << int_expected << std::endl;
198+
std::cout << "Output: " << int_area << std::endl;
199+
assert(int_area == int_expected);
200+
std::cout << "TEST PASSED" << std::endl << std::endl;
201+
202+
// 6th test
203+
double_radius = 6;
204+
double_expected = 113.09733552923255; // rounded down because the double datatype truncates after 14 decimal places
205+
double_area = math::circle_area(double_radius);
206+
207+
std::cout << "AREA OF A CIRCLE" << std::endl;
208+
std::cout << "Input Radius: " << double_radius << std::endl;
209+
std::cout << "Expected Output: " << double_expected << std::endl;
210+
std::cout << "Output: " << double_area << std::endl;
211+
assert(double_area == double_expected);
212+
std::cout << "TEST PASSED" << std::endl << std::endl;
213+
214+
// 7th test
215+
int_base = 6;
216+
int_height = 7;
217+
int_expected = 42;
218+
int_area = math::parallelogram_area(int_base, int_height);
219+
220+
std::cout << "AREA OF A PARALLELOGRAM" << std::endl;
221+
std::cout << "Input Base: " << int_base << std::endl;
222+
std::cout << "Input Height: " << int_height << std::endl;
223+
std::cout << "Expected Output: " << int_expected << std::endl;
224+
std::cout << "Output: " << int_area << std::endl;
225+
assert(int_area == int_expected);
226+
std::cout << "TEST PASSED" << std::endl << std::endl;
227+
228+
// 8th test
229+
double_length = 5.5;
230+
double_expected = 181.5;
231+
double_area = math::cube_surface_area(double_length);
232+
233+
std::cout << "SURFACE AREA OF A CUBE" << std::endl;
234+
std::cout << "Input Length: " << double_length << std::endl;
235+
std::cout << "Expected Output: " << double_expected << std::endl;
236+
std::cout << "Output: " << double_area << std::endl;
237+
assert(double_area == double_expected);
238+
std::cout << "TEST PASSED" << std::endl << std::endl;
239+
240+
// 9th test
241+
double_radius = 10.0;
242+
double_expected = 1256.6370614359172; // rounded down because the whole value gets truncated
243+
double_area = math::sphere_surface_area(double_radius);
244+
245+
std::cout << "SURFACE AREA OF A SPHERE" << std::endl;
246+
std::cout << "Input Radius: " << double_radius << std::endl;
247+
std::cout << "Expected Output: " << double_expected << std::endl;
248+
std::cout << "Output: " << double_area << std::endl;
249+
assert(double_area == double_expected);
250+
std::cout << "TEST PASSED" << std::endl << std::endl;
251+
252+
// 10th test
253+
double_radius = 4.0;
254+
double_height = 7.0;
255+
double_expected = 276.46015351590177;
256+
double_area = math::cylinder_surface_area(double_radius, double_height);
257+
258+
std::cout << "SURFACE AREA OF A CYLINDER" << std::endl;
259+
std::cout << "Input Radius: " << double_radius << std::endl;
260+
std::cout << "Input Height: " << double_height << std::endl;
261+
std::cout << "Expected Output: " << double_expected << std::endl;
262+
std::cout << "Output: " << double_area << std::endl;
263+
assert(double_area == double_expected);
264+
std::cout << "TEST PASSED" << std::endl << std::endl;
265+
}
266+
267+
/**
268+
* @brief Main function
269+
* @returns 0 on exit
270+
*/
271+
int main() {
272+
test(); // run self-test implementations
273+
return 0;
274+
}

0 commit comments

Comments
 (0)