Initial commit.

This commit is contained in:
winneratwin 2022-02-03 10:38:54 +00:00
commit cd24377d03
Signed by: winneratwin
GPG Key ID: 5F9280186EA11AC6
4 changed files with 50 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "standard-deviation"
version = "0.1.0"

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "standard-deviation"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

34
src/main.rs Normal file
View File

@ -0,0 +1,34 @@
fn main() {
let sample: Vec<i32> = vec![46, 69, 32, 60, 52, 41];
let mut sample_sorted: Vec<i32> = sample.clone();
sample_sorted.sort();
let sample_len = sample.len() as i32;
let sample_sum = sample.iter().sum::<i32>();
let sample_mean = sample_sum / sample_len;
let sample_median = sample_sorted[(sample_len / 2) as usize];
let standard_deviation = (sample
.iter()
.map(|input| (input - sample_mean).pow(2))
.collect::<Vec<i32>>()
.iter()
.sum::<i32>() as f32
/ sample_len as f32)
.sqrt();
let sample_standard_deviation = (sample
.iter()
.map(|input| (input - sample_mean).pow(2))
.collect::<Vec<i32>>()
.iter()
.sum::<i32>() as f32
/ (sample_len - 1) as f32)
.sqrt();
println!("sample size: {}", sample_len);
println!("sample sum: {}", sample_sum);
println!("sample median: {}", sample_median);
println!("sample average: {}", sample_mean);
println!("standard deviation: {}", standard_deviation);
println!(
"standard deviation (sample conservitive estimate): {}",
sample_standard_deviation
);
}