Algorithms_in_C++ 1.0.0
Set of 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:

Namespaces

namespace  math
 for IO operations
 

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)
 
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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
276 {
277 test(); // run self-test implementations
278 return 0;
279}
static void test()
Self-test implementations.
Definition area.cpp:118
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
118 {
119 // I/O variables for testing
120 uint16_t int_length = 0; // 16 bit integer length input
121 uint16_t int_width = 0; // 16 bit integer width input
122 uint16_t int_base = 0; // 16 bit integer base input
123 uint16_t int_height = 0; // 16 bit integer height input
124 uint16_t int_expected = 0; // 16 bit integer expected output
125 uint16_t int_area = 0; // 16 bit integer output
126
127 float float_length = NAN; // float length input
128 float float_expected = NAN; // float expected output
129 float float_area = NAN; // float output
130
131 double double_length = NAN; // double length input
132 double double_width = NAN; // double width input
133 double double_radius = NAN; // double radius input
134 double double_height = NAN; // double height input
135 double double_expected = NAN; // double expected output
136 double double_area = NAN; // double output
137
138 // 1st test
139 int_length = 5;
140 int_expected = 25;
141 int_area = math::square_area(int_length);
142
143 std::cout << "AREA OF A SQUARE (int)" << std::endl;
144 std::cout << "Input Length: " << int_length << std::endl;
145 std::cout << "Expected Output: " << int_expected << std::endl;
146 std::cout << "Output: " << int_area << std::endl;
147 assert(int_area == int_expected);
148 std::cout << "TEST PASSED" << std::endl << std::endl;
149
150 // 2nd test
151 float_length = 2.5;
152 float_expected = 6.25;
153 float_area = math::square_area(float_length);
154
155 std::cout << "AREA OF A SQUARE (float)" << std::endl;
156 std::cout << "Input Length: " << float_length << std::endl;
157 std::cout << "Expected Output: " << float_expected << std::endl;
158 std::cout << "Output: " << float_area << std::endl;
159 assert(float_area == float_expected);
160 std::cout << "TEST PASSED" << std::endl << std::endl;
161
162 // 3rd test
163 int_length = 4;
164 int_width = 7;
165 int_expected = 28;
166 int_area = math::rect_area(int_length, int_width);
167
168 std::cout << "AREA OF A RECTANGLE (int)" << std::endl;
169 std::cout << "Input Length: " << int_length << std::endl;
170 std::cout << "Input Width: " << int_width << std::endl;
171 std::cout << "Expected Output: " << int_expected << std::endl;
172 std::cout << "Output: " << int_area << std::endl;
173 assert(int_area == int_expected);
174 std::cout << "TEST PASSED" << std::endl << std::endl;
175
176 // 4th test
177 double_length = 2.5;
178 double_width = 5.7;
179 double_expected = 14.25;
180 double_area = math::rect_area(double_length, double_width);
181
182 std::cout << "AREA OF A RECTANGLE (double)" << std::endl;
183 std::cout << "Input Length: " << double_length << std::endl;
184 std::cout << "Input Width: " << double_width << std::endl;
185 std::cout << "Expected Output: " << double_expected << std::endl;
186 std::cout << "Output: " << double_area << std::endl;
187 assert(double_area == double_expected);
188 std::cout << "TEST PASSED" << std::endl << std::endl;
189
190 // 5th test
191 int_base = 10;
192 int_height = 3;
193 int_expected = 15;
194 int_area = math::triangle_area(int_base, int_height);
195
196 std::cout << "AREA OF A TRIANGLE" << std::endl;
197 std::cout << "Input Base: " << int_base << std::endl;
198 std::cout << "Input Height: " << int_height << std::endl;
199 std::cout << "Expected Output: " << int_expected << std::endl;
200 std::cout << "Output: " << int_area << std::endl;
201 assert(int_area == int_expected);
202 std::cout << "TEST PASSED" << std::endl << std::endl;
203
204 // 6th test
205 double_radius = 6;
206 double_expected =
207 113.09733552923255; // rounded down because the double datatype
208 // truncates after 14 decimal places
209 double_area = math::circle_area(double_radius);
210
211 std::cout << "AREA OF A CIRCLE" << std::endl;
212 std::cout << "Input Radius: " << double_radius << std::endl;
213 std::cout << "Expected Output: " << double_expected << std::endl;
214 std::cout << "Output: " << double_area << std::endl;
215 assert(double_area == double_expected);
216 std::cout << "TEST PASSED" << std::endl << std::endl;
217
218 // 7th test
219 int_base = 6;
220 int_height = 7;
221 int_expected = 42;
222 int_area = math::parallelogram_area(int_base, int_height);
223
224 std::cout << "AREA OF A PARALLELOGRAM" << std::endl;
225 std::cout << "Input Base: " << int_base << std::endl;
226 std::cout << "Input Height: " << int_height << std::endl;
227 std::cout << "Expected Output: " << int_expected << std::endl;
228 std::cout << "Output: " << int_area << std::endl;
229 assert(int_area == int_expected);
230 std::cout << "TEST PASSED" << std::endl << std::endl;
231
232 // 8th test
233 double_length = 5.5;
234 double_expected = 181.5;
235 double_area = math::cube_surface_area(double_length);
236
237 std::cout << "SURFACE AREA OF A CUBE" << std::endl;
238 std::cout << "Input Length: " << double_length << std::endl;
239 std::cout << "Expected Output: " << double_expected << std::endl;
240 std::cout << "Output: " << double_area << std::endl;
241 assert(double_area == double_expected);
242 std::cout << "TEST PASSED" << std::endl << std::endl;
243
244 // 9th test
245 double_radius = 10.0;
246 double_expected = 1256.6370614359172; // rounded down because the whole
247 // value gets truncated
248 double_area = math::sphere_surface_area(double_radius);
249
250 std::cout << "SURFACE AREA OF A SPHERE" << std::endl;
251 std::cout << "Input Radius: " << double_radius << std::endl;
252 std::cout << "Expected Output: " << double_expected << std::endl;
253 std::cout << "Output: " << double_area << std::endl;
254 assert(double_area == double_expected);
255 std::cout << "TEST PASSED" << std::endl << std::endl;
256
257 // 10th test
258 double_radius = 4.0;
259 double_height = 7.0;
260 double_expected = 276.46015351590177;
261 double_area = math::cylinder_surface_area(double_radius, double_height);
262
263 std::cout << "SURFACE AREA OF A CYLINDER" << std::endl;
264 std::cout << "Input Radius: " << double_radius << std::endl;
265 std::cout << "Input Height: " << double_height << std::endl;
266 std::cout << "Expected Output: " << double_expected << std::endl;
267 std::cout << "Output: " << double_area << std::endl;
268 assert(double_area == double_expected);
269 std::cout << "TEST PASSED" << std::endl << std::endl;
270}
T endl(T... args)
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
Here is the call graph for this function: