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

C++ HELP!!! (CHECK REQUIREMENTS) You will make a 2D array of 13 columns and 2 ro

ID: 3849543 • Letter: C

Question

C++ HELP!!! (CHECK REQUIREMENTS)

You will make a 2D array of 13 columns and 2 rows of type char. char theArray[13][2]; will work to create the array you need to store the data.

The program will process a set of commands. The commands will be described below.

load - The first command will be a load command. The load command will be followed by a file to open. That file will contain a set of characters in a 13 x 2 grid that you are to read and store in the array. The load command will always be first and it will always be followed by a filename to open. For example

This would mean you create another ifstream object using the file name data.txt, so store that in a variable and open that.

location - The second command is the location command. The location command will be followed by 2 integers. The integers represent an x and y location in the array. The location given by the x and y may or may not be valid. You need to make sure that they are at least 0 and less then 13 for x and 2 for y. For example:

The first two examples are valid and the last two are invalid.

Depending on what is stored in the first two locations you would get output like this:

For the last two you would get this output:

So when the location is good, you output the coordinates and then the data that is there and when the location is bad, you output the coordinates then the words "bad location". Note the comma between the x and y and the colon after the y coordinate.

Sample Input

If the input file contains this:

And the test1data.txt contains this data:

This is the output:

Requirements

You must use the file lab8.h for the header.

In the header you must declare the function void read2D_Data( string input, string output );

In your cpp file you must implement the function and use a 2D array of characters of size 13x2.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
   char theArray[13][2];
   string command,file = "";
   int x, y,i = 0;
   ifstream in;
   in.open("input.txt");
   //check if input file is open
   if (!in)
   {
       cout << "Not able to open input file" << endl;
   }
   while (!in.eof())
   {
       in >> command;
       if (command.compare("load") == 0)
       {
           in >> file;
       }
       if (command.compare("location") == 0)
       {
           in >> x >> y;
           //open the file given after load command
           ifstream In;
           In.open(file);
           if (!In)
           {
               cout << "Not able to open the input file " << file << endl;
           }
           int j = 0, k = 0;
           char c;
           while (In.get(c))
           {
               if (c != 10 && c!=32)
                   theArray[k][j++] = c;
               if (j > 1)
               {
                   j = 0;
                   k++;
               }
           }
           if (x < 0 || y < 0 || x > 12 || y > 13)
           {
               cout << x << ", " << y << ": " << "bad location" << endl;
           }
           else
           {
               cout << x << "," << y << ": " << theArray[x][y] << endl;
           }
       }
      
   }
}

---------------------------------------------------------------

//output

12,1:
0,0: 2
12,1:
-1, -1: bad location
13, 2: bad location