In: Computer Science
In c++, please make it short and simple
A hotel chain needs a program which will produce statistics for the hotels it owns.
Write a program which displays the occupancy rate and other statistics for any of the chain’s large hotels (all are 20 floors with 16 rooms each floor).
In main:
-declare a 20 X 16 array of “int” named hotel to represent a hotel’s 20 floors and 16 rooms per floor.
Then in main, repeatedly:
-call a function which fills the hotel array with 1s and 0s, where 1 indicates an occupied room and 0 indicates an unoccupied room. Call a second function to validate that each entry is either 1 or 0 by having the second function access the original entry using a pointer in its parameter list.
-pass the hotel array to a third function which dynamically allocates a new size 20 “int” array with each element representing a floor of the hotel and the floor’s number of occupied rooms; place the number of occupied rooms of each floor into the new array and return it to main.
-display the floor number** and number of occupied rooms on each floor by incrementing the address of the new array.
-also calculate and display the hotel’s overall occupancy rate to 1 decimal (total rooms occupied per total rooms), and the floor number** and number of occupied rooms of the floor with the highest occupancy.
-process another hotel (unknown number of hotels in chain).
**NOTE: hotels do not have a floor numbered 13 due to guests’ superstitions.
To create your screen print, temporarily set the floors and rooms to 3 and 5 respectively to reduce data entry, and enter:
Floor #1: occupied, occupied, unoccupied, occupied, unoccupied
Floor #2: occupied, unoccupied, occupied, occupied, occupied
Floor #3: occupied, unoccupied, unoccupied, unoccupied, occupied
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
Code:
#include<iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string>
#define NUM_FLOORS 20
#define NUM_ROOMS 16
using namespace std;
void fillHotelRandom(int hotel[NUM_FLOORS][NUM_ROOMS])
{
srand (time(NULL));
for(int i=0;i<NUM_FLOORS;i++){
for(int
j=0;j<NUM_ROOMS;j++){
hotel[i][j]=rand()%2;
}
}
}
void fillHotelUser(int hotel[NUM_FLOORS][NUM_ROOMS])
{
string input;
for(int i=0;i<NUM_FLOORS;i++){
if(i!=12){
cout<<"FLOOR #"<<i+1<<": ";
for(int
j=0;j<NUM_ROOMS;j++){
cin>>input;
if(input=="occupied"
||input=="occupied,"){
hotel[i][j]=1;
}else{
hotel[i][j]=0;
}
}
}
}
}
bool validateHotel(int *hotel){
for(int i=0;i<NUM_FLOORS;i++){
if(i!=12){
for(int
j=0;j<NUM_ROOMS;j++){
int val=*((hotel+i*NUM_ROOMS)+j);
if(val!=0 && val!=1){
return false;
}
}
}
}
return true;
}
int* occupiedRooms(int hotel[NUM_FLOORS][NUM_ROOMS]){
int *occupied;
occupied=new int[NUM_FLOORS];
for(int i=0;i<NUM_FLOORS;i++){
if(i!=12){
for(int
j=0;j<NUM_ROOMS;j++){
if(j==0){
occupied[i]=0;
}
if(hotel[i][j]==1){
occupied[i]=occupied[i]+1;
}
}
}
}
return occupied;
}
void main(){
int hotel[NUM_FLOORS][NUM_ROOMS];
int *occupied;
bool result;
char choice='y';
while(choice=='y'){
//fillHotelRandom(hotel);
fillHotelUser(hotel);
result=validateHotel((int
*)hotel);
if(!result){
cout<<"Invalid Data\n";
}else{
occupied=occupiedRooms(hotel);
for(int
i=0;i<NUM_FLOORS;i++){
if(i!=12){
cout<<"Room #"<<i+1<<" Total
rooms booked :"<<occupied[i]<<" Occupancy Rate
:"<<(double)occupied[i]/NUM_ROOMS<<endl<<endl;
}
}
}
cout<<"Do you want to
continue ?(Press y)\n";
cin>>choice;
}
system("pause");
}
Output: