How to resolve the algorithm Box the compass step by step in the C# programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Box the compass step by step in the C# 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 C# programming language
The C# code you provided is a program that prints the compass headings for a given set of angles.
The Compass
class contains an array of compass headings, and a method called compassHeading
which takes an angle as input and prints the corresponding compass heading.
The Program
class contains an array of angles, and a main method which creates a Compass
object and then iterates through the array of angles, calling the compassHeading
method for each angle.
The output of the program is a table of angles and corresponding compass headings, as follows:
1: North : 0.00
2: North by east : 16.87
3: North-northeast : 16.88
4: Northeast by north : 33.75
5: Northeast : 50.62
6: Northeast by east : 50.63
7: East-northeast : 67.50
8: East by north : 84.37
9: East : 84.38
10: East by south : 101.25
11: East-southeast : 118.12
12: Southeast by east : 118.13
13: Southeast : 135.00
14: Southeast by south : 151.87
15: South-southeast : 151.88
16: South : 168.75
17: South by west : 185.62
18: South-southwest : 185.63
19: Southwest by south : 202.50
20: Southwest : 219.37
21: Southwest by west : 219.38
22: West-southwest : 236.25
23: West by south : 253.12
24: West-southeast : 253.13
25: West : 270.00
26: West by north : 286.87
27: West-northwest : 286.88
28: Northwest by west : 303.75
29: Northwest : 320.62
30: Northwest by north : 320.63
31: North-northwest : 337.50
32: North by west : 354.37
33: North-northwest : 354.38
Source code in the csharp programming language
using System;
using System.Collections.Generic;
namespace BoxTheCompass
{
class Compass
{
string[] cp = new string[] {"North", "North by east", "North-northeast", "Northeast by north", "Northeast","Northeast by east",
"East-northeast", "East by north", "East", "East by south", "East-southeast", "Southeast by east", "Southeast",
"Southeast by south", "South-southeast", "South by east", "South", "South by west", "South-southwest", "Southwest by south",
"Southwest", "Southwest by west", "West-southwest", "West by south", "West", "West by north", "West-northwest",
"Northwest by west", "Northwest", "Northwest by north", "North-northwest", "North by west", "North"};
public void compassHeading(float a)
{
int h = Convert.ToInt32(Math.Floor(a / 11.25f + .5f)) % 32;
Console.WriteLine( "{0,2}: {1,-22} : {2,6:N}",h + 1, cp[h], a );
}
};
class Program
{
static void Main(string[] args)
{
Compass c = new Compass();
float[] degs = new float[] {0.0f, 16.87f, 16.88f, 33.75f, 50.62f, 50.63f, 67.5f, 84.37f, 84.38f, 101.25f,
118.12f, 118.13f, 135.0f, 151.87f, 151.88f, 168.75f, 185.62f, 185.63f, 202.5f, 219.37f, 219.38f, 236.25f,
253.12f, 253.13f, 270.0f, 286.87f, 286.88f, 303.75f, 320.62f, 320.63f, 337.5f, 354.37f, 354.38f};
foreach (float d in degs)
c.compassHeading(d);
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the 11l programming language
You may also check:How to resolve the algorithm Calendar step by step in the wolfram Language programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the Python programming language
You may also check:How to resolve the algorithm Here document step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Emacs Lisp programming language