Advertisement
xi_arma

Untitled

Sep 5th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. class Process {
  6. public:
  7.     int pid;
  8.     int arrivalTime;
  9.     int burstTime;
  10.     int completionTime;
  11.     int turnaroundTime;
  12.     int waitingTime;
  13.     int responseTime;
  14.  
  15.     Process(int pid, int arrivalTime, int burstTime) {
  16.         this->pid = pid;
  17.         this->arrivalTime = arrivalTime;
  18.         this->burstTime = burstTime;
  19.     }
  20. };
  21.  
  22. int main() {
  23.     const int n = 5;
  24.     Process* processes[n];
  25.  
  26.     for (int i = 0; i < n; i++) {
  27.         int at, bt;
  28.         cout << "Enter arrival time for process " << (i+1) << ": ";
  29.         cin >> at;
  30.         cout << "Enter burst time for process " << (i+1) << ": ";
  31.         cin >> bt;
  32.         processes[i] = new Process(i+1, at, bt);
  33.     }
  34.  
  35.     for (int i = 0; i < n - 1; i++) {
  36.         for (int j = 0; j < n - i - 1; j++) {
  37.             if (processes[j]->arrivalTime > processes[j + 1]->arrivalTime) {
  38.                 Process* temp = processes[j];
  39.                 processes[j] = processes[j + 1];
  40.                 processes[j + 1] = temp;
  41.             }
  42.         }
  43.     }
  44.  
  45.     int currentTime = 0;
  46.     float totalTurnaroundTime = 0;
  47.     float totalWaitingTime = 0;
  48.  
  49.     for (int i = 0; i < n; i++) {
  50.         if (currentTime < processes[i]->arrivalTime) {
  51.             currentTime = processes[i]->arrivalTime;
  52.         }
  53.  
  54.         processes[i]->completionTime = currentTime + processes[i]->burstTime;
  55.         processes[i]->turnaroundTime = processes[i]->completionTime - processes[i]->arrivalTime;
  56.         processes[i]->waitingTime = processes[i]->turnaroundTime - processes[i]->burstTime;
  57.         processes[i]->responseTime = currentTime - processes[i]->arrivalTime;
  58.  
  59.         currentTime += processes[i]->burstTime;
  60.  
  61.         totalTurnaroundTime += processes[i]->turnaroundTime;
  62.         totalWaitingTime += processes[i]->waitingTime;
  63.     }
  64.  
  65.  
  66.     Process* sortedProcesses[n];
  67.     for (int i = 0; i < n; i++) {
  68.         sortedProcesses[i] = processes[i];
  69.     }
  70.  
  71.     for (int i = 0; i < n - 1; i++) {
  72.         for (int j = 0; j < n - i - 1; j++) {
  73.             if (sortedProcesses[j]->pid > sortedProcesses[j + 1]->pid) {
  74.                 Process* temp = sortedProcesses[j];
  75.                 sortedProcesses[j] = sortedProcesses[j + 1];
  76.                 sortedProcesses[j + 1] = temp;
  77.             }
  78.         }
  79.     }
  80.  
  81.  
  82.     cout << "\nPID\tAT\tBT\tCT\tTT\tWT\tRT\n";
  83.     for (int i = 0; i < n; i++) {
  84.         cout << sortedProcesses[i]->pid << "\t"
  85.              << sortedProcesses[i]->arrivalTime << "\t"
  86.              << sortedProcesses[i]->burstTime << "\t"
  87.              << sortedProcesses[i]->completionTime << "\t"
  88.              << sortedProcesses[i]->turnaroundTime << "\t"
  89.              << sortedProcesses[i]->waitingTime << "\t"
  90.              << sortedProcesses[i]->responseTime << "\n";
  91.     }
  92.  
  93.     cout << fixed << setprecision(2);
  94.     cout << "\nAverage Turnaround Time: " << totalTurnaroundTime / n << endl;
  95.     cout << "Average Waiting Time: " << totalWaitingTime / n << endl;
  96.  
  97.  
  98.     cout << "\nGantt Chart:\n";
  99.     cout << "|";
  100.     for (int i = 0; i < n; i++) {
  101.         cout << " P" << processes[i]->pid << " |";
  102.     }
  103.     cout << "\n0";
  104.     for (int i = 0; i < n; i++) {
  105.         cout << "\t" << processes[i]->completionTime;
  106.     }
  107.     cout << endl;
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement