0

I have created this program, and it works fine for random values .
But when I ever enter same Arrival time it gives error output !
.i.e arrivaltime bursttime
p1 0 10
p2 0 6
now the problem is that instead of p2 running first p1 starts...
plz help.
Process.h

#include <iostream>
using namespace std;
class Process
{
private:
	int bstTime,wtTime,arrTime;
	int procNo; int procTime;
	int trArTime;int startTime; int endTime;
public:
	Process()
	{
		bstTime=wtTime=arrTime=trArTime=0;
		procTime=0;
	}
	Process(int bT, int arT)
	{
		bstTime=bT;
		arrTime=arT;
	}
	/////////////////////////////////////////
	void setBstTime(int bTime)
	{
		bstTime=bTime;
	}
	///////////////////////////////////////
	void setArrTime(int arTime)
	{
		arrTime=arTime;
	}
////////////////////////////////////////
	 void getInput(int i)
	 {
		 cout<<"\t\tEnter the Burst Time of the   P"<<i<<" : ";;cin>>bstTime;
		 procTime=bstTime;
		 cout<<"\t\tEnter the Arrival Time of the P"<<i<<" : ";cin>>arrTime;
		 procNo=i;
		
	 }
	 ////////////////////////////////////
	 int getBstTime()
	 {return bstTime;}
	 //////////////////////////////////
	 int getWtTime()
	 {
		 return wtTime;
	 }
	 ////////////////////////////////////
	 int getArrTime()
	 {return arrTime;}
	 /////////////////////////////////////
	 int getProcID()
	 {return procNo;}
	 ///////////////////////////
	 //Decrease Remaining time
	 void decProcTime()
	 {
		 if(procTime>0)
		 procTime--;
	 }
	 ///////////////////get time to be processed 
	 int getProcTime()
	 {return procTime;}
	 //////////////////////////////////////////
	 void setStartingTime(int t)
	 {startTime=t;}
	 //////////////////////////////////////////
	 void setEndingTime(int t)
	 {endTime=t;}
	 //////////////////////////////////////////
	 int getStartTime()
	 {return startTime;}
	 //////////////////////////////////////////
	 int getEndingTime()
	 {return endTime;}
	 //////////////////////////////////////////
	void calcWaitingTime()
	 {
		 wtTime=(endTime-bstTime-arrTime);
		 trArTime=wtTime + bstTime;
		
	 }
	 //////////////////////////////////Get Turn around Time

	 int getTurnAroundTime()
	 {
		 return trArTime;
	 }
	 /////////////////////////////////////////////////
	 int getWaitingTime()
	 {return wtTime;}


};

and .cpp file

#include "process.h"
#include <sstream>
#include <string>

stringstream st,st1,st2;

void sortAgain(Process pro[], int no, int t)		//Sorting the processes
{
for(int i=0;i<no;i++)
{
	for(int j=0;j<no;j++)
		if((pro[j].getProcTime()>pro[j+1].getProcTime())&&(pro[j+1].getArrTime()==t)&& (pro[j].getArrTime()<t))
			
		{
			st<<char(179);
			st1<<char(179);
			st2<<t;
			Process temp=pro[j];
			pro[j]=pro[j+1];
			pro[j+1]=temp;
		}
}
}
void sortByID(Process pro[],int no)
{
	for(int i=0;i<no;i++)			//Sorting by process ID
	{
		for(int j=0;j<no-i;j++)
		{
			if(pro[j].getProcID()>pro[j+1].getProcID())
			{
				Process temp=pro[j];
			      pro[j]=pro[j+1];
			       pro[j+1]=temp;
			}
		}
	}
}
void drawLine(int n)
{
	for(int i=0;i<n;i++)	//Draw Line
		cout<<"-";
}
int main()
{st2<<"0";
//	char ch='219';
	int totalTime=0;
	double avgWaitTime;
	Process *pros=NULL;
	int noPros;
	cout<<"\n\tEnter the Number of Processes ";cin>>noPros;
	/////////*Create a dynamic array of Processes using new operator*/
	pros=new Process[noPros];
	/*Take input from user*/
	for(int k=0;k<noPros;k++)
	{
		cout<<"\n\t\tEnter the data for the P"<<k+1<<endl<<endl;
		pros[k].getInput(k+1);
	}
	
	cout<<"\n\n\tProcesses\t\tArrival Time\t\tCPU Burst Time\n";
	drawLine(80);
	cout<<endl;
	for(int j=0;j<noPros;j++)
	{
		cout<<"\t P"<<pros[j].getProcID()<<"\t\t\t    "<<pros[j].getArrTime()<<"\t\t\t    "<<pros[j].getBstTime()<<endl;
	}
///Calculate total Burst CPU Time of all the processes
	for(int l=0;l<noPros;l++)
		totalTime+=pros[l].getBstTime();
	/*Now start calculating waiting time */
	
	int time=pros[0].getArrTime();
	int done=0;
	pros[0].setStartingTime(time);
	while(time<totalTime)
	{
		while(done<noPros)
		{
			
			if(pros[done].getProcTime()==0)
			{
				pros[done].setEndingTime(time);
				pros[done+1].setStartingTime(time);
				done++;
				st1<<char(179);
				st<<char(179);
				st2<<time;
				
			}
			else
			{	
				st<<"P"<<pros[done].getProcID();
				pros[done].decProcTime();
				st1<<char(219)<<char(219);
				time++;
				sortAgain(pros, noPros, time);
				
			}
			
			
		}
		
	}
	sortByID(pros,noPros);
	
		cout<<"\n\n\tProcesses\tArrival Time\tCPU Burst Time\tWaitingTime\t\n";
	drawLine(80);
	cout<<endl;
	for(int m=0;m<noPros;m++)
		pros[m].calcWaitingTime();
	for(int n=0;n<noPros;n++)
	{
		cout<<"\t P"<<pros[n].getProcID()<<"\t\t    "<<pros[n].getArrTime()<<"\t\t    "<<pros[n].getBstTime()<<"\t\t    "<<pros[n].getWaitingTime()<<"\t\t    "<<endl;
	}
	string str=st.str();
	string str2=st1.str();
	string str3=st2.str();
	
	
	/////////////////////////Average Calculations/////////////////////////
	avgWaitTime=0.0;
	for(int o=0;o<noPros;o++)
	avgWaitTime+=pros[o].getWaitingTime(); ////////////////calculate avg waiting time
	avgWaitTime/=noPros;
	cout<<"\n\tAverage Waiting Time = "<<avgWaitTime<<endl<<endl;
	cout<<"\t::Gantt Chart ::- \n\n";

cout<<"\t";
cout<<str<<endl;
cout<<"\t";
cout<<str2<<endl;
cout<<"\t";

	
	cout<<endl<<endl;
	system("pause");

return 0;
}
2
Contributors
1
Reply
5
Views
7 Years
Discussion Span
Last Post by kashifhameed88
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.