Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <iostream>

using namespace std;

const int MAX_PROCESSES = 5;

class Scheduler {
private:
int cpu_burst[MAX_PROCESSES];
int arrival_time[MAX_PROCESSES];
int completion_time[MAX_PROCESSES];
int turn_around_time[MAX_PROCESSES];
int waiting_time[MAX_PROCESSES];
float avg_turnaround_time;
float avg_waiting_time;

public:
void get_process_data() {
for (int i = 0; i < MAX_PROCESSES; i++) {
cout << "Enter the CPU burst time for process p" << i << ": ";
cin >> cpu_burst[i];
cout << "Enter the arrival time for process p" << i << ": ";
cin >> arrival_time[i];
}
}

void calculate_completion_time() {
int current_time = 0;
for (int i = 0; i < MAX_PROCESSES; i++) {
current_time += cpu_burst[i];
completion_time[i] = current_time;
}
}

void calculate_turnaround_time() {
for (int i = 0; i < MAX_PROCESSES; i++) {
turn_around_time[i] = completion_time[i] - arrival_time[i];
}
}

void calculate_waiting_time() {
for (int i = 0; i < MAX_PROCESSES; i++) {
waiting_time[i] = turn_around_time[i] - cpu_burst[i];
}
}

void calculate_averages() {
float total_turnaround_time = 0;
float total_waiting_time = 0;
for (int i = 0; i < MAX_PROCESSES; i++) {
total_turnaround_time += turn_around_time[i];
total_waiting_time += waiting_time[i];
}
avg_turnaround_time = total_turnaround_time / MAX_PROCESSES;
avg_waiting_time = total_waiting_time / MAX_PROCESSES;
}

void display_results() {
cout << "Process\t CPU Burst Time\t Arrival Time\t Completion Time\t Turnaround
Time\t Waiting Time\n";
for (int i = 0; i < MAX_PROCESSES; i++) {
cout << "p" << i << "\t\t" << cpu_burst[i] << "\t\t\t" << arrival_time[i] <<
"\t\t\t" << completion_time[i] << "\t\t\t" << turn_around_time[i] << "\t\t\t" <<
waiting_time[i] << endl;
}
cout << "Average Turnaround Time: " << avg_turnaround_time << endl;
cout << "Average Waiting Time: " << avg_waiting_time << endl;
}
};

int main() {
Scheduler scheduler;
scheduler.get_process_data();
scheduler.calculate_completion_time();
scheduler.calculate_turnaround_time();
scheduler.calculate_waiting_time();
scheduler.calculate_averages();
scheduler.display_results();

return 0;
}

You might also like