Pure Programmer
Blue Matrix


Cluster Map

Project: Median Filter

Write a filter program that accepts text representations of floating point values on the standard input, one per line. The program should then compute the [[median]] value. To find the median you must sort the data and pick the middle value. If the number of values is even, you must average the middle two values to compute the median. Finally the program should output the value of the median to 16 decimal places on a single line so that it can easily be used by other programs.

Output
$ rustc MedianFilter.rs error: unknown format trait `g` --> MedianFilter.rs:45:30 | 45 | println!("{}", format!("{0:g}", median)); | ^ | = 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 `Utils` in this scope --> MedianFilter.rs:18:12 | 18 | line = &(Utils.trim(line)); | ^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> MedianFilter.rs:17:8 | 17 | while getline(line) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> MedianFilter.rs:20:8 | 20 | d = stod(line)?; | ^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> MedianFilter.rs:28:9 | 28 | if strlen(line) > 0 { | ^^^^^^ not found in this scope error[E0425]: cannot find function `sortNumeric` in this scope --> MedianFilter.rs:36:11 | 36 | values = sortNumeric(values); | ^^^^^^^^^^^ not found in this scope error[E0277]: the type `[f64]` cannot be indexed by `isize` --> MedianFilter.rs:40:20 | 40 | median = values[(count - 1) / 2]; | ^^^^^^^^^^^^^^^ 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` --> MedianFilter.rs:43:21 | 43 | median = (values[middle] + values[middle - 1]) / 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[E0277]: the type `[f64]` cannot be indexed by `isize` --> MedianFilter.rs:43:38 | 43 | median = (values[middle] + values[middle - 1]) / 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: aborting due to 9 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc MedianFilter.rs error: unknown format trait `g` --> MedianFilter.rs:45:30 | 45 | println!("{}", format!("{0:g}", median)); | ^ | = 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 `Utils` in this scope --> MedianFilter.rs:18:12 | 18 | line = &(Utils.trim(line)); | ^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> MedianFilter.rs:17:8 | 17 | while getline(line) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> MedianFilter.rs:20:8 | 20 | d = stod(line)?; | ^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> MedianFilter.rs:28:9 | 28 | if strlen(line) > 0 { | ^^^^^^ not found in this scope error[E0425]: cannot find function `sortNumeric` in this scope --> MedianFilter.rs:36:11 | 36 | values = sortNumeric(values); | ^^^^^^^^^^^ not found in this scope error[E0277]: the type `[f64]` cannot be indexed by `isize` --> MedianFilter.rs:40:20 | 40 | median = values[(count - 1) / 2]; | ^^^^^^^^^^^^^^^ 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` --> MedianFilter.rs:43:21 | 43 | median = (values[middle] + values[middle - 1]) / 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[E0277]: the type `[f64]` cannot be indexed by `isize` --> MedianFilter.rs:43:38 | 43 | median = (values[middle] + values[middle - 1]) / 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: aborting due to 9 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc MedianFilter.rs error: unknown format trait `g` --> MedianFilter.rs:45:30 | 45 | println!("{}", format!("{0:g}", median)); | ^ | = 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 `Utils` in this scope --> MedianFilter.rs:18:12 | 18 | line = &(Utils.trim(line)); | ^^^^^ not found in this scope error[E0425]: cannot find function `getline` in this scope --> MedianFilter.rs:17:8 | 17 | while getline(line) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `stod` in this scope --> MedianFilter.rs:20:8 | 20 | d = stod(line)?; | ^^^^ not found in this scope error[E0425]: cannot find function `strlen` in this scope --> MedianFilter.rs:28:9 | 28 | if strlen(line) > 0 { | ^^^^^^ not found in this scope error[E0425]: cannot find function `sortNumeric` in this scope --> MedianFilter.rs:36:11 | 36 | values = sortNumeric(values); | ^^^^^^^^^^^ not found in this scope error[E0277]: the type `[f64]` cannot be indexed by `isize` --> MedianFilter.rs:40:20 | 40 | median = values[(count - 1) / 2]; | ^^^^^^^^^^^^^^^ 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` --> MedianFilter.rs:43:21 | 43 | median = (values[middle] + values[middle - 1]) / 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[E0277]: the type `[f64]` cannot be indexed by `isize` --> MedianFilter.rs:43:38 | 43 | median = (values[middle] + values[middle - 1]) / 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: aborting due to 9 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.

Solution