In: Computer Science
C++
good documentation as well as explanations would be great.
In this HW assignment we will simulate a car wash and calculate the arrival time, departure time and wait time for each car that came in for a car wash. We will use the following assumptions about our car wash:
Each car takes 3 minutes from start of the car wash to the end.
Only one car can be in the car wash at a time. This mean that if a car enters a car wash at 1:00 PM then no car can enter the car wash till 1:03 PM.
Simulation will start at the opening time of the car wash and will be represented with 0. We do not not care about the actual time as we are interested in time intervals. Example: a car arrived at time 5 means that car arrived 5 minutes after the opening of the car wash.
We will assume that car wash opens at 8:00 AM and closes at 5:00 PM. This implies that car wash is open for 9 hours or 540 minutes.
Simulation will run for number of minutes given by SIMULATION_END_TIME. We will set it to 540 as our car wash is open for 540 minutes.
The arrival time of cars will be stored in the file named arrival_time.txt. The file will list the arrival time of one car per line. The numbers of cars that will come to our car wash will be variable. You can have an input file with arrival time for 5 cars or for 100 cars. Example of input file is given at the end of this handout. Any car that arrives after the closing time given by SIMULATION_END_TIME will not be serviced.
The program will display the following information about each car: car number, arrival time, car wash start time, departure time, wait time, and total time in form of a table.
Arrival time, car wash start time, and departure time will be in minutes from the opening of the car wash.
Arrival time and car wash start time may be same if there are no cars in the car wash.
Wait time is car wash start time – arrival time.
Total time is the departure time – arrival time.
The program will also display the following statistics: total wait time, average wait time, total car wash use time, percentage of time car was was in use.
Hint: Use a Car class to keep track of car number, car arrival time and car wash start time.
Sample Input File:
1
2
4
10
13
15
16
75
Sample Output (Set SIMULATION_END_TIME to 60 for this example):
Opening Time: 8:00 AM (0 minutes)
Closing Time: 9:00 AM (60 minutes)
Start of Simulation
Car Number |
Arrival Time |
Car Wash |
Departure Time |
Wait Time |
Total |
1 |
1 |
1 |
4 |
0 |
3 |
2 |
2 |
4 |
7 |
2 |
5 |
3 |
4 |
7 |
10 |
3 |
6 |
4 |
10 |
10 |
13 |
0 |
3 |
5 |
13 |
13 |
16 |
0 |
3 |
6 |
15 |
16 |
19 |
1 |
4 |
7 |
16 |
19 |
22 |
3 |
6 |
8 |
75 |
Car arrived after closing time and was not served. |
End of Simulation
Statistics:
Total wait time: 9 minutes
Average wait time: 1 minutes and 17 seconds
Total car wash use time: 21 minutes
Percentage of time car was was in use: 35%
Notes:
Use Queue STL to keep track of the arrival time.
Multiple cars can arrive at same time.
Input file is sorted by arrival time.
// Some topic to refer for setw
http://www.cplusplus.com/reference/iomanip/setw/
// class in c++
https://www.geeksforgeeks.org/c-classes-and-objects/
// queue in c++ https://www.geeksforgeeks.org/queue-cpp-stl/
// Reading data from file
http://www.cplusplus.com/reference/cstdio/freopen/
// Variable name are written in form such that you will understand
what is going in example.
#include<bits/stdc++.h>
using namespace std;
class car
{
public:
int
Car_number,Arrival_time,Car_wash_start_time,Departure_time,Wait_time,Total_time;
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
fstream file;
ifstream myfile;
myfile.open("input-1.txt"); //
https://www.geeksforgeeks.org/file-handling-c-classes/ read form
here
// Arrival time of each car is in sorted order as mentioned in
question
int t,cnt=0,i=0;
queue<int> q1;
while(myfile>>t) // Reading data from file till there is data
in file..
{ q1.push(t); cout<<t<<" "; cnt++; }
cout<<endl;
car a1[cnt];
int p=0,l=0,i1,Total_wait_time=0;
while(!q1.empty()) // Storing data in queue
{
t=q1.front();
p=max(p,t); if(p>537) break; // if(p>537) it means before
closing time car can not be washed so we are breaking loop .
q1.pop();
a1[i].Car_number=i+1;
a1[i].Arrival_time=t; // Here in loop p is max( p,t) which means
let arrival time of new car is 10 and we have take 15 minutes to
process car which came before it so
// now this car will be processed after 15 minute and if arrival of
new car is 20 and we have taken 10 minute to preproces previous car
so we have time left but we
// can't do anything with it we have to wait till 20 . so p=
max(p,t)
a1[i].Car_wash_start_time=p; p+=3;
a1[i].Departure_time=p; // Car is washed for 3 minute so car
departure time is p+3;
a1[i].Wait_time=a1[i].Car_wash_start_time -
a1[i].Arrival_time;
Total_wait_time+=a1[i].Wait_time;
a1[i].Total_time=a1[i].Wait_time+3; i++;
l++;
}
cout<<"Opening Time: 8:00 AM(0 minutes)"<<endl;
cout<<"Closing Time: 5:00 PM(540 minutes)"<<endl;
cout<<"Start of Simulation"<<endl;
cout<<"Car Number Arrival Time Car Wash Departue Wait Time
Total"<<endl;
cout<<setw(34)<<"Start
time"<<setw(9)<<"time"<<setw(21)<<"time"<<endl;
cout<<"-----------------------------------------------------------------------------"<<endl;
for(i1=0;i1<l;i1++)
{
cout<<setw(10)<<a1[i1].Car_number<<setw(12)<<a1[i1].Arrival_time<<setw(11)<<a1[i1].Car_wash_start_time<<setw(9)<<a1[i1].Departure_time<<setw(14)<<a1[i1].Wait_time<<setw(8)<<a1[i1].Total_time<<endl;
}
int Average_wait_time_in_min=0,Average_wait_time_in_sec=0;
int Total_car_wash_use_time=i*3;
if(i>0)
{
Average_wait_time_in_min=Total_wait_time/i; // To find minute part
of average wait time we have multiplied total time*60 and divided
it by i
Average_wait_time_in_sec=((Total_wait_time-(Average_wait_time_in_min*i))*60)/i;
}
if(!q1.empty())
{
while(!q1.empty())
{
cout<<setw(10)<<i+1<<setw(12)<<q1.front()<<"
Car arrived after Simulation time and was not served"<<endl;
i++; q1.pop();}
}
int
Percentage_of_time_car_was_in_use=(Total_car_wash_use_time*100)/540;
// percenatage would be total time used *100 divide by total time
we have
cout<<"End of Simulation"<<endl<<endl;
cout<<"Statistics:"<<endl;
cout<<setw(5)<<"Total wait time :
"<<setw(20)<<Total_wait_time<<"minutes"<<endl;
cout<<setw(5)<<"Average wait time:
"<<setw(20)<< Average_wait_time_in_min<<" minutes
and "<<Average_wait_time_in_sec<<"
seconds"<<endl;
cout<<setw(5)<<"Total car wash use time:
"<<setw(14)<<Total_car_wash_use_time<<"
minutes"<<endl;
cout<<setw(5)<<"Percentage of time car was in
use:"<<setw(4)<<Percentage_of_time_car_was_in_use<<"%"<<endl;
return 0;
}
First line of output is input that i have in my file .
In sample output total time is taken as 1 hour but in question we have total of 9 hours so percentage is calculated accordingly.
Total wait time is sum of wait time in each case.
Average wait time is = ( Total wait time )/ (Number of car washed ).
i hope it helps..
If you have any doubts please comment and please don't dislike.
PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME