Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please don\'t post code if its not relevant to the program. The purpose of this

ID: 3638538 • Letter: P

Question

Please don't post code if its not relevant to the program.

The purpose of this program is to take driving directions from point A to point B and produce the reverse directions, i.e. the path from point B to point A. The best way to do this is to use arrays, and define static methods.

1:Start at 7 Frog Pond Road facing south. Drive 89.9 miles.
2:Turn left on Lexington Street. Drive 82.4 miles.
3:Turn right on Arlington Street. Drive 76.1 miles.
4:Turn right on Morning Light Drive. Drive 61.1 miles.
5:Stop at 65 Morning Light Drive facing east

Things that might help
1. first line has the following form
1: Start at <address> facing <north, south, east or west>. Drive <distance> miles.
2. last line has the following form
<line>: Stop at <address> facing <north, south, east or west>.
3. inner lines (i.e. those that are not the first and not the last) have form
<line>: Turn <left or right> on <street>. Drive <distance> miles.
4. the last line and the line before the last always mention the same street. The rest of the streets are
mentioned only once.

Below is the reversal of the directions above:

1:Start at 65 Morning Light Drive facing west. Drive 61.1 miles.
2:Turn LEFT on Arlington Street. Drive 76.1 miles.
3:Turn LEFT on Lexington Street. Drive 82.4 miles.
4:Turn RIGHT on Frog Pond Road. Drive 89.9 miles.
5:Stop at 7 Frog Pond Road facing north.

Things to keep in mind:

• The last line of the reversed directions is based on the first line of the original directions.
• The first line of the reversed directions is based on the information from the last line and the one before the last line of the original directions.
• The line right before the last in the reversed directions is based on the first and second lines of the original.
• Every inner line is based on the information on two adjacent lines of the original, e.g line 2 of the revested directions is based on lines 3 and 4 of the original. If the original direction included a left turn from a street, the reversed directions will include a right turn on that street. The turn on Arlington street is LEFT, because in the original, the turn that was made from the Arlington street on Morning Light Drive was a right turn (line 4 of the original).

The way that it should be done is to define an array in your main method that would store the directions presented in the example above as follows:

String [] directions = new String [] {
"1: Start at 7 Frog Pond Road facing south. Drive 89.9 miles.",
"2: Turn left on Lexington Street. Drive 82.4 miles.",
"3: Turn right on Arlington Street. Drive 76.1 miles.",
"4: Turn right on Morning Light Drive. Drive 61.1 miles.",
"5: Stop at 65 Morning Light Drive facing east."
};

If you can complete this program for me, I would be highly grateful. I have enough on my schedule as a TA.

Thank You

G

Explanation / Answer

I'm NOT using string to store the directions.

I DO NOT include the starting number of each line in my directions cstring. If you want that, go tweak the code yourself.

#include <iostream>
using namespace std;

//SYMBOLIC CONSTANTS
const int LINE_MAX_LEN = 101;

int main(int argc, char * argv[])
{
   //PROTOTYPES
   void spellCheck(const char *sentence, char *correctSentence,
                const char *mispelledWord, const char * word);

   //LOCAL DECLARATIONS
   const char DIRECTIONS[][LINE_MAX_LEN] = {
      "Start at 7 Frog Pond Road facing south. Drive 89.9 miles.",
      "Turn left on Lexington Street. Drive 82.4 miles.",
      "Turn right on Arlington Street. Drive 76.1 miles.",
      "Turn right on Morning Light Drive. Drive 61.1 miles.",
      "Stop at 65 Morning Light Drive facing east."
   };
   int directionLen = sizeof(DIRECTIONS) / sizeof(*DIRECTIONS);
   const char DIRECTION[4][10] = { "north", "south", "east", "west" };
   char tempLine[100];
   char **reverse = new char*[directionLen];
   for (int i = 0; i < directionLen; i++)
   {
      reverse[i] = new char[LINE_MAX_LEN];
   }
   int tempIndex = 0;
   const int START_AT_INDEX = 9;
   const int TURN_LEFT_INDEX = 13;

   //PROCEDURES
   /// First line of reverse direction
   ///
   //..Use the last line of DIRECTIONS
   //..Replace "Stop" with "Start".
   //..Replace "north" or "east" with "south" or "west" and vice versa.
   //..Copy the " Drive xxxxx miles." part to the end of the reverse line.
   ///
   spellCheck(DIRECTIONS[directionLen - 1], tempLine, "Stop", "Start");
   for (int i = 0; i < 4; i++)
   {
      if (strstr(DIRECTIONS[directionLen - 1], DIRECTION[i]))
      {
         spellCheck(tempLine, reverse[0],
                    DIRECTION[i], DIRECTION[i - 2 * (i % 2) + 1]);
         break;
      }
   }
   const char *drivePtr = strstr(DIRECTIONS[directionLen - 2], ". Drive ");
   for (tempIndex = strlen(reverse[0]) - 1; *drivePtr; tempIndex++)
   {
      reverse[0][tempIndex] = *drivePtr++;
   }
   reverse[0][tempIndex] = '';
   cout << reverse[0] << endl; //check point

   /// Middle lines, except the line next the to last line
   ///
   //..Use the middle lines of DIRECTIONS (above the last line & below the 2nd line).
   //..Replace "left" with "right" or vice versa.
   //..Overwrite the part after "Turn left on " or "right on " with the above line's.
   ///
   for (int i = 1; i < directionLen - 2; i++)
   {
      int startStreetIndex = TURN_LEFT_INDEX;
      if (strstr(DIRECTIONS[directionLen - 1 - i], "Turn left on "))
      {
         spellCheck(DIRECTIONS[directionLen - 1 - i], reverse[i],
                    "Turn left on ", "Turn right on ");
         startStreetIndex++;
      }
      else
      {
         spellCheck(DIRECTIONS[directionLen - 1 - i], reverse[i],
                    "Turn right on ", "Turn left on ");
      }
      if (strstr(DIRECTIONS[directionLen - 1 - i - 1], "Turn left on "))
      {
         for ( ; DIRECTIONS[directionLen - 1 - i - 1][startStreetIndex]; startStreetIndex++)
         {
            reverse[i][startStreetIndex] = DIRECTIONS[directionLen - 1 - i - 1][startStreetIndex];
         }
      }
      else
      {
         for ( ; DIRECTIONS[directionLen - 1 - i - 1][startStreetIndex + 1]; startStreetIndex++)
         {
            reverse[i][startStreetIndex] = DIRECTIONS[directionLen - 1 - i - 1][startStreetIndex + 1];
         }
      }
      reverse[i][startStreetIndex] = '';
      cout << reverse[i] << endl; //check point
   }

   /// The line next the to last line
   ///
   //..Use the second line of DIRECTIONS.
   //..Replace "left" with "right" or vice versa.
   //..Overwrite the part after "Turn left on " or "right on " with the first line's.
   //..You need to seperate this line from the middle lines because
   //the first line part after "Turn l/r on " has the home number and
   //facing direction. You need to get rid of both of them.
   ///
   int startStreetIndex = TURN_LEFT_INDEX;
   if (strstr(DIRECTIONS[1], "Turn left on "))
   {
      spellCheck(DIRECTIONS[1], reverse[directionLen - 2],
                 "Turn left on ", "Turn right on ");
      startStreetIndex++;
   }
   else
   {
      spellCheck(DIRECTIONS[1], reverse[directionLen - 2],
                 "Turn right on ", "Turn left on ");
   }
   const char *streetPtr = DIRECTIONS[0] + START_AT_INDEX;
   const char *facePtr = strstr(DIRECTIONS[0], " facing ");
   while (*streetPtr != ' ')
   {
      streetPtr++;
   }
   streetPtr++;
   for ( ; streetPtr < facePtr; startStreetIndex++)
   {
      reverse[directionLen - 2][startStreetIndex] = *streetPtr++;
   }
   streetPtr = strstr(DIRECTIONS[0], ". Drive ");
   for ( ; *streetPtr; startStreetIndex++)
   {
      reverse[directionLen - 2][startStreetIndex] = *streetPtr++;
   }

   reverse[directionLen - 2][startStreetIndex] = '';
   cout << reverse[directionLen - 2] << endl; //check point

   /// Last line of reverse direction
   ///
   //..Use the first line of DIRECTIONS.
   //..Replace "Start" with "Stop".
   //..Replace "north" or "east" with "south" or "west" and vice versa.
   //..Delete the " Drive xxxxx miles." part.
   ///
   spellCheck(DIRECTIONS[0], tempLine, "Start", "Stop");
   for (int i = 0; i < 4; i++)
   {
      if (strstr(DIRECTIONS[0], DIRECTION[i]))
      {
         spellCheck(tempLine, reverse[directionLen - 1],
                    DIRECTION[i], DIRECTION[i - 2 * (i % 2) + 1]);
         break;
      }
   }
   char *cutPtr = strstr(reverse[directionLen - 1], ". Drive") + 1;
   *cutPtr = '';
   cout << reverse[directionLen - 1] << endl; //check point

   /// DONE.
   //..Now display the result.
   cout << endl << endl << endl;
   cout << "From A to B:" << endl;
   for (int i = 0; i < directionLen; i++)
   {
      cout << i + 1 << ": " << DIRECTIONS[i] << endl;
   }
   cout << endl << "From B to A:" << endl;
   for (int i = 0; i < directionLen; i++)
   {
      cout << i + 1 << ": " << reverse[i] << endl;
   }

   //cleaning up **reverse
   for (int i = 0; i < directionLen; i++)
   {
      delete [] reverse[i];
   }
   delete [] reverse;
   cout << endl << endl << endl;
   return 0;
}

//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
// This function works like "Find and Replace" function in any text editor.
// Output to correctSentence.
//---------------------------------------------------------
void spellCheck(const char *sentence, char *correctSentence,
                const char *mispelledWord, const char * word)
{
   int i = 0;
   int sentenceLen = strlen(sentence);
   int mispelledWordLen = strlen(mispelledWord);
   int wordLen = strlen(word);
   for ( ; i < sentenceLen - mispelledWordLen + 1; i++)
   {
      bool found = true;
      for (int j = 0; j < mispelledWordLen; j++)
      {
         if (mispelledWord[j] != sentence[i + j])
         {
            found = false;
            break;
         }
      }
      if (found)
      {
         for (int j = 0; j < wordLen; j++)
         {
            *correctSentence++ = word[j];
         }
         i += mispelledWordLen - 1;
      }
      else
      {
         *correctSentence++ = sentence[i];
      }
   }
   if (sentence[i])
   {
      for ( ; sentence[i]; i++)
      {
         *correctSentence++ = sentence[i];
      }
   }
   *correctSentence = 0;
}
//---------------------------------------------------------

Output:

Start at 65 Morning Light Drive facing west. Drive 61.1 miles.
Turn left on Arlington Street. Drive 76.1 miles.
Turn left on Lexington Street. Drive 82.4 miles.
Turn right on Frog Pond Road. Drive 89.9 miles.
Stop at 7 Frog Pond Road facing north.



From A to B:
1: Start at 7 Frog Pond Road facing south. Drive 89.9 miles.
2: Turn left on Lexington Street. Drive 82.4 miles.
3: Turn right on Arlington Street. Drive 76.1 miles.
4: Turn right on Morning Light Drive. Drive 61.1 miles.
5: Stop at 65 Morning Light Drive facing east.

From B to A:
1: Start at 65 Morning Light Drive facing west. Drive 61.1 miles.
2: Turn left on Arlington Street. Drive 76.1 miles.
3: Turn left on Lexington Street. Drive 82.4 miles.
4: Turn right on Frog Pond Road. Drive 89.9 miles.
5: Stop at 7 Frog Pond Road facing north.