Facebook
From Perl Agouti, 1 Year ago, written in C++.
This paste is a reply to Untitled from Diminutive Baboon - go back
Embed
Viewing differences between Untitled and Re: Untitled
#include 
using namespace std;

struct process {
    int pid;
    int burst_time;
    int start_time;
    int completion_time;
    int turnaround_time;
    int waiting_time;
};

bool compareBurst(process p1, process p2)
{
    return p1.burst_time < p2.burst_time;
}

bool compareID(process p1, process p2)
{
    return p1.pid < p2.pid;
}

int main() {

    int n;
    struct process p[100];
    float avg_turnaround_time;
    float avg_waiting_time;
    int total_turnaround_time = 0;
    int total_waiting_time = 0;

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

    for(int i = 0; i < n; i++) {
        cout<<"Enter burst time of process "<         cin>>p[i].burst_time;
        p[i].pid = i+1;
    }

    sort(p,p+n,compareBurst);

    for(int i = 0; i < n; i++) {

        p[i].start_time = (i == 0)? 0 :p[i-1].turnaround_time;
        p[i].turnaround_time = p[i].start_time + p[i].burst_time;
        p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;

        total_turnaround_time += p[i].turnaround_time;
        total_waiting_time += p[i].waiting_time;
    }

    avg_turnaround_time = (float) total_turnaround_time / n;
    avg_waiting_time = (float) total_waiting_time / n;

    sort(p,p+n,compareID);

    cout<     cout<<"#P\t"<<"BT\t"<<"ST\t"<<"TAT\t"<<"WT\t"<<"\n"<
    for(int i = 0; i < n; i++) {
        cout<     }
    cout<<"Average Turnaround Time = "<     cout<<"Average Waiting Time = "<

}