• Source
    1. // C++ program for implementation of FCFS
    2. // scheduling
    3. #include<iostream>
    4. using namespace std;
    5.  
    6. // Function to find the waiting time for all
    7. // processes
    8. void findWaitingTime(int processes[], int n,
    9. int bt[], int wt[])
    10. {
    11. // waiting time for first process is 0
    12. wt[0] = 0;
    13.  
    14. // calculating waiting time
    15. for (int i = 1; i < n ; i++ )
    16. wt[i] = bt[i-1] + wt[i-1] ;
    17. }
    18.  
    19. // Function to calculate turn around time
    20. void findTurnAroundTime( int processes[], int n,
    21. int bt[], int wt[], int tat[])
    22. {
    23. // calculating turnaround time by adding
    24. // bt[i] + wt[i]
    25. for (int i = 0; i < n ; i++)
    26. tat[i] = bt[i] + wt[i];
    27. }
    28.  
    29. //Function to calculate average time
    30. void findavgTime( int processes[], int n, int bt[])
    31. {
    32. int wt[n], tat[n], total_wt = 0, total_tat = 0;
    33.  
    34. //Function to find waiting time of all processes
    35. findWaitingTime(processes, n, bt, wt);
    36.  
    37. //Function to find turn around time for all processes
    38. findTurnAroundTime(processes, n, bt, wt, tat);
    39.  
    40. //Display processes along with all details
    41. cout << "Processes "<< " Burst time "
    42. << " Waiting time " << " Turn around time\n";
    43.  
    44. // Calculate total waiting time and total turn
    45. // around time
    46. for (int i=0; i<n; i++)
    47. {
    48. total_wt = total_wt + wt[i];
    49. total_tat = total_tat + tat[i];
    50. cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
    51. << wt[i] <<"\t\t " << tat[i] <<endl;
    52. }
    53.  
    54. cout << "Average waiting time = "
    55. << (float)total_wt / (float)n;
    56. cout << "\nAverage turn around time = "
    57. << (float)total_tat / (float)n;
    58. }
    59.  
    60. // Driver code
    61. int main()
    62. {
    63. //process id's
    64. int processes[] = { 1, 2, 3};
    65. int n = sizeof processes / sizeof processes[0];
    66.  
    67. //Burst time of all processes
    68. int burst_time[] = {10, 5, 8};
    69.  
    70. findavgTime(processes, n, burst_time);
    71. return 0;
    72. }