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

Objective: Familiarize with the use of functions to obtain a more readable and r

ID: 3636921 • Letter: O

Question

Objective: Familiarize with the use of functions to obtain a more readable and reusable
code, design an interactive menu.

Instructions:
This homework is an extension of hw8. A map of a city planning is given to you, the same as
in hw9. The map of the city is stored in the file “/ece/www/ism2008/hw9/HW9.dat”. The
format of the data file is the same as in hw8. The map size is 24x24. Your program should:

1. Read the data file. Save all the map information to a 2-dimensional char or integer
array. Get all building information in the map similar to Hw8.

For all buildings derive the spatial information to an array of size 20x5. The
information includes the coordinates of the top left corner of the building, the width
(number of columns) and the length (number of rows), and the area. Here, the row
size is 20, as we assume that the maximum number of buildings is 20.
(A) If you are able to get the function of reading building information in Hw8 to
work: Write a function similar to the one you have in hw8 to get the building
info.
(B) If you are not able to get the function of reading building information to work:
Read the building info from the file “/ece/www/ism2008/hw9/bldginfo.dat”,
and save the information to the building information array. In this file, the first
line is the number of buildings, and each line following the first line stores the
spatial information about a building (the row number of the upper left corner,
the column number of the top left corner, the width, the length, and the area).
Following is an example:
6
0 6 6 3 18
3 15 3 3 9
6 11 2 4 8
13 15 4 3 12
16 0 2 3 6
20 22 2 2 4

2. Print a message that says the map has been successfully read and print the map,
followed by a menu of options for the user to select:

Menu:

0: exit program
1: add a building
2: delete a building
3: print the map
4: print all building info
5: sort the buildings in increasing order based on the area
Please enter a choice:

3. When the user selects an option, a function is called to realize the option. When the
function is called, the user may be asked for further input to complete the task.

4. When the task is done, display a message that indicates if the task is successfully
accomplished or not. The menu should be displayed again asking the user for the
next task.

Menu Options:

0:

(Exit) The program exits.

1:

(Add a building) The function for adding a building asks the user to input the
location of the new building (i.e., the row and column numbers of the upper left
corner, the width and length). The function should determine if there is any overlap
with existing buildings. If a conflict is detected, the building cannot be added;
otherwise the building is added to the map, and added to the building information
array (the 20x5 array). In either case a message should be displayed indicating if the
operation is successful. You should also increment the building count by 1.

2:

(Delete a building) The user will be asked to enter the building number to delete.
The building will be removed from the map (the 24x24 array), and the building
information is also removed from the building information array (the 20x5 array),
the building count should be reduced by 1.

3:
4:
5:

Print out the map.
Print out and building information.
(extra credit) Use bubble sort (discussed in the textbook) to sort the 20x5 array in
increasing order based on the area. Note that you have to copy the 20x5 array to
another 20x5 array for this option so that the original array is not changed as
otherwise it cannot be used for further operations.

Hints:
1. The add building function and move building function should be able to detect an

overlap with another existing building. Think about how to develop an algorithm to
detect if two rectangle overlap or not.
2. The delete building/add building function should update the 24x24 array and the
20x5 array.

Explanation / Answer

#include #include #define N 20 #define MAPW 24 #define MAPL 24 void printMap(char map[MAPL][MAPW]); int getbldg(int bld[N][5], char map[MAPL][MAPW]); int addbldg(int bld[N][5], char map[MAPL][MAPW], int counter); void printBld(int bld[N][5], int counter); void sortbldg(int bld[N][5], int counter); void swap(int p1[5], int p2[5]); int deletebldg(int bld[N][5], char map[MAPL][MAPW], int counter); int main(void) { int i, j; char map_main[MAPL][MAPW]; //2-D char array to save the map info int bld[N][5]; //2-D array to save all building info, assume totoal building less than 20 int counter = 0; // counter save the number of building char temp; int menu; FILE *fp; if((fp=fopen("/ece/www/ism2008/hw9/HW9.dat","r"))==NULL) { printf("fail"); exit(1); } for(i=0; i