Preview:
#include <stdio.h>

int main() {
    int n, i, j, temp, time = 0, count, completed = 0, sum_wait = 0, sum_turnaround = 0, start;
    float avg_wait, avg_turnaround;
    int at[10], bt[10], p[10]; // Arrays for arrival times, burst times, and process numbers

    printf("Enter the number of processes: ");
    scanf("%d", &n);

    // Input arrival times and burst times for each process
    for (i = 0; i < n; i++) {
        printf("Enter the arrival time and burst time for process %d: ", i + 1);
        scanf("%d%d", &at[i], &bt[i]);
        p[i] = i + 1;
    }

    // Sort processes by arrival times, and then by burst times
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if (at[i] > at[j] || (at[i] == at[j] && bt[i] > bt[j])) {
                temp = at[i];
                at[i] = at[j];
                at[j] = temp;
                temp = bt[i];
                bt[i] = bt[j];
                bt[j] = temp;
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }

    printf("\nProcess\tAT\tBT\tWT\tTAT\n");

    // Main loop to calculate waiting times and turnaround times
    while (completed < n) {
        count = 0;
        for (i = completed; i < n; i++) {
            if (at[i] <= time) {
                count++;
            } else {
                break;
            }
        }

        // Sort the ready processes by burst time
        if (count > 1) {
            for (i = completed; i < completed + count - 1; i++) {
                for (j = i + 1; j < completed + count; j++) {
                    if (bt[i] > bt[j]) {
                        temp = at[i];
                        at[i] = at[j];
                        at[j] = temp;
                        temp = bt[i];
                        bt[i] = bt[j];
                        bt[j] = temp;
                        temp = p[i];
                        p[i] = p[j];
                        p[j] = temp;
                    }
                }
            }
        }

        start = time;
        time += bt[completed];
        printf("P[%d]\t%d\t%d\t%d\t%d\n", p[completed], at[completed], bt[completed], time - at[completed] - bt[completed], time - at[completed]);
        sum_wait += time - at[completed] - bt[completed];
        sum_turnaround += time - at[completed];
        completed++;
    }

    avg_wait = (float)sum_wait / (float)n;
    avg_turnaround = (float)sum_turnaround / (float)n;

    printf("Average waiting time is %.2f\n", avg_wait);
    printf("Average turnaround time is %.2f\n", avg_turnaround);

    return 0;
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter