Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void calculateTimes(int n, int bt[], int at[], int wt[], int tat[]) {
- int timer = 0;
- for (int i = 0; i < n; i++) {
- int minArrivalIndex = -1;
- // Find process with the lowest arrival time that has not been processed
- for (int j = 0; j < n; j++) {
- if (at[j] <= timer && wt[j] == -1) {
- if (minArrivalIndex == -1 || at[j] < at[minArrivalIndex]) {
- minArrivalIndex = j;
- }
- }
- }
- // Process the selected process
- wt[minArrivalIndex] = timer - at[minArrivalIndex];
- tat[minArrivalIndex] = wt[minArrivalIndex] + bt[minArrivalIndex];
- timer += bt[minArrivalIndex];
- }
- }
- void printResults(int n, int wt[], int tat[]) {
- float avgWT = 0, avgTAT = 0;
- printf("Process\tWaiting Time\tTurnaround Time\n");
- for (int i = 0; i < n; i++) {
- printf("%d\t%d\t\t%d\n", i + 1, wt[i], tat[i]);
- avgWT += wt[i];
- avgTAT += tat[i];
- }
- avgWT /= n;
- avgTAT /= n;
- printf("Average Waiting Time: %.2f\n", avgWT);
- printf("Average Turnaround Time: %.2f\n", avgTAT);
- }
- int main() {
- int n;
- printf("Enter the number of processes: ");
- scanf("%d", &n);
- int bt[n], at[n], wt[n], tat[n];
- printf("Enter CPU times:\n");
- for (int i = 0; i < n; i++) {
- scanf("%d", &bt[i]);
- }
- printf("Enter arrival times:\n");
- for (int i = 0; i < n; i++) {
- scanf("%d", &at[i]);
- wt[i] = -1; // Initialize waiting time to -1 to mark as not processed
- }
- calculateTimes(n, bt, at, wt, tat);
- printResults(n, wt, tat);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment