Name: Ajay Chauhan
Branch:CSIT-4A
Roll no: 2400290119001
Lab:-2
Object: Implement shortest job first algorithm using c programming.
#include <stdio.h>
int main() {
  int n, i, j, min, temp;
  int at[20], bt[20], ct[20], wt[20], tat[20], pid[20];
  int completed[20] = {0};
  float avg_wt = 0, avg_tat = 0, time = 0;
  printf("Enter the number of processes: ");
  scanf("%d", &n);
  for(i = 0; i < n; i++) {
      printf("Enter Arrival Time for Process P[%d]: ", i + 1);
      scanf("%d", &at[i]);
      printf("Enter Burst Time for Process P[%d]: ", i + 1);
      scanf("%d", &bt[i]);
      pid[i] = i + 1;
  int count = 0;
  while(count < n) {
      min = -1;
    for(i = 0; i < n; i++) {
        if(at[i] <= time && completed[i] == 0) {
            if(min == -1 || bt[i] < bt[min]) {
                min = i;
    if(min == -1) {
        time++;
    } else {
        ct[min] = time + bt[min];
        tat[min] = ct[min] - at[min];
        wt[min] = tat[min] - bt[min];
        avg_wt += wt[min];
        avg_tat += tat[min];
        completed[min] = 1;
        time = ct[min];
        count++;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for(i = 0; i < n; i++) {
    printf("P[%d]\t%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], ct[i], tat[i], wt[i]);
    }
    printf("\nAverage Waiting Time = %.2f", avg_wt / n);
    printf("\nAverage Turnaround Time = %.2f\n", avg_tat / n);
    return 0;
                                              Output
Enter the number of processes: 2
Enter Arrival Time for Process P[1]: 1
Enter Burst Time for Process P[1]: 4
Enter Arrival Time for Process P[2]: 0
Enter Burst Time for Process P[2]: 6
Process AT          BT    CT   TAT   WT
P[1] 1          4    10    9    5
P[2] 0          6    6     6   0
Average Waiting Time = 2.50
Average Turnaround Time = 7.50