First Come First Serve (FCFS) Scheduling

#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<pair<int,int> >v;
    int x[100],wt[100];
    //freopen("sjfp.txt","r",stdin);
    while(1)
    {
    int i,j,n,a,b,sum=0;
    cin>>n;
    if(n==0)
    {
        break;
    }
    for(i=0;i<n;i++)
    {
        cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    x[0]=0;
    for(i=1;i<=n;i++)
    {
        x[i]=x[i-1]+v[i-1].second;
        wt[i]=x[i-1];
        sum=sum+wt[i];

    }
    cout<<"\nGrantt Chart\n"<<endl;
    for(i=1;i<=n;i++)
    {
        cout<<"P"<<i<<"="<<x[i]<<"---> ";
    }
    cout<<endl<<endl;

    cout<<"P\t"<<"BT\t"<<"WT"<<endl;
    for(i=1;i<=n;i++)
    {
        cout<<i<<"\t"<<v[i-1].second<<"\t"<<wt[i]<<endl;
    }

    cout<<"Average Waiting Time"<<(double)sum/n<<" ms"<<endl;
    }
    return 0;

}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment