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

Loading...
Searching...
No Matches
volume.cpp File Reference

Implmentations for the volume of various 3D shapes. More...

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

## Namespaces

namespace | math
| for std::rand

## Functions

template<typename T >
T | math::cube_volume (T length)
| The volume of a cube More...
template<typename T >
T | math::rect_prism_volume (T length, T width, T height)
| The volume of a rectangular prism. More...
template<typename T >
T | math::cone_volume (T radius, T height, double PI=3.14)
| The volume of a cone More...
template<typename T >
T | math::triangle_prism_volume (T base, T height, T depth)
| The volume of a triangular prism. More...
template<typename T >
T | math::pyramid_volume (T length, T width, T height)
| The volume of a pyramid More...
template<typename T >
T | math::sphere_volume (T radius, double PI=3.14)
| The volume of a sphere More...
template<typename T >
T | math::cylinder_volume (T radius, T height, double PI=3.14)
| The volume of a cylinder More...
static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Detailed Description

Implmentations for the volume of various 3D shapes.

The volume of a 3D shape is the amount of 3D space that the shape takes up. All shapes have a formula to get the volume 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: volume.cpp:112
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
// Input variables
uint32_t int_length = 0; // 32 bit integer length input
uint32_t int_width = 0; // 32 bit integer width input
uint32_t int_base = 0; // 32 bit integer base input
uint32_t int_height = 0; // 32 bit integer height input
uint32_t int_depth = 0; // 32 bit integer depth input
double double_radius = NAN; // double radius input
double double_height = NAN; // double height input
// Output variables
uint32_t int_expected = 0; // 32 bit integer expected output
uint32_t int_volume = 0; // 32 bit integer output
double double_expected = NAN; // double expected output
double double_volume = NAN; // double output
// 1st test
int_length = 5;
int_expected = 125;
int_volume = math::cube_volume(int_length);
std::cout << "VOLUME OF A CUBE" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 2nd test
int_length = 4;
int_width = 3;
int_height = 5;
int_expected = 60;
int_volume = math::rect_prism_volume(int_length, int_width, int_height);
std::cout << "VOLUME OF A RECTANGULAR PRISM" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Input Width: " << int_width << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 3rd test
double_radius = 5;
double_height = 7;
double_expected = 183.16666666666666; // truncated to 14 decimal places
double_volume = math::cone_volume(double_radius, double_height);
std::cout << "VOLUME OF A CONE" << 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_volume << std::endl;
assert(double_volume == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 4th test
int_base = 3;
int_height = 4;
int_depth = 5;
int_expected = 30;
int_volume = math::triangle_prism_volume(int_base, int_height, int_depth);
std::cout << "VOLUME OF A TRIANGULAR PRISM" << std::endl;
std::cout << "Input Base: " << int_base << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Input Depth: " << int_depth << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 5th test
int_length = 10;
int_width = 3;
int_height = 5;
int_expected = 50;
int_volume = math::pyramid_volume(int_length, int_width, int_height);
std::cout << "VOLUME OF A PYRAMID" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Input Width: " << int_width << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 6th test
double_radius = 3;
double_expected = 113.04;
double_volume = math::sphere_volume(double_radius);
std::cout << "VOLUME 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_volume << std::endl;
assert(double_volume == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
// 7th test
double_radius = 5;
double_height = 2;
double_expected = 157;
double_volume = math::cylinder_volume(double_radius, double_height);
std::cout << "VOLUME 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_volume << std::endl;
assert(double_volume == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
}
std::cout
std::endl
T endl(T... args)
math::triangle_prism_volume
T triangle_prism_volume(T base, T height, T depth)
The volume of a triangular prism.
Definition: volume.cpp:67
math::sphere_volume
T sphere_volume(T radius, double PI=3.14)
The volume of a sphere
Definition: volume.cpp:91
math::rect_prism_volume
T rect_prism_volume(T length, T width, T height)
The volume of a rectangular prism.
Definition: volume.cpp:41
math::cone_volume
T cone_volume(T radius, T height, double PI=3.14)
The volume of a cone
Definition: volume.cpp:53
math::pyramid_volume
T pyramid_volume(T length, T width, T height)
The volume of a pyramid
Definition: volume.cpp:80
math::cylinder_volume
T cylinder_volume(T radius, T height, double PI=3.14)
The volume of a cylinder
Definition: volume.cpp:103
math::cube_volume
T cube_volume(T length)
The volume of a cube
Definition: volume.cpp:28
Here is the call graph for this function: