The name of your C program file must be called build.c, files with any other name will not be marked.
'.' = empty land '=' = horizontal road '#' = vertical road '@' = house
To locate a square (a plot of land) in the city, we can make use of the row and column coordinates, hence city[row][column] should uniquely identify a plot of land. The coordinate of the top left corner of the city is [0][0], and the top right corner of the city is at [0][19].
The city starts off as totally empty, i.e. all squares are '.'. Then the user will issue the following building commands:
Your program will then attempt to carry out the commands and change the city accordingly. The summary of the additional input from user as well as conditions for each of the commands are summarized below:
Command | Additional User Input | Conditions | Effects on City |
Build Road |
|
|
|
Build House |
|
|
|
Other than the above commands, you are required to output the final city into a file named “city.txt” when the user choose to exit.
So, the overall flow of your program is as follows:
$ ./build .................... // the starting city, all squares empty. .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 1 Row ==> 0 Column ==> 0 Length ==> 25 1.Horizontal 2.Vertical ==> 1 // try to build a horizontal road at [0][0] of length 25. RESULT: 20 squares of road built // only 20 squares can be built. ==================== // new road. .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 1 Row ==> 0 Column ==> 10 Length ==> 10 1.Horizontal 2.Vertical ==> 2 // try to build a vertical road at [0][10] of length 10. RESULT: 10 squares of road built // all 10 squares can be built. ==========#========= // earlier road "overwritten". ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... // the end of vertical road. .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 2 Row ==> 10 Column ==> 10 // try to build a house at [10][10]. RESULT : House built successfully ==========#========= ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........@......... // can be built: there is a road on top. .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 1 Row ==> 10 Column ==> 5 Length ==> 15 1.Horizontal 2.Vertical ==> 1 // try to build a horizontal road at [10][5] of length 15. RESULT: 5 squares of road built // only 5 square of road can be built. ==========#========= ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... .....=====@......... // stopped by a house. .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 2 Row ==> 3 Column ==> 3 // try to build house at [3][3]. RESULT: House cannot be built! // failed: no adjacent road. ==========#========= ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... .....=====@......... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 2 Row ==> 10 Column ==> 10 // try to build house at [10][10]. RESULT: House cannot be built! // failed: already has a house at target square. ==========#========= ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... .....=====@......... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 2 Row ==> 0 Column ==> 1 // try to build a house at [0][1]. RESULT: House cannot be built! // failed: [0][1] is a road. ==========#========= ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... .....=====@......... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 2 Row ==> 1 Column ==> 0 // try to build house at [1][0]. RESULT : House built successfully // successful. ==========#========= @.........#......... // House ok : road above. ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... .....=====@......... .................... .................... .................... .................... .................... .................... .................... .................... .................... 1.Road 2.House 3.Exit ==> 3 // exit the program. City written into output file. // write into the file "city.txt". $ less -e city.txt // the content of output file. ==========#========= @.........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... ..........#......... .....=====@......... .................... .................... .................... .................... .................... .................... .................... .................... .................... $ // you are done!! phew!!
A total of 8 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 8 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.