Skip to content

Commit

Permalink
main files
Browse files Browse the repository at this point in the history
  • Loading branch information
woflydev committed Aug 2, 2022
1 parent eb69ef2 commit 1c6da79
Show file tree
Hide file tree
Showing 3 changed files with 478 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cargo.toml
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"
67 changes: 67 additions & 0 deletions src/cylinder.rs
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();
}
Loading

0 comments on commit 1c6da79

Please sign in to comment.