Pure Programmer
Blue Matrix


Cluster Map

Project: Histogram

Write a program to compute a [[histogram]] graph. The program should accept frequency data from stdin. The input data should be tab-delimited, two columns. The first column is the label and the second column is the frequency (count / total). All the fequencies should total 100%. The first line will be the column headers, the remaining lines will be data. Print the label in a fields of 18 characters, right justified, the frequency as a percentage (frequency * 100) in a fixed width field of 8 and then a string of equal signs to form the histogram bar. The histogram bars should be between 0 and 50 equal signs where 50 represents the maximum frequency found. You can use the FrequencyTableBytes or FrequencyTableChars project to generate frequency tables then select just the appropriate columns using the ReorderColumns project.

$ ./FrequencyTableChars < some_text_file | ./ReorderColumns 2 4 | ./Histogram

Output
$ rustc Histogram.rs error: unknown format trait `s` --> Histogram.rs:41:37 | 41 | println!("{}", format!("{0:>18.18s} {1:8.4f}% {2:s}", labels[i], freqs[i], repeat('=',barSize))); | ^ | = 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` --> Histogram.rs:41:46 | 41 | println!("{}", format!("{0:>18.18s} {1:8.4f}% {2:s}", labels[i], freqs[i], repeat('=',barSize))); | ^ | = 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 `s` --> Histogram.rs:41:53 | 41 | println!("{}", format!("{0:>18.18s} {1:8.4f}% {2:s}", labels[i], freqs[i], repeat('=',barSize))); | ^ | = 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 --> Histogram.rs:28:19 | 28 | let freq:f64 = Utils.stodWithDefault(right_tail(data,pos + 1), 0.0f64) * 100.f64; | ^^^^^ not found in this scope error[E0425]: cannot find function `repeat` in this scope --> Histogram.rs:41:79 | 41 | println!("{}", format!("{0:>18.18s} {1:8.4f}% {2:s}", labels[i], freqs[i], repeat('=',barSize))); | ^^^^^^ not found in this scope | help: consider importing one of these items | 7 + use std::io::repeat; | 7 + use std::iter::repeat; | error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> Histogram.rs:7:30 | 7 | static MAX_BAR_SIZE:f64 = 50.f64; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 7 | static MAX_BAR_SIZE:f64 = 50.0f64; | + error[E0425]: cannot find function `getline` in this scope --> Histogram.rs:12:8 | 12 | while getline(line) { | ^^^^^^^ not found in this scope error[E0308]: mismatched types --> Histogram.rs:13:15 | 13 | buffer.push(line); | ---- ^^^^- help: try using a conversion method: `.to_string()` | | | | | expected `String`, found `&str` | arguments to this method are incorrect | note: method defined here --> /opt/local/libexec/rust/src/rustc-1.71.1-src/library/alloc/src/vec/mod.rs:1824:12 error[E0425]: cannot find function `find` in this scope --> Histogram.rs:25:19 | 25 | let pos:isize = find(data,"\t"); | ^^^^ not found in this scope error[E0425]: cannot find function `substr` in this scope --> Histogram.rs:27:23 | 27 | let label:String = substr(data,0,pos); | ^^^^^^ not found in this scope error[E0425]: cannot find function `right_tail` in this scope --> Histogram.rs:28:41 | 28 | let freq:f64 = Utils.stodWithDefault(right_tail(data,pos + 1), 0.0f64) * 100.f64; | ^^^^^^^^^^ not found in this scope error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> Histogram.rs:28:81 | 28 | let freq:f64 = Utils.stodWithDefault(right_tail(data,pos + 1), 0.0f64) * 100.f64; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 28 | let freq:f64 = Utils.stodWithDefault(right_tail(data,pos + 1), 0.0f64) * 100.0f64; | + error[E0308]: mismatched types --> Histogram.rs:39:13 | 39 | while i < labels.len() { | - ^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 39 | while i < labels.len().try_into().unwrap() { | ++++++++++++++++++++ error[E0277]: the type `[f64]` cannot be indexed by `isize` --> Histogram.rs:40:37 | 40 | let barSize:isize = (round(freqs[i] / maxFreq * MAX_BAR_SIZE)) as isize; | ^ 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 `round` in this scope --> Histogram.rs:40:25 | 40 | let barSize:isize = (round(freqs[i] / maxFreq * MAX_BAR_SIZE)) as isize; | ^^^^^ not found in this scope | help: use the `.` operator to call the method `StdFloat::round` on `{type error}` | 40 | let barSize:isize = ((freqs[i] / maxFreq * MAX_BAR_SIZE).round() as isize; | ~ ~~~~~~~~~ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Histogram.rs:41:65 | 41 | println!("{}", format!("{0:>18.18s} {1:8.4f}% {2:s}", labels[i], freqs[i], repeat('=',barSize))); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error[E0277]: the type `[f64]` cannot be indexed by `isize` --> Histogram.rs:41:75 | 41 | println!("{}", format!("{0:>18.18s} {1:8.4f}% {2:s}", labels[i], freqs[i], repeat('=',barSize))); | ^ 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 17 previous errors Some errors have detailed explanations: E0277, E0308, E0425, E0610. For more information about an error, try `rustc --explain E0277`.

Solution