-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
478 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
default-run = "main" | ||
name = "math_calculations" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "main" | ||
path = "src/main.rs" | ||
|
||
[[bin]] | ||
name = "cylinder" | ||
path = "src/cylinder.rs" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
indicatif = "0.17.0" | ||
duration-string = "0.1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::collections::HashMap; | ||
|
||
const SYSTEM_PI:f64 = std::f64::consts::PI; | ||
|
||
fn main() { | ||
let mut current_iteration = 0; | ||
let radius_iteration_amount = 4; | ||
let height_iteration_amount = 4; | ||
let radius_increment = 1.; | ||
let height_increment = 1.; | ||
|
||
let mut current_radius:f64 = 1.; | ||
let mut current_height:f64 = 1.; | ||
|
||
let baseline_height:f64 = current_height; | ||
|
||
let mut hash = HashMap::new(); | ||
|
||
for _x in 1..radius_iteration_amount { | ||
for _y in 1..height_iteration_amount { | ||
hash.insert(current_iteration, calculate(current_radius, current_height)); | ||
|
||
current_height += height_increment; | ||
|
||
// update iteration | ||
current_iteration += 1; | ||
} | ||
|
||
// reset the height so we can iterate again | ||
current_height = baseline_height; | ||
|
||
// update radius to account for all combinations | ||
current_radius += radius_increment; | ||
} | ||
|
||
// makes a mutable copy of the hashmap for memory safety | ||
print_all_values(&mut hash); | ||
} | ||
|
||
fn calculate(radius:f64, height:f64) -> (f64, f64) { | ||
let surface_area = (2 as f64 * SYSTEM_PI * radius * height) + 2 as f64 * SYSTEM_PI * radius.powi(2); | ||
let volume = SYSTEM_PI * radius.powi(2) * height; | ||
|
||
let result_tuple = (surface_area, volume); | ||
|
||
let converted = convert_to_cm(result_tuple); | ||
|
||
return converted; | ||
} | ||
|
||
fn convert_to_cm(tuple:(f64, f64)) -> (f64, f64) { | ||
let root:f64 = 10.; | ||
|
||
let cm_surface_area = tuple.0 / root.powi(2); | ||
let cm_volume = tuple.1 / root.powi(3); | ||
|
||
return (cm_surface_area, cm_volume); | ||
} | ||
|
||
fn print_all_values(map: &mut HashMap<i32, (f64, f64)>) { | ||
for (key, value) in &*map { | ||
//println!("Radius: {:} || Height: {:} || Final Surface Area: {:}", radius, height, result); | ||
println!("Key: {:} || Final Surface Area: {:} || Final Volume: {:}", key, value.0, value.1); | ||
} | ||
|
||
map.clear(); | ||
} |
Oops, something went wrong.