FCFS Scheduling Algorithm Program in C++ : Write a C++ Program To Calculate Average Waiting Time and Average Turn Around Time using FCFS.
Program Code: FCFS Scheduling Algorithm Program in C++
We shall use the loop concept and the use of arrays to solve this programming assignment. First of all we will define some variables and the required arrays to store the information about the given processes. The user will enter the number of processes to use FCFS. She will use different formulas to calculate waiting times and turn around times of the given processes. Considering C++ programming language the solution of this FCFS program is as follows:
#include<iostream> using namespace std; int main() { int n,bt[10],wt[10],tat[10],total_wt=0, total_tat=0,avwt=0,avtat=0,i,j; cout<<"Enter total number of processes(Maximum 10):"; cin>>n; cout<<"\nEnter Process Burst Time\n"; for(i=0;i<n;i++) { cout<<"P"<<i+1<<" = "; cin>>bt[i]; } // calculate waiting time wt[0] = 0; for (int i = 1; i < n ; i++ ) wt[i] = bt[i-1] + wt[i-1] ; // calculate turn around time = BT + WT for (int i = 0; i < n ; i++) tat[i] = bt[i] + wt[i]; //Display processes with headings cout << "Processes "<< " Burst time " << " Waiting time " << " Turn around time\n"; // Calculate total waiting time and total turn // around time for (int i=0; i<n; i++) { total_wt = total_wt + wt[i]; total_tat = total_tat + tat[i]; cout << " " << i+1 << "\t\t" << bt[i] <<"\t " << wt[i] <<"\t\t " << tat[i] <<endl; } cout << "Average Waiting Time = " << (float)total_wt / (float)n; cout << "\nAverage Turn Around Time = " << (float)total_tat / (float)n; } // end of program
Explanation of Program Code using Comments
//Program To Calculate Average Waiting Time //and Average Turn Around Time using FCFS Scheduling #include<iostream> using namespace std; int main() { int n,bt[10],wt[10],tat[10],total_wt=0, total_tat=0,avwt=0,avtat=0,i,j; cout<<"Enter total number of processes(Maximum 10):"; cin>>n; cout<<"\nEnter Process Burst Time\n"; for(i=0;i<n;i++) { cout<<"P"<<i+1<<" = "; cin>>bt[i]; } // calculate waiting time wt[0] = 0; // waiting time for first process is 0 // calculating waiting time for other processes for (int i = 1; i < n ; i++ ) wt[i] = bt[i-1] + wt[i-1] ; // calculate turn around time = BT + WT // formula turnaround = bt[i] + wt[i] for (int i = 0; i < n ; i++) tat[i] = bt[i] + wt[i]; //Display processes with headings cout << "Processes "<< " Burst time " << " Waiting time " << " Turn around time\n"; // Calculate total waiting time and total turn // around time for (int i=0; i<n; i++) { total_wt = total_wt + wt[i]; total_tat = total_tat + tat[i]; cout << " " << i+1 << "\t\t" << bt[i] <<"\t " << wt[i] <<"\t\t " << tat[i] <<endl; } cout << "Average Waiting Time = " << (float)total_wt / (float)n; cout << "\nAverage Turn Around Time = " << (float)total_tat / (float)n; } // end of program
Output
Enter total number of processes(Maximum 10):3 Enter Process Burst Time P1 = 24 P2 = 3 P3 = 3 Processes Burst time Waiting time Turn around time 1 24 0 24 2 3 24 27 3 3 27 30 Average Waiting Time = 17 Average Turn Around Time = 27 -------------------------------- Process exited after 35.88 seconds with return value 0 Press any key to continue . . .