Click here to Skip to main content
15,851,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want code in c++ of cpu scheduling where order of arrival time can be in ascending descending or in mix order

What I have tried:

i have tried a code that is only providing me the correct answer when the arrival time is in ascending order when i change it it is not working properly i mean when i enter the arrival just randomly without any order
here is my code
#include <iostream>
using namespace std;

int main()
{
    int bt[20], wt[20], tat[20], ct[20], at[20], i, n;
    float wtavg = 0, tatavg = 0;

    cout << "Enter the number of processes: ";
    cin >> n;

    for (i = 0; i < n; i++)
    {
        cout << "Enter Arrival Time for Process " << i << ": ";
        cin >> at[i];

        cout << "Enter Burst Time for Process " << i << ": ";
        cin >> bt[i];
    }

    ct[0] = at[0] + bt[0];
    tat[0] = ct[0] - at[0];
    wt[0] = tat[0] - bt[0];
    wtavg += wt[0];
    tatavg += tat[0];

    for (i = 1; i < n; i++)
    {
        // Calculate the completion time based on the arrival time and previous completion time
        if (at[i] > ct[i - 1])
            ct[i] = at[i] + bt[i];
        else
            ct[i] = ct[i - 1] + bt[i];

        tat[i] = ct[i] - at[i];
        wt[i] = tat[i] - bt[i];

        wtavg += wt[i];
        tatavg += tat[i];
    }

    cout << "\nPROCESS \tARRIVAL TIME \tBURST TIME \tCOMPLETION TIME \tWAITING TIME \tTURNAROUND TIME\n";
    for (i = 0; i < n; i++)
    {
        cout << "\n\t P" << i << "\t\t " << at[i] << "\t\t " << bt[i] << "\t\t " << ct[i] << "\t\t " << wt[i] << "\t\t " << tat[i];
    }

    cout << "\nAverage Waiting Time -- " << wtavg / n;
    cout << "\nAverage Turnaround Time -- " << tatavg / n;

    return 0;
}
Posted
Updated 7-Jun-23 5:14am
Comments
Rick York 7-Jun-23 11:05am    
Of what significance is the value 20? You have five integer arrays of that size. It is bad practice to use hard-coded values like that.

1 solution

Quote:
i have tried a code that is only providing me the correct answer when the arrival time is in ascending order when i change it it is not working properly
So sort the arrival times before you start processing them.
 
Share this answer
 
Comments
OriginalGriff 8-Jun-23 0:44am    
I mean that if you have code that relies on an ordered input, then making sure it's ordered before you process it seems like a good idea ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900