Reference no: EM13163983
start with code in the file lab.cpp. This program works with a three dimensional array representing parking spaces in a parking garage on several floors. The code is incomplete. The functions "main", "display" and "showSpace" are complete. Your job is to finish function "choose". The idea behind this program is to display a parking garage arrangement of available and occupied car spaces and then allow the user to type in a space that can be made occupied (if available). If the user types in a space that is already occupied, the program is to simply type a message on the screen to say this is not possible since the space is already occupied. If available, the program is to change the space to occupied and then display the parking arrangement again with the change.
The function "choose" is bool return because if a change is actually made, this returns true so the main function can display the parking arrangement again (call function "display"). If a change is not made because the user selected a space that is already occupied, no change is made and only an error message is displayed. The error message is displayed by the "choose" function. In this case, the "choose" function returns false.
The matrix is composed of bool values (true if occupied and false if available). The showSpace function is responsible for displaying the '-' or 'O' depending on the matrix element value.
Remember that in the display, the rows and floor numbers start with 1, but they correspond to index values which start with 0. In the display, the columns are not numbered.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS = 6;
const int COLUMNS = 12;
const int FLOORS = 3;
void display(bool ar[][COLUMNS][FLOORS]);
void showSpace(bool sp);
bool choose(bool ar[][COLUMNS][FLOORS]);
int main()
{
srand((unsigned)time(0));
bool car_sp[ROWS][COLUMNS][FLOORS];
int i, j, k;
// randomly assign car spaces to be occupied
// or not
for (i=0; i<ROWS; i++)
for (j=0; j<COLUMNS; j++)
for (k=0; k<FLOORS; k++)
{
if (rand()%10 > 2)
car_sp[i][j][k] = false;
else
car_sp[i][j][k] = true;
}
cout << "the existing car spaces are shown below\n"
<< "(0) if occupied, and (-) if available\n";
display(car_sp);
if (choose(car_sp))
{
cout << "\nafter the car space you chose is occupied,\n"
<< "the new result is below\n\n";
display(car_sp);
}
return 0;
}
void showSpace(bool sp)
{
if (sp)
cout << setw(3) << 'O';
else
cout << setw(3) << '-';
}
bool choose(bool ar[][COLUMNS][FLOORS])
{
bool changed = false;
// ask the user to select a space (choose floor,
// row and column).
// check whether the space is occupied.
// if occupied, report the error and return false
// if available, change to occupied and return true
return changed;
}
void display(bool ar[][COLUMNS][FLOORS])
{
for (int fl=0; fl<FLOORS; fl++)
{
cout << "floor number " << fl+1
<< "\nRow\n";
for (int rw=0; rw<ROWS; rw++)
{
cout << rw+1 << " ";
for (int cl=0; cl<COLUMNS; cl++)
showSpace(ar[rw][cl][fl]);
cout << endl;
}
}
}