I'm fresh on these scheduling algorithms. I've become comfortable with SJF non-preemptive and I understand it from a pen and paper Gantt chart perspective but not quite so from a programming perspective. My code is below and though it runs successfully my math is incorrect. How can I fix it?
TEST CASE Process Burst Arrival P1 20 0 P2 3 3 P3 2 5
Expected Outputs Avg wait of 11.3 Avg turnaround time of 19.6 Avg response time of 10.6
Actual outputs avg wait of 8.3 avg turnaround time of 6.6 avg response time of 14
#include <iostream>
using namespace std;
void SJF_NP(int n, int burst[], int arrival[], int throughput)
{
cout << "Output for SJF_Non_Preemptive scheduling algorithm" << endl;
int i, j, temp, temp2;
double tot, avgwait, avgturnaround, avgresponse, tp;
//array instantiations
int start[n], end[n], wait[n];
//calculations
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if (i>=2 && burst[i-1]>burst[j-1])
{
temp = burst[i-1];
temp2 = arrival[i-1];
burst[i-1]=burst[j-1];
arrival[i-1]=arrival[j-1];
burst[j-1]=temp;
arrival[j-1]=temp2;
}
}
if(i==1)
{
start[0]=0;
end[0]=burst[0];
wait[0]=0;
}
else
{
start[i-1]=end[i-2];
end[i-1]=start[i-1]+burst[i-1];
wait[i-1]=start[i-1]+arrival[i-1];
}
//throughput
if (start[i+1] <= throughput)
tp = i+1;
}
//output
cout << "\n\nPROCESS \t BURST TIME\tARRIVAL TIME\tWAIT TIME\tSTART TIME\tEND TIME\n";
for (i=0;i<n;i++){
cout << "\nP[" << i + 1 << "]" << "\t\t" << burst[i] << "\t\t" << arrival[i] << "\t\t" << wait[i] << "\t\t" << start[i] << "\t\t" << end[i];
}
//avg wait time
for(i=1,tot=0;i<n;i++){
tot+=wait[i-1];
avgwait=tot/n;
}
//avg turnaround time
for(i=1,tot=0;i<n;i++){
tot+=end[i-1];
avgturnaround=tot/n;
}
//avg response time
for(i=1,tot=0;i<n;i++){
tot+=start[i-1];
avgresponse=tot/n;
}
cout << "\n\nAverage Wait Time: " << avgwait;
cout << "\nAverage Response Time: " << avgturnaround;
cout << "\nAverage Turnaround Time: " << avgresponse;
cout << "\nThroughput for (" << throughput << "): " << tp << endl;
}