Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class Process {
- public:
- int pid;
- int arrivalTime;
- int burstTime;
- int completionTime;
- int turnaroundTime;
- int waitingTime;
- int responseTime;
- Process(int pid, int arrivalTime, int burstTime) {
- this->pid = pid;
- this->arrivalTime = arrivalTime;
- this->burstTime = burstTime;
- }
- };
- int main() {
- const int n = 5;
- Process* processes[n];
- for (int i = 0; i < n; i++) {
- int at, bt;
- cout << "Enter arrival time for process " << (i+1) << ": ";
- cin >> at;
- cout << "Enter burst time for process " << (i+1) << ": ";
- cin >> bt;
- processes[i] = new Process(i+1, at, bt);
- }
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (processes[j]->arrivalTime > processes[j + 1]->arrivalTime) {
- Process* temp = processes[j];
- processes[j] = processes[j + 1];
- processes[j + 1] = temp;
- }
- }
- }
- int currentTime = 0;
- float totalTurnaroundTime = 0;
- float totalWaitingTime = 0;
- for (int i = 0; i < n; i++) {
- if (currentTime < processes[i]->arrivalTime) {
- currentTime = processes[i]->arrivalTime;
- }
- processes[i]->completionTime = currentTime + processes[i]->burstTime;
- processes[i]->turnaroundTime = processes[i]->completionTime - processes[i]->arrivalTime;
- processes[i]->waitingTime = processes[i]->turnaroundTime - processes[i]->burstTime;
- processes[i]->responseTime = currentTime - processes[i]->arrivalTime;
- currentTime += processes[i]->burstTime;
- totalTurnaroundTime += processes[i]->turnaroundTime;
- totalWaitingTime += processes[i]->waitingTime;
- }
- Process* sortedProcesses[n];
- for (int i = 0; i < n; i++) {
- sortedProcesses[i] = processes[i];
- }
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n - i - 1; j++) {
- if (sortedProcesses[j]->pid > sortedProcesses[j + 1]->pid) {
- Process* temp = sortedProcesses[j];
- sortedProcesses[j] = sortedProcesses[j + 1];
- sortedProcesses[j + 1] = temp;
- }
- }
- }
- cout << "\nPID\tAT\tBT\tCT\tTT\tWT\tRT\n";
- for (int i = 0; i < n; i++) {
- cout << sortedProcesses[i]->pid << "\t"
- << sortedProcesses[i]->arrivalTime << "\t"
- << sortedProcesses[i]->burstTime << "\t"
- << sortedProcesses[i]->completionTime << "\t"
- << sortedProcesses[i]->turnaroundTime << "\t"
- << sortedProcesses[i]->waitingTime << "\t"
- << sortedProcesses[i]->responseTime << "\n";
- }
- cout << fixed << setprecision(2);
- cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;
- cout << "Average Waiting Time: " << totalWaitingTime / n << endl;
- cout << "\nGantt Chart:\n";
- cout << "|";
- for (int i = 0; i < n; i++) {
- cout << " P" << processes[i]->pid << " |";
- }
- cout << "\n0";
- for (int i = 0; i < n; i++) {
- cout << "\t" << processes[i]->completionTime;
- }
- cout << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment