Friday, June 8, 2012

First Come First Serve Algorithm using C++ code

Sample Output
// this program code is a First come First served Algorithm.// created by: jieng46

#include<iostream>
#include<stdio.h>
#include<conio.h>

using namespace std;
int main()
{
int jd_n,bu[20];
float twt=0.f,awt=0.f,wt[20],tat[20];
int i;

//Getting no of processes and Burst time
cout <<"First come First served:";
cout <<"\nEnter the no of processes:";
cin>>jd_n;
for(i=1;i<=jd_n;i++)
{
cout << "Enter value for process "<<i<<"=";
cin>>bu[i];
}
//First come First served
int b[10];
twt=0.0;

for(i=1;i<=jd_n;i++) //looping for waiting time
{
b[i]=bu[i];
}

wt[1]=0;
for(i=2;i<=jd_n;i++)
{
wt[i]=b[i-1]+wt[i-1];
cout<<"\nWaiting time p"<<i<<"="; // display the value of waiting time
cout<<wt[i];
}
for(i=1;i<=jd_n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
}
awt=twt/jd_n; //compute the averagre of waiting time
cout<<"\nTotal Waiting Time="<<twt;
cout<<"\nAverage Waiting Time="<<awt;

getche();
return 0;
}
//end....
//