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

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

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

Go to the source code of this file.

Namespaces

namespace  math
 for assert
 

Functions

template<typename T >
math::square_perimeter (T length)
 perimeter of a square (4 * l)
 
template<typename T >
math::rect_perimeter (T length, T width)
 perimeter of a rectangle ( 2(l + w) )
 
template<typename T >
math::triangle_perimeter (T base, T height, T hypotenuse)
 perimeter of a triangle (a + b + c)
 
template<typename T >
math::circle_perimeter (T radius)
 perimeter of a circle (2 * pi * r)
 
template<typename T >
math::parallelogram_perimeter (T base, T height)
 perimeter of a parallelogram 2(b + h)
 
template<typename T >
math::cube_surface_perimeter (T length)
 surface perimeter of a cube ( 12
 
template<typename T >
math::n_polygon_surface_perimeter (T sides, T length)
 surface perimeter of a n-polygon ( n * l)
 
template<typename T >
math::cylinder_surface_perimeter (T radius, T height)
 surface perimeter of a cylinder (2 * radius + 2 * height)
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementations for the perimeter of various shapes.

The of a shape is the amount of 2D space it takes up. All shapes have a formula for their perimeter. These implementations support multiple return types.

Author
OGscorpion

Definition in file perimeter.cpp.

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES

Definition at line 11 of file perimeter.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 284 of file perimeter.cpp.

284 {
285 test(); // run self-test implementations
286 return 0;
287}
static void test()
Self-test implementations.

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void

Definition at line 120 of file perimeter.cpp.

120 {
121 // I/O variables for testing
122 uint16_t int_length = 0; // 16 bit integer length input
123 uint16_t int_width = 0; // 16 bit integer width input
124 uint16_t int_base = 0; // 16 bit integer base input
125 uint16_t int_height = 0; // 16 bit integer height input
126 uint16_t int_hypotenuse = 0; // 16 bit integer hypotenuse input
127 uint16_t int_sides = 0; // 16 bit integer sides input
128 uint16_t int_expected = 0; // 16 bit integer expected output
129 uint16_t int_perimeter = 0; // 16 bit integer output
130
131 float float_length = NAN; // float length input
132 float float_expected = NAN; // float expected output
133 float float_perimeter = NAN; // float output
134
135 double double_length = NAN; // double length input
136 double double_width = NAN; // double width input
137 double double_radius = NAN; // double radius input
138 double double_height = NAN; // double height input
139 double double_expected = NAN; // double expected output
140 double double_perimeter = NAN; // double output
141
142 // 1st test
143 int_length = 5;
144 int_expected = 20;
145 int_perimeter = math::square_perimeter(int_length);
146
147 std::cout << "perimeter OF A SQUARE (int)" << std::endl;
148 std::cout << "Input Length: " << int_length << std::endl;
149 std::cout << "Expected Output: " << int_expected << std::endl;
150 std::cout << "Output: " << int_perimeter << std::endl;
151 assert(int_perimeter == int_expected);
152 std::cout << "TEST PASSED" << std::endl << std::endl;
153
154 // 2nd test
155 float_length = 2.5;
156 float_expected = 10;
157 float_perimeter = math::square_perimeter(float_length);
158
159 std::cout << "perimeter OF A SQUARE (float)" << std::endl;
160 std::cout << "Input Length: " << float_length << std::endl;
161 std::cout << "Expected Output: " << float_expected << std::endl;
162 std::cout << "Output: " << float_perimeter << std::endl;
163 assert(float_perimeter == float_expected);
164 std::cout << "TEST PASSED" << std::endl << std::endl;
165
166 // 3rd test
167 int_length = 4;
168 int_width = 7;
169 int_expected = 22;
170 int_perimeter = math::rect_perimeter(int_length, int_width);
171
172 std::cout << "perimeter OF A RECTANGLE (int)" << std::endl;
173 std::cout << "Input Length: " << int_length << std::endl;
174 std::cout << "Input Width: " << int_width << std::endl;
175 std::cout << "Expected Output: " << int_expected << std::endl;
176 std::cout << "Output: " << int_perimeter << std::endl;
177 assert(int_perimeter == int_expected);
178 std::cout << "TEST PASSED" << std::endl << std::endl;
179
180 // 4th test
181 double_length = 2.5;
182 double_width = 5.7;
183 double_expected = 16.4;
184 double_perimeter = math::rect_perimeter(double_length, double_width);
185
186 std::cout << "perimeter OF A RECTANGLE (double)" << std::endl;
187 std::cout << "Input Length: " << double_length << std::endl;
188 std::cout << "Input Width: " << double_width << std::endl;
189 std::cout << "Expected Output: " << double_expected << std::endl;
190 std::cout << "Output: " << double_perimeter << std::endl;
191 assert(double_perimeter == double_expected);
192 std::cout << "TEST PASSED" << std::endl << std::endl;
193
194 // 5th test
195 int_base = 10;
196 int_height = 3;
197 int_hypotenuse = 5;
198 int_expected = 18;
199 int_perimeter =
200 math::triangle_perimeter(int_base, int_height, int_hypotenuse);
201
202 std::cout << "perimeter OF A TRIANGLE" << std::endl;
203 std::cout << "Input Base: " << int_base << std::endl;
204 std::cout << "Input Height: " << int_height << std::endl;
205 std::cout << "Expected Output: " << int_expected << std::endl;
206 std::cout << "Output: " << int_perimeter << std::endl;
207 assert(int_perimeter == int_expected);
208 std::cout << "TEST PASSED" << std::endl << std::endl;
209
210 // 6th test
211 double_radius = 6;
212 double_expected =
213 37.69911184307752; // rounded down because the double datatype
214 // truncates after 14 decimal places
215 double_perimeter = math::circle_perimeter(double_radius);
216
217 std::cout << "perimeter OF A CIRCLE" << std::endl;
218 std::cout << "Input Radius: " << double_radius << std::endl;
219 std::cout << "Expected Output: " << double_expected << std::endl;
220 std::cout << "Output: " << double_perimeter << std::endl;
221 assert(double_perimeter == double_expected);
222 std::cout << "TEST PASSED" << std::endl << std::endl;
223
224 // 7th test
225 int_base = 6;
226 int_height = 7;
227 int_expected = 26;
228 int_perimeter = math::parallelogram_perimeter(int_base, int_height);
229
230 std::cout << "perimeter OF A PARALLELOGRAM" << std::endl;
231 std::cout << "Input Base: " << int_base << std::endl;
232 std::cout << "Input Height: " << int_height << std::endl;
233 std::cout << "Expected Output: " << int_expected << std::endl;
234 std::cout << "Output: " << int_perimeter << std::endl;
235 assert(int_perimeter == int_expected);
236 std::cout << "TEST PASSED" << std::endl << std::endl;
237
238 // 8th test
239 double_length = 5.5;
240 double_expected = 66.0;
241 double_perimeter = math::cube_surface_perimeter(double_length);
242
243 std::cout << "SURFACE perimeter OF A CUBE" << std::endl;
244 std::cout << "Input Length: " << double_length << std::endl;
245 std::cout << "Expected Output: " << double_expected << std::endl;
246 std::cout << "Output: " << double_perimeter << std::endl;
247 assert(double_perimeter == double_expected);
248 std::cout << "TEST PASSED" << std::endl << std::endl;
249
250 // 9th test
251 int_sides = 7;
252 int_length = 10;
253 int_expected = 70;
254 int_perimeter = math::n_polygon_surface_perimeter(int_sides, int_length);
255
256 std::cout << "SURFACE perimeter OF A N-POLYGON" << std::endl;
257 std::cout << "Input Sides: " << int_sides << std::endl;
258 std::cout << "Input Length: " << int_length << std::endl;
259 std::cout << "Expected Output: " << int_expected << std::endl;
260 std::cout << "Output: " << int_perimeter << std::endl;
261 assert(int_perimeter == int_expected);
262 std::cout << "TEST PASSED" << std::endl << std::endl;
263
264 // 10th test
265 double_radius = 4.0;
266 double_height = 7.0;
267 double_expected = 22.0;
268 double_perimeter =
269 math::cylinder_surface_perimeter(double_radius, double_height);
270
271 std::cout << "SURFACE perimeter OF A CYLINDER" << std::endl;
272 std::cout << "Input Radius: " << double_radius << std::endl;
273 std::cout << "Input Height: " << double_height << std::endl;
274 std::cout << "Expected Output: " << double_expected << std::endl;
275 std::cout << "Output: " << double_perimeter << std::endl;
276 assert(double_perimeter == double_expected);
277 std::cout << "TEST PASSED" << std::endl << std::endl;
278}
T parallelogram_perimeter(T base, T height)
perimeter of a parallelogram 2(b + h)
Definition perimeter.cpp:75
T cylinder_surface_perimeter(T radius, T height)
surface perimeter of a cylinder (2 * radius + 2 * height)
T triangle_perimeter(T base, T height, T hypotenuse)
perimeter of a triangle (a + b + c)
Definition perimeter.cpp:52
T rect_perimeter(T length, T width)
perimeter of a rectangle ( 2(l + w) )
Definition perimeter.cpp:40
T cube_surface_perimeter(T length)
surface perimeter of a cube ( 12
Definition perimeter.cpp:86
T square_perimeter(T length)
perimeter of a square (4 * l)
Definition perimeter.cpp:28
T n_polygon_surface_perimeter(T sides, T length)
surface perimeter of a n-polygon ( n * l)
Definition perimeter.cpp:99
T circle_perimeter(T radius)
perimeter of a circle (2 * pi * r)
Definition perimeter.cpp:63