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

Dani AI

Generated

The first process shows a nonzero waiting time because the code updates the cumulative wait before recording the current process's waiting time. Waiting time in FCFS = sum of burst times of all earlier processes, so the very first process must use 0. was right to suggest saving previous work, but a clearer pattern is to keep one running "elapsed" time and use it as the waiting time for each process, then add the current burst.

A concise pattern you can apply (uses a different variable pattern than earlier examples):

int elapsed = 0;          // time elapsed before current process
int total_wait = 0;
int total_tat = 0;

while (!sc.empty()) {
    int bt = sc.front();
    int wait = elapsed;   // waiting time for this process (0 for first)
    int tat  = wait + bt;
    total_wait += wait;
    total_tat  += tat;
    // output bt, wait, tat here
    elapsed += bt;        // advance elapsed after using it
    sc.pop();
}

double avg_wait = (double)total_wait / n;
double avg_tat  = (double)total_tat  / n;

Notes: use integer types for times and double for averages to avoid integer division. If you need to keep process IDs or arrival times, use a vector of pairs or a queue of structs instead of a plain queue<int>. This approach directly addresses the off-by-one accumulation that caused the first waiting time to equal its burst.

Recommended Answers

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 ^^

If your question is answered, please remember to mark this solved. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.