TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
volume.cpp
Go to the documentation of this file.
1
12#include <cassert>
13#include <cmath>
14#include <cstdint>
15#include <iostream>
16
21namespace math {
27template <typename T>
28T cube_volume(T length) {
29 return std::pow(length, 3);
30}
31
40template <typename T>
41T rect_prism_volume(T length, T width, T height) {
42 return length * width * height;
43}
44
52template <typename T>
53T cone_volume(T radius, T height, double PI = 3.14) {
54 return std::pow(radius, 2) * PI * height / 3;
55}
56
66template <typename T>
67T triangle_prism_volume(T base, T height, T depth) {
68 return base * height * depth / 2;
69}
70
79template <typename T>
80T pyramid_volume(T length, T width, T height) {
81 return length * width * height / 3;
82}
83
90template <typename T>
91T sphere_volume(T radius, double PI = 3.14) {
92 return PI * std::pow(radius, 3) * 4 / 3;
93}
94
102template <typename T>
103T cylinder_volume(T radius, T height, double PI = 3.14) {
104 return PI * std::pow(radius, 2) * height;
105}
106} // namespace math
107
112static void test() {
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}
230
235int main() {
236 test(); // run self-test implementations
237 return 0;
238}
int height(node *root)
Definition avltree.cpp:38
for assert
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
static void test()
Self-test implementations.
Definition volume.cpp:112
int main()
Main function.
Definition volume.cpp:235