this code is correct expect that part which calculate the waiting time .. it should be 0 in first process but in output it equals the burst time of 1st process. how can i fix it ?

``````#include <iostream>
#include<iomanip>
#include<queue>
using namespace std;
int main(){

queue<int> sc;
float tat = 0, total_tat = 0, tw=0, total_tw = 0; //tat: turnaround time , tw: waiting time
int n = 0,i,j, bt = 0;//bt: burst time -time to start-
cout << "enter number of processes : " << endl;
cin >> n;
cout << "\n enter process working time :"<<endl;
for (i = 0; i < n; i++){
cout << "p[" << i+1 << "]:";
cin >> bt;
sc.push(bt);
}
cout << "\n process "<<setw(3)<<" burst time"<<setw(15)<<"waiting time"<<setw(19)<<"turnaround time";
for (j = 0; j < n; j++){
bt = sc.front();
tw = tw + bt; //here is the problem
tat = bt + tw;
total_tw = total_tw + tw;
total_tat = total_tat + tat;

cout << "\n p[" << j +1 << "]" << setw(7) << bt << setw(17) << tw<< setw(20) << tat;
sc.pop();
}
cout << "\n average waiting time : " << total_tw / n << endl;
cout << "average turnaround time : " << total_tat / n << endl;
return 0;
}
``````

## All 3 Replies

It looks to me the problem is you're adding the burst time to the waiting time on the first iteration. You probably need to save the previous burst time then add it to the wait time. Something like this:

``````int pbt = 0;
for (j = 0; j < n; j++)
{
bt = sc.front();
tw = tw + pbt;
tat = bt + tw;
total_tw = total_tw + tw;
total_tat = total_tat + tat;
cout << "\n p[" << j +1 << "]" << setw(7) << bt << setw(17) << tw<< setw(20) << tat;
pbt = bt;
sc.pop();
}
``````

ahaa .. then it will start counting from 0 .. i got it thank you ^^