Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.5 kB
2
Indexable
Never
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include <utility>
using namespace std;

int main()
{
int n=7;
int ct[n],tat[n],wt[n];
    int at[7]={0,2,1,4,6,5,7};
    int bt[7]={3,5,4,2,9,4,10};
    int pri[7]={2,6,3,5,7,4,10};
    vector <pair<int,pair<int,int>>> 	v;
    
    for(int i=0;i<n;i++)
    {
    v.push_back({pri[i],{at[i],bt[i]}});
    }
    
    cout<<endl;
    sort(v.begin(),v.end());
  cout<<"Non-Preemptive\n";
    int sum=0;
    for(const auto& element :v)
    {
    sum+=element.second.second;
    ct[find(at,at+n,element.second.first)-at]=sum;
    printf("P%ld ",(find(at,at+n,element.second.first)-at)+1);
    }
 cout<<endl;
 cout<<"PID"<<"\t"<<"PRI"<<"\t"<<"AT"<<"\t"<<"BT"<<"\t"<<"CT"<<"\t"<<"TAT"<<"\t"<<"WT"<<endl;
 for(int i=0;i<n;i++)
 {
     tat[i]=ct[i]-at[i];
     wt[i]=tat[i]-bt[i];
    printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i+1,pri[i],at[i],bt[i],ct[i],tat[i],wt[i]);
 }

 float avgtat=0,avgwt=0;
 for(int i=0;i<n;i++)
 {
     avgtat+=tat[i];
     avgwt+=wt[i];
 }
 avgtat/=n;
 avgwt/=n;
 cout<<"Average Turnaround time "<<avgtat<<"\nAverage Wating time "<<avgwt<<endl;
    cout<<endl;
 
 cout<<"Preemptive:\n";
 int tat1[n],wt1[n],sum1=0;
 int max=numeric_limits<int>::min();
 for(const auto& ele: v)
 {
 int second = ele.second.first;
 if(second>max)
 max=second;
 }
int ct1[7]={0,0,0,0,0,0,0};
  for(int i=0;i<7;i++)
    {
    while(v[i].second.second!=0)
    {
    if(v[i].second.first==0)
    {sum1+=1;
    v[i].second.second--;
    ct1[find(at,at+n,v[i].second.first)-at]+=sum1;
    printf("P%ld ",(find(at,at+n,v[i].second.first)-at)+1);
    }
    else
    {
    if(sum1<max)
    {
    sum1+=1;
    v[i].second.second--;
    ct1[find(at,at+n,v[i].second.first)-at]+=sum1;
    printf("P%ld ",(find(at,at+n,v[i].second.first)-at)+1);
    }
    else if(sum1>=max)
    {
    sum1+= v[i].second.second;
    v[i].second.second=0;
    ct1[find(at,at+n,v[i].second.first)-at]+=sum1;
    printf("P%ld ",(find(at,at+n,v[i].second.first)-at)+1);
    }
    }}}
 cout<<endl;
 cout<<"PID"<<"\t"<<"PRI"<<"\t"<<"AT"<<"\t"<<"BT"<<"\t"<<"CT"<<"\t"<<"TAT"<<"\t"<<"WT"<<endl;
 for(int i=0;i<n;i++)
 {
     tat1[i]=ct1[i]-at[i];
     wt1[i]=tat1[i]-bt[i];
    printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n",i+1,pri[i],at[i],bt[i],ct1[i],tat1[i],wt1[i]);
 }

 float avgtat1=0,avgwt1=0;
 for(int i=0;i<n;i++)
 {
     avgtat1+=tat1[i];
     avgwt1+=wt1[i];
 }
 avgtat1/=n;
 avgwt1/=n;
 cout<<"Average Turnaround time "<<avgtat1<<"\nAverage Wating time "<<avgwt1<<endl;
    cout<<endl;
 
    return 0;
    
}