al__nasim

srtn1.cpp

Jan 14th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int ar[100],wt[100],p[100],i,j,k,l,pp,bu[100],n,realburst[100],small,totbu=0,totwt=0,minn;
  4. int main()
  5. {
  6.     cout<<"Enter number of process" <<endl;
  7.     cin >>n;
  8.      cout<<" Enter arrival time and burst time" <<endl;
  9.     for(i=1;i<=n;i++)
  10.     {
  11.         cin >> ar[i] >> bu[i] ;
  12.         p[i]=i;
  13.         totbu+=bu[i];
  14.  
  15.     }
  16.     // if arrival time is not sorted
  17.  
  18.     for(i=1;i<=n;i++)
  19.     {
  20.  
  21.         for(j=i+1;j<=n;j++)
  22.         {
  23.             if(ar[i]>ar[j])
  24.             {
  25.                 swap(ar[i],ar[j]);
  26.                 swap(bu[i],bu[j]);
  27.                 swap(p[i],p[j]);
  28.  
  29.             }
  30.  
  31.         }
  32.     }
  33.     for(i=1;i<=n;i++)
  34.     {
  35.  
  36.         realburst[i]=bu[i];
  37.     }
  38.  
  39.  
  40.     int t=0;
  41.     i=1;
  42.     while(t<=totbu)
  43.     {
  44.           if(ar[i]<=t && i<=n)
  45.             {
  46.                 k=i;
  47.                 i++;
  48.  
  49.             }
  50.             minn=99999999;
  51.            // cout<<"k  "<<k<<endl;
  52.             for(j=1;j<=k;j++)
  53.             {
  54.                  if(bu[j]<minn&&bu[j]!=0)
  55.                  {
  56.  
  57.                      minn=bu[j];
  58.                      small=j;
  59.                  }
  60.             }
  61.            // cout <<"small  " <<small <<endl;
  62.             bu[small]--;
  63.             t++;
  64.             if(bu[small]==0)
  65.             {
  66.                 wt[small]= t-ar[small]- realburst[small];
  67.  
  68.             }
  69.  
  70.  
  71.  
  72.  
  73.     }
  74.  
  75.    for(i=1;i<=n;i++)
  76.    {
  77.  
  78.        cout <<p[i] <<"   " <<wt[i] <<endl;
  79.        totwt+=wt[i];
  80.  
  81.    }
  82.    double avgwt=0;
  83.    avgwt= totwt/(n*1.0);
  84.    cout <<"avg waiting time   "<<avgwt <<endl;
  85.  
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment