TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
area.cpp File Reference

Implementations for the area of various shapes. More...

#include <cassert>
#include <cmath>
#include <cstdint>
#include <iostream>
Include dependency graph for area.cpp:

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

template<typename T >
math::square_area (T length)
 area of a square (l * l)
 
template<typename T >
math::rect_area (T length, T width)
 area of a rectangle (l * w)
 
template<typename T >
math::triangle_area (T base, T height)
 area of a triangle (b * h / 2)
 
template<typename T >
math::circle_area (T radius)
 area of a circle (pi
 
template<typename T >
math::parallelogram_area (T base, T height)
 area of a parallelogram (b * h)
 
template<typename T >
math::cube_surface_area (T length)
 surface area of a cube ( 6 * (l
 
template<typename T >
math::sphere_surface_area (T radius)
 surface area of a sphere ( 4 * pi * r^2)
 
template<typename T >
math::cylinder_surface_area (T radius, T height)
 surface area of a cylinder (2 * pi * r * h + 2 * pi * r^2)
 
template<typename T >
math::hemi_sphere_surface_area (T radius)
 surface area of a hemi-sphere ( 3 * pi * r^2)
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementations for the area of various shapes.

The area of a shape is the amount of 2D space it takes up. All shapes have a formula to get the area of any given shape. These implementations support multiple return types.

Author
Focusucof

Definition in file area.cpp.

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

Definition at line 11 of file area.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 300 of file area.cpp.

300 {
301 test(); // run self-test implementations
302 return 0;
303}
static void test()
Self-test implementations.
Definition area.cpp:130

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 130 of file area.cpp.

130 {
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}
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