MONTE

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Python code and output

# SCT-254-045/2020 Watheri Kelvin Mugwe


#SCT-254-008/2020 Jakes Kariuki
#SCT-254-062/2020 Wamalwa John
#SCT-254-078/2020 Flastine Ahmed
# SCT-254-037/2020 Rosemary Wanjiku
#SCT-254-011/2020 Leonn Ngigi
#SCT-254-017/2020 Esther Watiri
num_customers = 15
arrivals = [82, 91, 12, 77, 90, 75, 33, 61, 19, 58, 41, 54, 52, 16, 86]
iat = [4, 5, 1, 4, 5, 4, 2, 3, 1, 3, 2, 3, 3, 1, 4]
service_times = [6, 3, 5, 4, 3, 3, 3, 6, 6, 3, 6, 2, 1, 1, 4]
on_clock = 0
service_begin = 0
service_end = 0
queue_length = 0
total_idle_time = 0
total_delay = 0

print('{:<8}{:<8}{:<15}{:<8}{:<8}{:<15}{:<15}{:<8}{:<15}{:<8}'.format(
'Customer', 'RN', 'Arrival time', 'IAT', 'KN', 'Service Begin', 'Service
End', 'No in', 'Service Idle', 'Delay'))
for i in range(num_customers):
arrival_time = 0 if i == 0 else arrivals[i-1] + iat[i-1]
service_time = service_times[i]
if arrival_time > on_clock:
total_idle_time += arrival_time - on_clock
on_clock = arrival_time
service_begin = on_clock
service_end = service_begin + service_time
queue_length = 0 if i == 0 else i - 1
if service_end < arrivals[i]:
total_idle_time += arrivals[i] - service_end
on_clock = arrivals[i]
service_begin = on_clock
service_end = service_begin + service_time
delay = service_begin - arrivals[i] if service_begin - arrivals[i] > 0 else 0
total_delay += delay
print('{:<8}{:<8}{:<15}{:<8}{:<8}{:<15}{:<15}{:<8}{:<15}{:<8}'.format(
i+1, arrivals[i], str(arrival_time) + 'pm', iat[i], on_clock,
str(service_begin) + 'pm', str(service_end) + 'pm', queue_length,
str(service_time), delay))
on_clock = service_end

print('\n')
print('{:<28}{:<15}{:<8}{:<8}{:<10}{:<10}{:<8}{:<15}{:<8}'.format(
'TOTAL', '', '', '', '', '', '', '', ''))
print('{:<28}{:<15}{:<8}{:<8}{:<10}{:<10}{:<8}{:<15}{:<8}'.format(
'', '', '', '', '', '', '', str(total_idle_time), total_delay))
print('\n')

avg_delay = total_delay / num_customers


avg_idle_time = total_idle_time / num_customers
print('Average delay:', avg_delay)
print('Average idle time:', avg_idle_time)
avg_waiting_time = total_delay / num_customers
print('Average waiting time:', avg_waiting_time)

C++ code and output


//SCT-254-045/2020 Watheri Kelvin Mugwe
//SCT-254-008/2020 Jakes Kariuki
//SCT-254-062/2020 Wamalwa John
//SCT-254-078/2020 Flastine Ahmed
// SCT-254-037/2020 Rosemary Wanjiku
//SCT-254-011/2020 Leonn Ngigi
//SCT-254-017/2020 Esther Watiri
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int num_customers = 15;
int arrivals[num_customers] = {82, 91, 12, 77, 90, 75, 33, 61, 19, 58, 41,
54, 52, 16, 86};
int iat[num_customers] = {4, 5, 1, 4, 5, 4, 2, 3, 1, 3, 2, 3, 3, 1, 4};
int service_times[num_customers] = {6, 3, 5, 4, 3, 3, 3, 6, 6, 3, 6, 2, 1, 1,
4};
int on_clock = 0;
int service_begin = 0;
int service_end = 0;
int queue_length = 0;
int total_idle_time = 0;
int total_delay = 0;
cout << left << setw(8) << "Customer" << setw(8) << "RN" << setw(15) <<
"Arrival time"
<< setw(8) << "IAT" << setw(8) << "KN" << setw(15) << "Service Begin" <<
setw(15) << "Service End" << setw(8) << "No in" << setw(15) << "Service Idle" <<
setw(8) << "Delay"
<< endl;
for (int i = 0; i < num_customers; i++)
{
int arrival_time = (i == 0) ? 0 : arrivals[i - 1] + iat[i - 1];
int service_time = service_times[i];
if (arrival_time > on_clock)
{
total_idle_time += arrival_time - on_clock;
on_clock = arrival_time;
}
service_begin = on_clock;
service_end = service_begin + service_time;
queue_length = (i == 0) ? 0 : i - 1;
if (service_end < arrivals[i])
{
total_idle_time += arrivals[i] - service_end;
on_clock = arrivals[i];
service_begin = on_clock;
service_end = service_begin + service_time;
}
int delay = (service_begin - arrivals[i] > 0) ? service_begin -
arrivals[i] : 0;
total_delay += delay;
cout << left << setw(8) << i + 1 << setw(8) << arrivals[i] << setw(15) <<
to_string(arrival_time) + "pm" << setw(8) << iat[i] << setw(8) << on_clock <<
setw(15) << to_string(service_begin) + "pm" << setw(15) << to_string(service_end)
+ "pm" << setw(8)
<< queue_length << setw(15) << to_string(service_time) << setw(8) <<
delay << endl;
on_clock = service_end;
}
cout << "\n";
cout << "TOTAL" << setw(20) << "" << setw(15) << "" << setw(8) << "" <<
setw(8) << "" << setw(10) << "" << setw(10) << "" << setw(8) << "" << setw(15) <<
to_string(total_idle_time) << setw(8) << total_delay << endl;
cout << "\n";

double avg_delay = (double)total_delay / num_customers;


double avg_idle_time = (double)total_idle_time / num_customers;
cout << "Average delay: " << avg_delay << endl;
cout << "Average idle time: " << avg_idle_time << endl;
double avg_waiting_time = (double)total_delay / num_customers;
cout << "Average waiting time: " << avg_waiting_time << endl;

return 0;
}

You might also like