#include <iostream>
#include <algorithm> 
#include <iomanip>
#include <string.h> 

using namespace std;

struct process {
    int PID;
    int ArrivalTime;
    int BurstTime;
    int Priority;
    int StartTime;
    int EndTime;
    int WaitTime;
    int TurnaroundTime;
    
};

int main()
{
    char choice;
    do {
    int n;
    struct process p[100];
    float avg_turnaround_time;
    float avg_waiting_time;
    int total_turnaround_time = 0;
    int total_waiting_time = 0;
    int total_idle_time = 0;
    int burst_remaining[100];
    int is_completed[100];
    memset(is_completed,0,sizeof(is_completed));

    cout << setprecision(2) << fixed;
     do {
        cout<<"Enter the number of processes: ";
        cin>>n;
     if (n >= 10) {
         cout << "That number is invalid. Please make sure you enter a number between 1 and 10" << endl;
         return 0; 
         cout << "That number works. Next player" << endl;
        }
   } while (n >= 10);
   
 

    for(int i = 0; i < n; i++) {
        cout<<endl<<"Enter arrival time of process "<<i+1<<": ";
        cin>>p[i].ArrivalTime;
        cout<<"Enter burst time of process "<<i+1<<": ";
        cin>>p[i].BurstTime;
        cout<<"Enter Priority of the process "<<i+1<<": ";
        cin>>p[i].Priority;
        p[i].PID = i+1;
        burst_remaining[i] = p[i].BurstTime;
}
 int current_time = 0;
    int completed = 0;
    int prev = 0;

    while(completed != n) {
        int idx = -1;
        int mx = -1;
        for(int i = 0; i < n; i++) {
            if(p[i].ArrivalTime <= current_time && is_completed[i] == 0) {
                if(p[i].Priority > mx) {
                    mx = p[i].Priority;
                    idx = i;
                }
                if(p[i].Priority == mx) {
                    if(p[i].ArrivalTime < p[idx].ArrivalTime) {
                        mx = p[i].Priority;
                        idx = i;
                    }
                }
            }
        }

        if(idx != -1) {
            if(burst_remaining[idx] == p[idx].BurstTime) {
                p[idx].StartTime = current_time;
                total_idle_time += p[idx].StartTime - prev;
            }
            burst_remaining[idx] -= 1;
            current_time++;
            prev = current_time;
            
            if(burst_remaining[idx] == 0) {
                p[idx].EndTime = current_time;
                p[idx].TurnaroundTime = p[idx].EndTime - p[idx].ArrivalTime;
                p[idx].WaitTime = p[idx].TurnaroundTime - p[idx].BurstTime;

                total_turnaround_time += p[idx].TurnaroundTime;
                total_waiting_time += p[idx].WaitTime;

                is_completed[idx] = 1;
                completed++;
            }
        }
        else {
             current_time++;
        }  
    }

    int min_ArrivalTime = 10000000;
    int max_completion_time = -1;
    for(int i = 0; i < n; i++) {
        min_ArrivalTime = min(min_ArrivalTime,p[i].ArrivalTime);
        max_completion_time = max(max_completion_time,p[i].EndTime);
    }

    avg_turnaround_time = (float) total_turnaround_time / n;
    avg_waiting_time = (float) total_waiting_time / n;
    

    cout<<endl<<endl;

    cout<<""<<"AT\t"<<"BT\t"<<"PRI\t"<<"ST\t"<<"ET\t"<<"TAT\t"<<"WT\t"<<"\n"<<endl;

    for(int i = 0; i < n; i++) {
        cout<<p[i].PID<<"\t"<<p[i].ArrivalTime<<"\t"<<p[i].BurstTime<<"\t"<<p[i].Priority<<"\t"<<p[i].StartTime<<"\t"<<p[i].EndTime<<"\t"<<p[i].TurnaroundTime<<"\t"<<p[i].WaitTime<<endl;
    }
    cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;
    cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;
     cout<<"do you want to do it again ? Y/N ";
     cin>>choice;
    }while(choice=='Y' || 'y');
    } 
by

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}