My code is calculating the turnaround time and arrival time wrong (Round Robin)

39 Views Asked by At

My task is to calculate the average turnaround time and waiting time, as well as the number of interruptions froma CSV file (1st row being the arrival times, the the rest of the row are summed up to be the burst time for each job)

// Implement Round Robin Scheduling
        queue<int> jobQueue;
        int remainingBurstTimes[MAX_COLS];

        for (int i = 0; i < colCount; ++i) {
            remainingBurstTimes[i] = burstTimes[i];
            jobQueue.push(i);
        }

        int currentTime = 0;

        while (!jobQueue.empty()) {
            int currentJob = jobQueue.front();
            jobQueue.pop();
        
            int executeTime = std::min(quantum, remainingBurstTimes[currentJob]);
            currentTime += executeTime;
            remainingBurstTimes[currentJob] -= executeTime;
        
            if (remainingBurstTimes[currentJob] > 0) {
                // Job needs more time, put it back in the queue
                jobQueue.push(currentJob);
                interrupts++;
            } else {
                // Job is completed
                turnaroundTimes[currentJob] = currentTime - arrivalTimes[currentJob];
                waitingTimes[currentJob] = turnaroundTimes[currentJob] - burstTimes[currentJob];
            }
        }

        double totalTurnaroundTime = 0;
        double totalWaitingTime = 0;

        for (int i = 0; i < colCount; ++i) {
            totalTurnaroundTime += turnaroundTimes[i]; //calc sum of turnaround time
            totalWaitingTime += waitingTimes[i]; //calc sum of waiting time
        }
        
        //calculate average turnaround & waiting time
        double averageTurnaroundTime = totalTurnaroundTime / colCount;
        double averageWaitingTime = totalWaitingTime / colCount;

I checked and it seems to be calculating the finish time wrong too. It should be able to show the completion time, turnaround time, and waiting time correctly.

0

There are 0 best solutions below