Program is to be in C++ T he CS1 hospital receives a variety of new patients eac
ID: 3771129 • Letter: P
Question
Program is to be in C++
The CS1 hospital receives a variety of new patients each day. At various times of the day patients are held in a temporary room until there is a determination made where the patient should be placed for their visit. The final bed assignments are added at the end of the day. There also are changes and updates that have to be made. These rearrangements are reflected and placed on a grid that contains all of the current days new room assignments.
You have been hired to program the daily floor updates for the hospital using the input provided from the head nurse and a file that contains the pre-existing grid of the floors. There are 5 floors with 8 rooms on each floor. The pre-existing grid is actually a two-dimensional array that contains the following numeric codes:
Vacant
Check out
Occupied
Nurse Station
Transfer
Utility room
These indicate the actions that must take place as updates to the grid. These updates have to be made to the floors (grid) first, and then the new patients are inserted into the vacant rooms.
Here are the basic requirements:
When rooms are checked out they may be filled with a transfer or with a new patient so check outs should be updated to vacancies.
When a room is vacant it can be filled with a transfer or with a new patient.
Transfers are to be handled first, before new patients. Transfers are to be moved to the first available room, and their existing room is eligible for new patients or for another transfer. You may have to transfer patients to a different floor.
After all transfers are completed and check outs have been completed, new patients may begin to move in.
Both the occupied, utility room, and the Nurse Station codes will not require any activity but they are also not available.
When accepting the user input, you are required to validate the input and also you will need to display the total number of rooms that are currently available. You must limit the user’s input to not be greater than that value.
The program must display the grid of rooms three times: 1) upon reading the file; 2) after the transfers and check outs but before the user input and; 3) final version of the completed grid. All codes will display as before except those that checked out (1), those will become occupied (2) or vacancies (0).
Strategy:
Open the file for reading.
Build and display the initial grid of rooms from the input file.
Remember that room 1, floor 1, is (0,0) on the grid.
Perform the checkout updates.
Perform the transfer updates.
Rebuild and redisplay the grid of rooms.
Ask the user for their daily new patient input (display the threshold and limit the number)
Update the new patients.
Rebuild and redisplay the grid of rooms.
Printing the grid of rooms
You read in the floor grid as a 2-dimensional array of integers for the sole purpose of file reading sanity. However, this is not how it will appear when you display it on the screen.
Transfers are represented by a ‘T’.
The Check outs are represented by a ‘C’.
Nurse work stations are represented by a ‘W’.
Occupied rooms are represented by an ‘O’.
A vacant room is represented by a ‘V’.
The utility rooms are represented by an ‘X’.
There are different ways to handle this so as long as it makes sense, I won’t interfere. If you want advice, see me during office hours or ask in class.
Code:
Use a two-dimensional array to represent the room grid.
Make sure that your input from the nurse’s station is valid (it is in the range of vacancies and it must be numeric).
At a minimum you are required to create the following functions to:
Read in the initial grid.
Display the current grid of rooms with current data on who is in what position.
Get the input from the nurse’s station.
Move the check outs.
Move the transfers (important: see instructions below*).
Insert the new patients.
Calculate the number of vacancies.
Make sure you use call by value and call by reference properly.
Make sure you close the input file when you are finished reading it.
Use a header comment with your name and the description of the program.
Use pre and post condition comments for each function.
Use appropriate code comments.
Use appropriate variable names.
Use constants where appropriate.
Use good coding practices (i.e., spacing, indentation, etc.)
*For moving transfers, your function is required to create a char array of pointers. This array will hold the addresses of the vacant positions. You will allocate an array the size of the number of vacancies and load the dynamic array with the transfers. Note that you may use a dynamic array (bonus points) or a regular array of pointers. Then move all of the transfers to available rooms (mark them as OCCUPIED) using the pointer.
Input data:
(R1) 1 0 4 1 3 2 0 2
(R2) 2 0 4 3 0 0 2 0
(R3) 2 0 2 0 3 4 0 2
(R4) 2 4 1 3 0 0 2 2
(R5) 1 2 0 4 3 1 2 5
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
void readFile(int (&rooms)[5][8],string fileAddress);
void displayRooms(int rooms[5][8]);
int countVacantRooms(int rooms[5][8]);
void addNewPatients(int(&rooms)[5][8],int count);
void performCheckouts(int (&rooms)[5][8]);
void performTransfers(int (&rooms)[5][8]);
void populateVacantRoomArray(int rooms[5][10], int* vacantRooms[]);
int** getVacantArray(int rooms[5][8]);
int main()
{
int rooms[5][8];
string fileAddress = "C:/rooms.txt";
readFile(rooms, fileAddress);
displayRooms(rooms); //Displaying the initial room grid
performCheckouts(rooms);
int VacantAfterCheckout = countVacantRooms(rooms);
performTransfers(rooms);
displayRooms(rooms); //For displaying room grid after the checkout and transfers
int n;
cout << "Enter the number of new patients to be admiited to rooms" << endl;
cin >> n;
int countVacant = countVacantRooms(rooms);
if (n > countVacant)
{
cout << "Number of new patients are more than the rooms available" << endl;
}
else
{
addNewPatients(rooms,n);
}
displayRooms(rooms);//For displaying the final room grid after the end of the day
system("pause");
}
//Reads the input file and populate the two dimensional room grid array
void readFile(int (&rooms)[5][8],string fileAddress)
{
ifstream roomFile(fileAddress);
if (roomFile.is_open())
{
for (int floor = 0; floor < 5; ++floor)
{
string line;
getline(roomFile, line);
stringstream ssin(line);
string floorName;
ssin >> floorName;
for (int roomNo = 0; roomNo < 8; ++roomNo)
{
ssin >> rooms[floor][roomNo];
}
}
roomFile.close();
}
else
{
cout << "Incorrect Address for input file" << endl;
}
}
//To display the room grid
void displayRooms(int rooms[5][8])
{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 8; ++j)
{
if (rooms[i][j] == 0)
{
cout << 'V' << " ";
}
else if (rooms[i][j] == 1)
{
cout << 'C' << " ";
}
else if (rooms[i][j] == 2)
{
cout << 'O' << " ";
}
else if (rooms[i][j] == 3)
{
cout << 'N' << " ";
}
else if (rooms[i][j] == 4)
{
cout << 'T' << " ";
}
else if (rooms[i][j] == 5)
{
cout << 'U' << " ";
}
}
cout << endl;
}
}
//Return the count of number of vacant rooms
//Pass the 2D array holding the room grid as input
int countVacantRooms(int rooms[5][8])
{
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (rooms[i][j] == 0)
{
count++;
}
}
}
return count;
}
//Performs the checkout of patients and update the room grid
void performCheckouts(int (&rooms)[5][8])
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (rooms[i][j] == 1)
{
rooms[i][j]=0;
}
}
}
}
void performTransfers(int (&rooms)[5][8])
{
int** vacantRooms = getVacantArray(rooms);
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (rooms[i][j] == 4)
{
*vacantRooms[count] = 2;
rooms[i][j] = 0;
vacantRooms = getVacantArray(rooms);
}
}
}
}
int** getVacantArray(int rooms[5][8])
{
int VacantAfterCheckout = countVacantRooms(rooms);
int** vacantRooms;
vacantRooms = new int*[VacantAfterCheckout];
int count = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (rooms[i][j] == 0)
{
vacantRooms[count++] = &rooms[i][j];
}
}
}
return vacantRooms;
}
//Add the new patients at vacant locations and uograde the room grid
void addNewPatients(int(&rooms)[5][8], int count)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 8; j++)
{
if (rooms[i][j] == 0)
{
if (count > 0)
{
rooms[i][j] = 2;
count--;
}
else
{
return;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.