hello,
i'm writing a code for non-preemptive shortest job first, everything is okay except to select the first process arrived in the queue. here is my code.
#include<iostream>
using namespace std;
struct process{
int processId;
int burstTime;
int arrivalTime;
};
int main(){
int numberOfProcesses;
//number of process
cout <<"please enter the number of process: ";
cin >> numberOfProcesses;
struct process p[numberOfProcesses];
struct process temp;
// get burst time , arrival time
for(int i=0; i<numberOfProcesses; i++){
p[i].processId = i;
cout << "p" << i << ":" << endl;
cout <<"Burst time: ";
cin >> p[i].burstTime;
cout <<"Arrival Time: ";
cin >> p[i].arrivalTime;
}
//display user input
cout<<"Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
for(int i=0;i<numberOfProcesses;i++){
cout <<"P" << p[i].processId << "\t" << p[i].burstTime << "\t" << p[i].arrivalTime <<endl;
}
int arrivedFirst;
//sort
for(int i=0; i<numberOfProcesses; i++){
for(int j=0; j<numberOfProcesses; j++){
for(int arrival=1; arrival<numberOfProcesses; arrival++){
arrivedFirst = p[0].arrivalTime;
if(p[arrival].arrivalTime<arrivedFirst){
arrivedFirst = p[arrival].arrivalTime;
}
}
// select the arrived proccess first
if(arrivedFirst){
}
// select the smallest burst time
if(p[j].burstTime>p[i].burstTime){
temp = p[i];
p[i] = p[j] ;
p[j] = temp;
}
// if same value with burst time, select the arrived first
if(p[j].burstTime==p[i].burstTime){
if(p[j].arrivalTime>p[i].arrivalTime){
temp = p[i];
p[i] = p[j] ;
p[j] = temp;
}
}
}
}
// after computing
//display
cout << endl;
cout<<"Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
for(int i=0;i<numberOfProcesses;i++){
cout <<"P" << p[i].processId << "\t" << p[i].burstTime << "\t" << p[i].arrivalTime <<endl;
}
return 0;
}
the missing code is in line 48.