Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
area.cpp File Reference

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

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

Namespaces

namespace  math
 for assert
 

Functions

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

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
271 {
272 test(); // run self-test implementations
273 return 0;
274}
static void test()
Self-test implementations.
Definition: area.cpp:116
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
116 {
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}
T endl(T... args)
T circle_area(T radius)
area of a circle (pi
Definition: area.cpp:61
T parallelogram_area(T base, T height)
area of a parallelogram (b * h)
Definition: area.cpp:73
T square_area(T length)
area of a square (l * l)
Definition: area.cpp:27
T rect_area(T length, T width)
area of a rectangle (l * w)
Definition: area.cpp:38
T triangle_area(T base, T height)
area of a triangle (b * h / 2)
Definition: area.cpp:50
T sphere_surface_area(T radius)
surface area of a sphere ( 4 * pi * r^2)
Definition: area.cpp:95
T cube_surface_area(T length)
surface area of a cube ( 6 * (l
Definition: area.cpp:84
T cylinder_surface_area(T radius, T height)
surface area of a cylinder (2 * pi * r * h + 2 * pi * r^2)
Definition: area.cpp:107
Here is the call graph for this function: