int main()
This line defines a function. main() is where the program starts running.
int n, i, j, temp;
Declares the variables used below. n is the number of elements (or processes). temp holds one value while two are swapped.
int p[20], bt[20], pr[20], wt[20], tat[20];
Declares the variables used below. bt holds each process's burst time (CPU time needed). pr holds each process's priority.
float total_wt = 0, total_tat = 0;
Declares the variables used below. total_wt sums waiting times for the average. total_tat sums turnaround times for the average.
printf("Enter number of processes: ");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
scanf("%d", &n);This line reads input. scanf() reads input into variables; pass their addresses with &. n is the number of elements (or processes).
printf("Enter burst time and priority for each process:\n");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
for (i = 0; i < n; i++)
This line is the loop header. for (init; condition; update) repeats a block a counted number of times. n is the number of elements (or processes).
printf("P%d: ", i + 1);This line prints output. printf() prints formatted output (%d int, %f float, %s string).
scanf("%d %d", &bt[i], &pr[i]);This line reads input. scanf() reads input into variables; pass their addresses with &. bt holds each process's burst time (CPU time needed).
p[i] = i + 1;
i is the loop counter.
for (i = 0; i < n - 1; i++)
This line is the loop header. for (init; condition; update) repeats a block a counted number of times. n is the number of elements (or processes).
for (j = i + 1; j < n; j++)
This line is the loop header. for (init; condition; update) repeats a block a counted number of times. n is the number of elements (or processes).
if (pr[j] < pr[i])
This line is a decision. if runs the block only when the condition is true. pr holds each process's priority.
temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;
Part of a swap: temp holds one value while the two are exchanged. temp holds one value while two are swapped. pr holds each process's priority.
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
Part of a swap: temp holds one value while the two are exchanged. temp holds one value while two are swapped. bt holds each process's burst time (CPU time needed).
temp = p[i]; p[i] = p[j]; p[j] = temp;
Part of a swap: temp holds one value while the two are exchanged. temp holds one value while two are swapped.
wt[0] = 0;
wt holds each process's waiting time.
for (i = 1; i < n; i++)
This line is the loop header. for (init; condition; update) repeats a block a counted number of times. n is the number of elements (or processes).
wt[i] = wt[i - 1] + bt[i - 1];
wt holds each process's waiting time. bt holds each process's burst time (CPU time needed).
tat[i] = wt[i] + bt[i];
tat is the turnaround time = completion − arrival. wt holds each process's waiting time. bt holds each process's burst time (CPU time needed).
total_wt += wt[i];
total_wt sums waiting times for the average. wt holds each process's waiting time.
total_tat += tat[i];
total_tat sums turnaround times for the average. tat is the turnaround time = completion − arrival.
printf("\nProcess\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");This line prints output. printf() prints formatted output (%d int, %f float, %s string).
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i], pr[i], bt[i], wt[i], tat[i]);This line prints output. printf() prints formatted output (%d int, %f float, %s string). pr holds each process's priority.
printf("\nAverage Waiting Time = %.2f\n", total_wt / n);This line prints output. printf() prints formatted output (%d int, %f float, %s string). total_wt sums waiting times for the average.
printf("Average Turnaround Time = %.2f\n", total_tat / n);This line prints output. printf() prints formatted output (%d int, %f float, %s string). total_tat sums turnaround times for the average.
return 0;
This line returns from the function. return exits the function and hands back a value.