# Algorithms_in_C++: math/area.cpp File Reference

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 std::rand

## Functions

template<typename T >
T | math::square_area (T length)
| area of a square (l * l) More...
template<typename T >
T | math::rect_area (T length, T width)
| area of a rectangle (l * w) More...
template<typename T >
T | math::triangle_area (T base, T height)
| area of a triangle (b * h / 2) More...
template<typename T >
T | math::circle_area (T radius)
| area of a circle (pi More...
template<typename T >
T | math::parallelogram_area (T base, T height)
| area of a parallelogram (b * h) More...
template<typename T >
T | math::cube_surface_area (T length)
| surface area of a cube ( 6 * (l More...
template<typename T >
T | math::sphere_surface_area (T radius)
| surface area of a sphere ( 4 * pi * r^2) More...
template<typename T >
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

{
test(); // run self-test implementations
return 0;
}
test
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

{
// I/O variables for testing
uint16_t int_length = 0; // 16 bit integer length input
uint16_t int_width = 0; // 16 bit integer width input
uint16_t int_base = 0; // 16 bit integer base input
uint16_t int_height = 0; // 16 bit integer height input
uint16_t int_expected = 0; // 16 bit integer expected output
uint16_t int_area = 0; // 16 bit integer output
float float_length = NAN; // float length input
float float_expected = NAN; // float expected output
float float_area = NAN; // float output
double double_length = NAN; // double length input
double double_width = NAN; // double width input
double double_radius = NAN; // double radius input
double double_height = NAN; // double height input
double double_expected = NAN; // double expected output
double double_area = NAN; // double output
// 1st test
int_length = 5;
int_expected = 25;
int_area = math::square_area(int_length);
std::cout << "AREA OF A SQUARE (int)" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_area << std::endl;
assert(int_area == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 2nd test
float_length = 2.5;
float_expected = 6.25;
float_area = math::square_area(float_length);
std::cout << "AREA OF A SQUARE (float)" << std::endl;
std::cout << "Input Length: " << float_length << std::endl;
std::cout << "Expected Output: " << float_expected << std::endl;
std::cout << "Output: " << float_area << std::endl;
assert(float_area == float_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 3rd test
int_length = 4;
int_width = 7;
int_expected = 28;
int_area = math::rect_area(int_length, int_width);
std::cout << "AREA OF A RECTANGLE (int)" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Input Width: " << int_width << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_area << std::endl;
assert(int_area == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 4th test
double_length = 2.5;
double_width = 5.7;
double_expected = 14.25;
double_area = math::rect_area(double_length, double_width);
std::cout << "AREA OF A RECTANGLE (double)" << std::endl;
std::cout << "Input Length: " << double_length << std::endl;
std::cout << "Input Width: " << double_width << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_area << std::endl;
assert(double_area == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 5th test
int_base = 10;
int_height = 3;
int_expected = 15;
int_area = math::triangle_area(int_base, int_height);
std::cout << "AREA OF A TRIANGLE" << std::endl;
std::cout << "Input Base: " << int_base << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_area << std::endl;
assert(int_area == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 6th test
double_radius = 6;
double_expected =
113.09733552923255; // rounded down because the double datatype
// truncates after 14 decimal places
double_area = math::circle_area(double_radius);
std::cout << "AREA OF A CIRCLE" << std::endl;
std::cout << "Input Radius: " << double_radius << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_area << std::endl;
assert(double_area == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 7th test
int_base = 6;
int_height = 7;
int_expected = 42;
int_area = math::parallelogram_area(int_base, int_height);
std::cout << "AREA OF A PARALLELOGRAM" << std::endl;
std::cout << "Input Base: " << int_base << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_area << std::endl;
assert(int_area == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 8th test
double_length = 5.5;
double_expected = 181.5;
double_area = math::cube_surface_area(double_length);
std::cout << "SURFACE AREA OF A CUBE" << std::endl;
std::cout << "Input Length: " << double_length << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_area << std::endl;
assert(double_area == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 9th test
double_radius = 10.0;
double_expected = 1256.6370614359172; // rounded down because the whole
// value gets truncated
double_area = math::sphere_surface_area(double_radius);
std::cout << "SURFACE AREA OF A SPHERE" << std::endl;
std::cout << "Input Radius: " << double_radius << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_area << std::endl;
assert(double_area == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 10th test
double_radius = 4.0;
double_height = 7.0;
double_expected = 276.46015351590177;
double_area = math::cylinder_surface_area(double_radius, double_height);
std::cout << "SURFACE AREA OF A CYLINDER" << std::endl;
std::cout << "Input Radius: " << double_radius << std::endl;
std::cout << "Input Height: " << double_height << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_area << std::endl;
assert(double_area == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
}
std::cout
std::endl
T endl(T... args)
math::circle_area
T circle_area(T radius)
area of a circle (pi
Definition: area.cpp:63
math::parallelogram_area
T parallelogram_area(T base, T height)
area of a parallelogram (b * h)
Definition: area.cpp:75
math::square_area
T square_area(T length)
area of a square (l * l)
Definition: area.cpp:29
math::rect_area
T rect_area(T length, T width)
area of a rectangle (l * w)
Definition: area.cpp:40
math::triangle_area
T triangle_area(T base, T height)
area of a triangle (b * h / 2)
Definition: area.cpp:52
math::sphere_surface_area
T sphere_surface_area(T radius)
surface area of a sphere ( 4 * pi * r^2)
Definition: area.cpp:97
math::cube_surface_area
T cube_surface_area(T length)
surface area of a cube ( 6 * (l
Definition: area.cpp:86
math::cylinder_surface_area
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: