#include<stdio.h>
typedef struct process
{
	int id, at, bt, ct=0, rt=0, tat=0, wt=0,at1=0;/* at=arrival time,bt=burst time,ct=completed time,rt=respond time,tat= turn around time ,wt=waiting time*/
int pri=0;
}pro;

void swap(pro &a, pro &b)
{
	pro temp = a;
	a = b;
	b = temp;
}

void sort_by_arrival_time(pro a[], int n)
{
	for (int i = 0; i < n-1; i++)
		for (int j = i + 1; j < n; j++)
		{
			if (a[i].at1 > a[j].at1)
				swap(a[i], a[j]);
			else if (a[i].at1 == a[j].at1)
			{
				if (a[i].bt > a[j].bt)
					swap(a[i], a[j]);
			}
			else{}
		}
}

void sort_by_id(pro a[], int n)
{
	for (int i = 0; i < n - 1; i++)
		for (int j = i + 1; j < n; j++)
			if (a[i].id>a[j].id)
				swap(a[i], a[j]);
}

void Calculate(pro* a, int n)
{
	a[0].at = a[0].at1;
	for (int i = 1; i < n; i++) //calculate "at" of every process in gain chart
		a[i].at = a[i - 1].at + a[i - 1].bt;
	for (int i = 0; i < n; i++) //calculate "ct" and "rt" of every process in gain chart
		a[i].ct = a[i].at + a[i].bt;
	for (int i = 0; i < n; i++) //calculate "rt" and "rt" of every process in gain chart
		a[i].rt = a[i].at - a[i].at1;
	for (int i = 0; i < n; i++) //calculate "tat" and "rt" of every process in gain chart
		a[i].tat = a[i].ct - a[i].at1;
	for (int i = 0; i < n; i++) // //calculate "wt" and "rt" of every process in gain chart
		a[i].wt = a[i].tat - a[i].bt;
}
void Table_Process(pro a[], int n)
{
	printf("PROCESS\tAT\tBT\tCT\tRT\tTAT\tWT\n");
	for (int i = 0; i < n; i++)
		printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", a[i].id, a[i].at1, a[i].bt, a[i].ct, a[i].rt, a[i].tat, a[i].wt);
}
void Gain_Chart(pro a[], int n)
{
	printf("Gain chart is : \n");
	for (int i = 0; i < n; i++)
		printf("%d______________", a[i].at);
	printf("%d", a[n - 1].ct); 
	printf(" (s)\n");
	printf("\tP%d", a[0].id);
	for (int i = 1; i < n; i++)
		printf("\t\tP%d", a[i].id);
	printf("\n");
}
void sum_average(pro p[],int n,float &totwt,float &totat)
{
	for(int i=0;i<n;i++)
	{
		totwt=totwt+p[i].wt;
		totat=totat+p[i].tat;
	}
	totwt =totwt/n;
	totat =totat/n;	
}
void sort_by_priority(pro p[],int n)
{
	for(int i=0;i<n-1;i++)
		for(int j=i+1;j<n;j++)
		{
			if(p[i].at >= p[j].at)
				if(p[i].pri
		}
}
int main()
{
	int n;
	pro *p;
	float totwt=0,totat=0;
	printf("How many process in ready queue?");
	scanf("%d", &n);
	p = new pro[n];
	for (int i = 0; i < n; i++)
	{
		printf("Insert arrival time & burst time of process P%d\n",i+1);
		scanf("%d%d", &p[i].at1, &p[i].bt);
		p[i].id = i + 1;
	}
	sort_by_arrival_time(p, n);
	Calculate(p,n);
	Gain_Chart(p, n);
	printf("\n");
	sort_by_id(p, n);
	Table_Process(p, n);
	sum_average(p,n,totwt,totat);
	printf("\nAverage Waiting Time = %2.2f (s)",totwt);
	printf("\nAverage Turn Around Time = %2.2f(s)\n",totat);
	return 0;
} 
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
}