Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
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 IO operations
 

Functions

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

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
235 {
236 test(); // run self-test implementations
237 return 0;
238}
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
112 {
113 // Input variables
114 uint32_t int_length = 0; // 32 bit integer length input
115 uint32_t int_width = 0; // 32 bit integer width input
116 uint32_t int_base = 0; // 32 bit integer base input
117 uint32_t int_height = 0; // 32 bit integer height input
118 uint32_t int_depth = 0; // 32 bit integer depth input
119
120 double double_radius = NAN; // double radius input
121 double double_height = NAN; // double height input
122
123 // Output variables
124 uint32_t int_expected = 0; // 32 bit integer expected output
125 uint32_t int_volume = 0; // 32 bit integer output
126
127 double double_expected = NAN; // double expected output
128 double double_volume = NAN; // double output
129
130 // 1st test
131 int_length = 5;
132 int_expected = 125;
133 int_volume = math::cube_volume(int_length);
134
135 std::cout << "VOLUME OF A CUBE" << std::endl;
136 std::cout << "Input Length: " << int_length << std::endl;
137 std::cout << "Expected Output: " << int_expected << std::endl;
138 std::cout << "Output: " << int_volume << std::endl;
139 assert(int_volume == int_expected);
140 std::cout << "TEST PASSED" << std::endl << std::endl;
141
142 // 2nd test
143 int_length = 4;
144 int_width = 3;
145 int_height = 5;
146 int_expected = 60;
147 int_volume = math::rect_prism_volume(int_length, int_width, int_height);
148
149 std::cout << "VOLUME OF A RECTANGULAR PRISM" << std::endl;
150 std::cout << "Input Length: " << int_length << std::endl;
151 std::cout << "Input Width: " << int_width << std::endl;
152 std::cout << "Input Height: " << int_height << std::endl;
153 std::cout << "Expected Output: " << int_expected << std::endl;
154 std::cout << "Output: " << int_volume << std::endl;
155 assert(int_volume == int_expected);
156 std::cout << "TEST PASSED" << std::endl << std::endl;
157
158 // 3rd test
159 double_radius = 5;
160 double_height = 7;
161 double_expected = 183.16666666666666; // truncated to 14 decimal places
162 double_volume = math::cone_volume(double_radius, double_height);
163
164 std::cout << "VOLUME OF A CONE" << std::endl;
165 std::cout << "Input Radius: " << double_radius << std::endl;
166 std::cout << "Input Height: " << double_height << std::endl;
167 std::cout << "Expected Output: " << double_expected << std::endl;
168 std::cout << "Output: " << double_volume << std::endl;
169 assert(double_volume == double_expected);
170 std::cout << "TEST PASSED" << std::endl << std::endl;
171
172 // 4th test
173 int_base = 3;
174 int_height = 4;
175 int_depth = 5;
176 int_expected = 30;
177 int_volume = math::triangle_prism_volume(int_base, int_height, int_depth);
178
179 std::cout << "VOLUME OF A TRIANGULAR PRISM" << std::endl;
180 std::cout << "Input Base: " << int_base << std::endl;
181 std::cout << "Input Height: " << int_height << std::endl;
182 std::cout << "Input Depth: " << int_depth << std::endl;
183 std::cout << "Expected Output: " << int_expected << std::endl;
184 std::cout << "Output: " << int_volume << std::endl;
185 assert(int_volume == int_expected);
186 std::cout << "TEST PASSED" << std::endl << std::endl;
187
188 // 5th test
189 int_length = 10;
190 int_width = 3;
191 int_height = 5;
192 int_expected = 50;
193 int_volume = math::pyramid_volume(int_length, int_width, int_height);
194
195 std::cout << "VOLUME OF A PYRAMID" << std::endl;
196 std::cout << "Input Length: " << int_length << std::endl;
197 std::cout << "Input Width: " << int_width << 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_volume << std::endl;
201 assert(int_volume == int_expected);
202 std::cout << "TEST PASSED" << std::endl << std::endl;
203
204 // 6th test
205 double_radius = 3;
206 double_expected = 113.04;
207 double_volume = math::sphere_volume(double_radius);
208
209 std::cout << "VOLUME OF A SPHERE" << std::endl;
210 std::cout << "Input Radius: " << double_radius << std::endl;
211 std::cout << "Expected Output: " << double_expected << std::endl;
212 std::cout << "Output: " << double_volume << std::endl;
213 assert(double_volume == double_expected);
214 std::cout << "TEST PASSED" << std::endl << std::endl;
215
216 // 7th test
217 double_radius = 5;
218 double_height = 2;
219 double_expected = 157;
220 double_volume = math::cylinder_volume(double_radius, double_height);
221
222 std::cout << "VOLUME OF A CYLINDER" << std::endl;
223 std::cout << "Input Radius: " << double_radius << std::endl;
224 std::cout << "Input Height: " << double_height << std::endl;
225 std::cout << "Expected Output: " << double_expected << std::endl;
226 std::cout << "Output: " << double_volume << std::endl;
227 assert(double_volume == double_expected);
228 std::cout << "TEST PASSED" << std::endl << std::endl;
229}
T endl(T... args)
T triangle_prism_volume(T base, T height, T depth)
The volume of a triangular prism.
Definition volume.cpp:67
T sphere_volume(T radius, double PI=3.14)
The volume of a sphere
Definition volume.cpp:91
T rect_prism_volume(T length, T width, T height)
The volume of a rectangular prism.
Definition volume.cpp:41
T cone_volume(T radius, T height, double PI=3.14)
The volume of a cone
Definition volume.cpp:53
T pyramid_volume(T length, T width, T height)
The volume of a pyramid
Definition volume.cpp:80
T cylinder_volume(T radius, T height, double PI=3.14)
The volume of a cylinder
Definition volume.cpp:103
T cube_volume(T length)
The volume of a cube
Definition volume.cpp:28
Here is the call graph for this function: