Pure Programmer
Blue Matrix


Cluster Map

Project: Sample Mean and Standard Deviation

Write a program that computes the [[Sample_mean_and_covariance|mean]] (or average) and [[standard deviation]] of a sample of N random floating point values in the interval [0,1). This closed/open [[Interval_(mathematics)|interval]] notation means 0 is in the range but 1 is not. The sample size (N) should be passed as a command line argument. Print the mean and and standard deviation to six decimal places. Run the program multiple times to confirm that you get different results each time. Do you notice that the mean and standard deviation get closer to consistent values as you increase the sample size (N)? As N gets larger the mean should approach the mean of the generator at 0.5 and the standard deviation should approach 0.288675 if our generator is perfectly uniform.

Output
$ rustc SampleMeanAndStdDev.rs error: unknown format trait `f` --> SampleMeanAndStdDev.rs:36:44 | 36 | println!("{}", format!("Sample mean: {0:.6f}", mean)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> SampleMeanAndStdDev.rs:37:49 | 37 | println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> SampleMeanAndStdDev.rs:10:5 | 10 | if args.len() != 2 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> SampleMeanAndStdDev.rs:12:3 | 12 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> SampleMeanAndStdDev.rs:14:29 | 14 | let mut sampleSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> SampleMeanAndStdDev.rs:14:51 | 14 | let mut sampleSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `random` in this scope --> SampleMeanAndStdDev.rs:21:17 | 21 | samples.push(random()); | ^^^^^^ not found in this scope error[E0277]: the type `[f64]` cannot be indexed by `isize` --> SampleMeanAndStdDev.rs:22:19 | 22 | sum += samples[i]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[f64]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<f64>` to implement `Index<isize>` error[E0277]: the type `[f64]` cannot be indexed by `isize` --> SampleMeanAndStdDev.rs:31:28 | 31 | variance += pow(samples[i] - mean,2.0f64); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[f64]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<f64>` to implement `Index<isize>` error[E0425]: cannot find function `pow` in this scope --> SampleMeanAndStdDev.rs:31:16 | 31 | variance += pow(samples[i] - mean,2.0f64); | ^^^ not found in this scope error[E0425]: cannot find function `sqrt` in this scope --> SampleMeanAndStdDev.rs:37:54 | 37 | println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); | ^^^^ not found in this scope | help: use the `.` operator to call the method `sqrt` on `f64` | 37 - println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); 37 + println!("{}", format!("Sample std. dev.: {0:.6f}", variance.sqrt())); | error: aborting due to 11 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc SampleMeanAndStdDev.rs error: unknown format trait `f` --> SampleMeanAndStdDev.rs:36:44 | 36 | println!("{}", format!("Sample mean: {0:.6f}", mean)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> SampleMeanAndStdDev.rs:37:49 | 37 | println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> SampleMeanAndStdDev.rs:10:5 | 10 | if args.len() != 2 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> SampleMeanAndStdDev.rs:12:3 | 12 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> SampleMeanAndStdDev.rs:14:29 | 14 | let mut sampleSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> SampleMeanAndStdDev.rs:14:51 | 14 | let mut sampleSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `random` in this scope --> SampleMeanAndStdDev.rs:21:17 | 21 | samples.push(random()); | ^^^^^^ not found in this scope error[E0277]: the type `[f64]` cannot be indexed by `isize` --> SampleMeanAndStdDev.rs:22:19 | 22 | sum += samples[i]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[f64]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<f64>` to implement `Index<isize>` error[E0277]: the type `[f64]` cannot be indexed by `isize` --> SampleMeanAndStdDev.rs:31:28 | 31 | variance += pow(samples[i] - mean,2.0f64); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[f64]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<f64>` to implement `Index<isize>` error[E0425]: cannot find function `pow` in this scope --> SampleMeanAndStdDev.rs:31:16 | 31 | variance += pow(samples[i] - mean,2.0f64); | ^^^ not found in this scope error[E0425]: cannot find function `sqrt` in this scope --> SampleMeanAndStdDev.rs:37:54 | 37 | println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); | ^^^^ not found in this scope | help: use the `.` operator to call the method `sqrt` on `f64` | 37 - println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); 37 + println!("{}", format!("Sample std. dev.: {0:.6f}", variance.sqrt())); | error: aborting due to 11 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc SampleMeanAndStdDev.rs error: unknown format trait `f` --> SampleMeanAndStdDev.rs:36:44 | 36 | println!("{}", format!("Sample mean: {0:.6f}", mean)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> SampleMeanAndStdDev.rs:37:49 | 37 | println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> SampleMeanAndStdDev.rs:10:5 | 10 | if args.len() != 2 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> SampleMeanAndStdDev.rs:12:3 | 12 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> SampleMeanAndStdDev.rs:14:29 | 14 | let mut sampleSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> SampleMeanAndStdDev.rs:14:51 | 14 | let mut sampleSize:isize = Utils.stoiWithDefault(args[1], 10); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `random` in this scope --> SampleMeanAndStdDev.rs:21:17 | 21 | samples.push(random()); | ^^^^^^ not found in this scope error[E0277]: the type `[f64]` cannot be indexed by `isize` --> SampleMeanAndStdDev.rs:22:19 | 22 | sum += samples[i]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[f64]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<f64>` to implement `Index<isize>` error[E0277]: the type `[f64]` cannot be indexed by `isize` --> SampleMeanAndStdDev.rs:31:28 | 31 | variance += pow(samples[i] - mean,2.0f64); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[f64]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<f64>` to implement `Index<isize>` error[E0425]: cannot find function `pow` in this scope --> SampleMeanAndStdDev.rs:31:16 | 31 | variance += pow(samples[i] - mean,2.0f64); | ^^^ not found in this scope error[E0425]: cannot find function `sqrt` in this scope --> SampleMeanAndStdDev.rs:37:54 | 37 | println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); | ^^^^ not found in this scope | help: use the `.` operator to call the method `sqrt` on `f64` | 37 - println!("{}", format!("Sample std. dev.: {0:.6f}", sqrt(variance))); 37 + println!("{}", format!("Sample std. dev.: {0:.6f}", variance.sqrt())); | error: aborting due to 11 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.

Solution