/*
* SJF Scheduling Program in C
*/
#include <stdio.h>
int main()
{
char pid[15][10], tempid[10];
int bt[15], wt[15], tat[15];
int n, i, j, temp, k;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter process ID: ");
for (i = 0; i < n; i++) {
scanf("%s", pid[i]);
}
printf("Enter burst times: ");
for (i = 0; i < n; i++) {
scanf("%d", &bt[i]);
}
// Sort by burst time (SJF non-preemptive)
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (bt[i] > bt[j]) {
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
// swap process ID manually
for (k = 0; k < 10; k++) {
tempid[k] = pid[i][k];
pid[i][k] = pid[j][k];
pid[j][k] = tempid[k];
}
}
}
}
// Waiting time and turnaround time
wt[0] = 0;
for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++)
wt[i] += bt[j];
}
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
float twt = 0, ttat = 0;
printf("\nPID\t\tBT\t\tWT\t\tTAT\n");
for (i = 0; i < n; i++) {
printf("%s\t\t%d\t\t%d\t\t%d\n", pid[i], bt[i], wt[i], tat[i]);
twt += wt[i];
ttat += tat[i];
}
printf("\nAverage Waiting Time = %.2f", twt / n);
printf("\nAverage Turnaround Time = %.2f\n", ttat / n);
return 0;
}