0

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;
}
2
Contributors
3
Replies
13
Views
1 Year
Discussion Span
Last Post by tinstaafl
0

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();
}

Edited by tinstaafl

0

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

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.