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

Create a program that can read a map. The map is located at the end of this ques

ID: 3633904 • Letter: C

Question

Create a program that can read a map. The map is located at the end of this question. a "-" represents an empty space, and a "*" represents a part of the building. each of these are 100 square feet. There are seven buildings, and the map as an area of 240 X 220 square feet. scan the file ( the file name is input.dat) print the map, and tell the location of each building (located the top left corner) as well as the width, length, and area. (use function outside main to reprint map and calculate the data)

------******------------
------******------------
------******------------
---------------***------
---------------***------
---------------***------
-----------**-----------
-----------**-----------
-----------**-----------
-----------**-----------
-----****---------------
-----****---------------
-----****---------------
---------------****-----
---------------****-----
---------------****-----
**----------------------
**----------------------
------------------------
------------------------
----------------------**
----------------------**

Explanation / Answer

Hi, here's the code, works like a charm :) the tricky part with reading text from a file is to add a little wiggle room at the end of the array for that return char, otherwise it messes everything up, that's why I initialize the array with 22+1 and 24+1 :) I included as much comments as I felt would be helpful without being overkill. But don't just expect to copy-paste blindly and have it work!!! Give it a good slow read, and run it a couple of times to understand what goes on in it.

I hope this helps, please rememeber to rate!

#include <stdio.h>

/* Function prototypes */
void print_map(char map[][24+1], int r, int c);
void calculate_data(char map[][24+1], int r, int c);

int main ()
{
/* Map variables */
int rows = 22;
int cols = 24;
char map[rows+1][cols+1];

/* File variables */
FILE *inputFile;

/* Helper variables */
int i, j;
char line[cols+2];

/* Scanning File */
printf(" Loading Map from file... ");
inputFile = fopen("input.dat", "r");

i=0;
j=0;
while( fgets( line, sizeof line, inputFile ) != NULL )
{
  if(i < rows)
  {
   for(j = 0; j < cols; j++)
   {
    map[i][j] = line[j];
   }
   i++;
  }
}
fclose ( inputFile );

/* Printing the Map */
print_map(map, rows, cols);

/* Display Buildings Information */
calculate_data(map, rows, cols);

return 0;
}

void print_map(char map[][24+1], int r, int c)
{
int rows = r;
int cols = c;
int i, j;
for(i = 0; i < rows; i++)
{
  for(j = 0; j < cols; j++)
  {
   printf("%c", map[i][j]);
  }
  printf(" ");
}
}

void calculate_data(char map[][24+1], int r, int c)
{
int rows = r;
int cols = c;
int locR, locC, width, length, area;
int i, j, k, l;

for(i = 0; i < rows; i++)
{
  for(j = 0; j < cols; j++)
  {
   if(map[i][j] == '*')
   {
    /*check if top left corner*/
    /*we do that by checking preceding row and column cells*/
    /*if both are free or this is the very first, it's a location */
    if( ( (i==0) || ( (i> 0) && (map[i-1][j] == '-') ) ) &&
     ( (j==0) || ( (j> 0) && (map[i][j-1] == '-') ) ) )
    {
     locR = i;
     locC = j;
     width = 0;
     length = 0;
     area = 0;
     /* the width is the number of * on same row */
     k = locC;
     while(map[locR][k] == '*')
     {
      width++;
      k++;
     }
     /* the length is the number of * on same column */
     l = locR;
     while(map[l][locC] == '*')
     {
      length++;
      l++;
     }
     /* the area is width x length */
     area = width * length;
     printf(" For building at %d:%d", locR, locC);
     printf(" Width: %d", width);
     printf(" Length: %d", length);
     printf(" Area: %d ", area);
    }
   }
  }
}
} // end of the calculate function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote