Program for First Come First Serve Algorithm
C++
// C++ program to Calculate Waiting
// Time for given Processes
#include
using namespace std;
// Function to Calculate waiting time
// and average waiting time
void CalculateWaitingTime(int at[],
int bt[], int N)
{
// Declare the array for waiting
// time
int wt[N];
// Waiting time for first process
// is 0
wt[0] = 0;
// Print waiting time process 1
cout << "PN\t\tAT\t\t"
<< "BT\t\tWT\n\n";
cout << "1"
<< "\t\t" << at[0] << "\t\t"
<< bt[0] << "\t\t" << wt[0] << endl;
// Calculating waiting time for
// each process from the given
// formula
for (int i = 1; i < 5; i++) {
wt[i] = (at[i - 1] + bt[i - 1]
+ wt[i - 1]) - at[i];
// Print the waiting time for
// each process
cout << i + 1 << "\t\t" << at[i]
<< "\t\t" << bt[i] << "\t\t"
<< wt[i] << endl;
}
// Declare variable to calculate
// average
float average;
float sum = 0;
// Loop to calculate sum of all
// waiting time
for (int i = 0; i < 5; i++) {
sum = sum + wt[i];
}
// Find average waiting time
// by dividing it by no. of process
average = sum / 5;
// Print Average Waiting Time
cout << "\nAverage waiting time = "
<< average;
}
// Driver code
int main()
{
// Number of process
int N = 5;
// Array for Arrival time
int at[] = { 0, 1, 2, 3, 4 };
// Array for Burst Time
int bt[] = { 4, 3, 1, 2, 5 };
// Function call to find
// waiting time
CalculateWaitingTime(at, bt, N);
return 0;
}
//this code is contributed by snehalsalokhe
Java
// Java program to Calculate Waiting
// Time for given Processes
class GFG
{
// Function to Calculate waiting time
// and average waiting time
static void CalculateWaitingTime(int at[],
int bt[], int N)
{
// Declare the array for waiting
// time
int []wt = new int[N];
// Waiting time for first process
// is 0
wt[0] = 0;
// Print waiting time process 1
System.out.print("P.No.\tArrival Time\t"
+ "Burst Time\tWaiting Time\n");
System.out.print("1"
+ "\t\t" + at[0]+ "\t\t"
+ bt[0]+ "\t\t" + wt[0] +"\n");
// Calculating waiting time for
// each process from the given
// formula
for (int i = 1; i < 5; i++) {
wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];
// Print the waiting time for
// each process
System.out.print(i + 1+ "\t\t" + at[i]
+ "\t\t" + bt[i]+ "\t\t"
+ wt[i] +"\n");
}
// Declare variable to calculate
// average
float average;
float sum = 0;
// Loop to calculate sum of all
// waiting time
for (int i = 0; i < 5; i++) {
sum = sum + wt[i];
}
// Find average waiting time
// by dividing it by no. of process
average = sum / 5;
// Print Average Waiting Time
System.out.print("Average waiting time = "
+ average);
}
// Driver code
public static void main(String[] args)
{
// Number of process
int N = 5;
// Array for Arrival time
int at[] = { 0, 1, 2, 3, 4 };
// Array for Burst Time
int bt[] = { 4, 3, 1, 2, 5 };
// Function call to find
// waiting time
CalculateWaitingTime(at, bt, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to Calculate Waiting
# Time for given Processes
# Function to Calculate waiting time
# and average waiting time
def CalculateWaitingTime(at, bt, N):
# Declare the array for waiting
# time
wt = [0]*N;
# Waiting time for first process
# is 0
wt[0] = 0;
# Print waiting time process 1
print("P.No.\tArrival Time\t" , "Burst Time\tWaiting Time");
print("1" , "\t\t" , at[0] , "\t\t" , bt[0] , "\t\t" , wt[0]);
# Calculating waiting time for
# each process from the given
# formula
for i in range(1,5):
wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];
# Print the waiting time for
# each process
print(i + 1 , "\t\t" , at[i] , "\t\t" , bt[i] , "\t\t" , wt[i]);
# Declare variable to calculate
# average
average = 0.0;
sum = 0;
# Loop to calculate sum of all
# waiting time
for i in range(5):
sum = sum + wt[i];
# Find average waiting time
# by dividing it by no. of process
average = sum / 5;
# Print Average Waiting Time
print("Average waiting time = " , average);
# Driver code
if __name__ == '__main__':
# Number of process
N = 5;
# Array for Arrival time
at = [ 0, 1, 2, 3, 4 ];
# Array for Burst Time
bt = [ 4, 3, 1, 2, 5 ];
# Function call to find
# waiting time
CalculateWaitingTime(at, bt, N);
# This code is contributed by 29AjayKumar
C#
// C# program to Calculate Waiting
// Time for given Processes
using System;
class GFG
{
// Function to Calculate waiting time
// and average waiting time
static void CalculateWaitingTime(int []at,
int []bt, int N)
{
// Declare the array for waiting
// time
int []wt = new int[N];
// Waiting time for first process
// is 0
wt[0] = 0;
// Print waiting time process 1
Console.Write("P.No.\tArrival Time\t"
+ "Burst Time\tWaiting Time\n");
Console.Write("1"
+ "\t\t" + at[0]+ "\t\t"
+ bt[0]+ "\t\t" + wt[0] +"\n");
// Calculating waiting time for
// each process from the given
// formula
for (int i = 1; i < 5; i++) {
wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];
// Print the waiting time for
// each process
Console.Write(i + 1+ "\t\t" + at[i]
+ "\t\t" + bt[i]+ "\t\t"
+ wt[i] +"\n");
}
// Declare variable to calculate
// average
float average;
float sum = 0;
// Loop to calculate sum of all
// waiting time
for (int i = 0; i < 5; i++) {
sum = sum + wt[i];
}
// Find average waiting time
// by dividing it by no. of process
average = sum / 5;
// Print Average Waiting Time
Console.Write("Average waiting time = "
+ average);
}
// Driver code
public static void Main(String[] args)
{
// Number of process
int N = 5;
// Array for Arrival time
int []at = { 0, 1, 2, 3, 4 };
// Array for Burst Time
int []bt = { 4, 3, 1, 2, 5 };
// Function call to find
// waiting time
CalculateWaitingTime(at, bt, N);
}
}
// This code is contributed by 29AjayKumar
Javascript
// Function to calculate waiting time and average waiting time
function calculateWaitingTime(at, bt, n) {
// Declare the array for waiting time
let wt = new Array(n);
// Waiting time for first process is 0
wt[0] = 0;
// Print waiting time for process 1
console.log("PN\t\tAT\t\tBT\t\tWT\n\n");
console.log(`1\t\t${at[0]}\t\t${bt[0]}\t\t${wt[0]}\n`);
// Calculate waiting time for each process from the given formula
for (let i = 1; i < n; i++) {
wt[i] = (at[i - 1] + bt[i - 1] + wt[i - 1]) - at[i];
// Print the waiting time for each process
console.log(`${i + 1}\t\t${at[i]}\t\t${bt[i]}\t\t${wt[i]}\n`);
}
// Declare variable to calculate average
let average;
let sum = 0;
// Loop to calculate sum of all waiting time
for (let i = 0; i < n; i++) {
sum = sum + wt[i];
}
// Find average waiting time by dividing it by no. of process
average = sum / n;
// Print Average Waiting Time
console.log(`\nAverage waiting time = ${average}`);
}
// Driver code
function main() {
// Number of processes
let n = 5;
// Array for arrival time
let at = [0, 1, 2, 3, 4];
// Array for burst time
let bt = [4, 3, 1, 2, 5];
// Function call to find waiting time
calculateWaitingTime(at, bt, n);
}
// Call the main function
main();...