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

Published on 7 June 2024 03:52 AM

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

==> Notes for the code and the AI responses...

  • The first response didn't answer my question at all.
  • The second response has some good details and gives us a good answer.

Here I will explain the code myself.

The C++ program determines the compass point abbreviation and its full name for a given angle. It uses Boost libraries for data manipulation and string formatting.

Here's a breakdown of the code:

  1. Header Includes:

    • The program includes necessary header files for input/output, mathematical functions, and Boost libraries:
      • <string> for string manipulation
      • <boost/array.hpp> for creating and manipulating Boost arrays
      • <boost/assign/list_of.hpp> for initializing Boost arrays with a list of values
      • <boost/format.hpp> for string formatting
      • <boost/foreach.hpp> for iterating through Boost arrays
      • <iostream> for input/output operations
      • <math.h> for mathematical functions
  2. Utility Functions:

    • get_Index(float angle): Converts an angle to an index in the range 1 to 32.
    • get_Abbr_From_Index(int i): Returns the compass point abbreviation for a given index.
    • Build_Name_From_Abbreviation(string a): Expands the compass point abbreviation into its full name.
  3. Main Function:

    • Declares a Boost array named headings containing 33 angles corresponding to different compass points.
    • Iterates through each angle in the headings array:
      • Calculates the index corresponding to the angle.
      • Gets the compass point abbreviation for the index.
      • Builds the full name for the compass point.
      • Outputs the index, full name, and original angle using Boost's string formatting capabilities.
  4. Output:

    • The program outputs a table with the following columns:
      • Index (1 to 32)
      • Full compass point name
      • Original angle in floating-point format

Example Output:

  1 north                  0.00
  2 north by east         16.87
  3 north-northeast       33.75
  4 northeast by north     50.62
  5 northeast              67.50
  6 northeast by east      84.37
  7 east-northeast        101.25
  8 east by north         118.12
  9 east                  135.00
 10 east by south         151.87
 11 southeast by east     168.75
 12 southeast             185.62
 13 southeast by south    202.50
 14 south-southeast       219.37
 15 south by east         236.25
 16 south                 253.12
 17 south by west         270.00
 18 southwest by south     286.87
 19 southwest             303.75
 20 southwest by west      320.62
 21 west-southwest        337.50
 22 west by south         354.37
 23 west                  0.00
 24 west by north         16.87
 25 northwest by west      33.75
 26 northwest by north     50.62
 27 northwest              67.50
 28 northwest by east      84.37
 29 north-northwest       101.25
 30 north by west         118.12
 31 north                  135.00
 32 north by east         151.87

Source code in the cpp programming language

#include <string>
#include <boost/array.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/format.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <math.h>
using std::string;
using namespace boost::assign;

int get_Index(float angle)
{
   return static_cast<int>(floor(angle / 11.25 +0.5 )) % 32 + 1;
}

string get_Abbr_From_Index(int i)
{
    static boost::array<std::string, 32> points(list_of
            ("N")("NbE")("NNE")("NEbN")("NE")("NEbE")("ENE")("EbN")
            ("E")("EbS")("ESE")("SEbE")("SE")("SEbS")("SSE")("SbE")
            ("S")("SbW")("SSW")("SWbS")("SW")("SWbW")("WSW")("WbS")
            ("W")("WbN")("WNW")("NWbW")("NW")("NWbN")("NNW")("NbW"));
    return points[i-1];
}

string Build_Name_From_Abbreviation(string a)
{
    string retval;
    for (int i = 0; i < a.size(); ++i){
        if ((1 == i) && (a[i] != 'b') && (a.size() == 3)) retval += "-";
        switch (a[i]){
            case 'N' : retval += "north"; break; 
            case 'S' : retval += "south"; break; 
            case 'E' : retval += "east";  break; 
            case 'W' : retval += "west";  break; 
            case 'b' : retval += " by ";  break;
        }
    }
    retval[0] = toupper(retval[0]);
    return retval;
}

int main()
{
    boost::array<float,33> headings(list_of
            (0.0)(16.87)(16.88)(33.75)(50.62)(50.63)(67.5)(84.37)(84.38)(101.25)
            (118.12)(118.13)(135.0)(151.87)(151.88)(168.75)(185.62)(185.63)(202.5)
            (219.37)(219.38)(236.25)(253.12)(253.13)(270.0)(286.87)(286.88)(303.75)
            (320.62)(320.63)(337.5)(354.37)(354.38));
    int i;
    boost::format f("%1$4d %2$-20s %3$_7.2f");

    BOOST_FOREACH(float a, headings)
    {
        i = get_Index(a);
        std::cout << f % i %  Build_Name_From_Abbreviation(get_Abbr_From_Index(i)) % a << std::endl;
    }
    return 0;
}


  

You may also check:How to resolve the algorithm Faulhaber's triangle step by step in the Haskell programming language
You may also check:How to resolve the algorithm Convex hull step by step in the Sidef programming language
You may also check:How to resolve the algorithm Peano curve step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Quackery programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Pascal programming language