How to resolve the algorithm Box the compass step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Box the compass step by step in the Rust programming language

Table of Contents

Problem Statement

There be many a land lubber that knows naught of the pirate ways and gives direction by degree! They know not how to box the compass!

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Box the compass step by step in the Rust programming language

Source code in the rust programming language

fn expand(cp: &str) -> String {
    let mut out = String::new();
    for c in cp.chars() {
        out.push_str(match c {
            'N' => "north",
            'E' => "east",
            'S' => "south",
            'W' => "west",
            'b' => " by ",
            _ => "-",
        });
    }
    out
}

fn main() {
    let cp = [
        "N", "NbE", "N-NE", "NEbN", "NE", "NEbE", "E-NE", "EbN",
        "E", "EbS", "E-SE", "SEbE", "SE", "SEbS", "S-SE", "SbE",
        "S", "SbW", "S-SW", "SWbS", "SW", "SWbW", "W-SW", "WbS",
        "W", "WbN", "W-NW", "NWbW", "NW", "NWbN", "N-NW", "NbW"
    ];
    println!("Index  Degrees  Compass point");
    println!("-----  -------  -------------");
    for i in 0..=32 {
        let index = i % 32;
        let heading = i as f32 * 11.25
            + match i % 3 {
                1 => 5.62,
                2 => -5.62,
                _ => 0.0,
            };
        println!(
            "{:2}     {:6.2}   {}",
            index + 1,
            heading,
            expand(cp[index])
        );
    }
}


  

You may also check:How to resolve the algorithm Sum and product of an array step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the C# programming language
You may also check:How to resolve the algorithm Maze generation step by step in the Lua programming language
You may also check:How to resolve the algorithm Successive prime differences step by step in the C# programming language
You may also check:How to resolve the algorithm String case step by step in the Common Lisp programming language