You are creating an array of struct process that always has 10 items but you are only using the first 2. Instead if indexing against maxprocesses maybe use
OK thanks but how to show the first arrival time with its process number and its arrival time? and then show the rest process depanding on shortest burst?
Change your last two for loops from maxprocesses to numberOfProcesses. You don't define the other 8 processes so they're going to give you undefined results.
#include<iostream>
usingnamespace 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;
constint maxprocesses = 10;
struct process p[maxprocesses];
//int smaller[numberOfProcesses];
// initialize variables
int smallest_burst = p[0].burstTime;
int smallest_burst_index = 0;
int smallest_arrivalTime = p[0].arrivalTime;
int smallest_arrivalTime_index = 0;
// 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;
}
// after computing
for(int i = 1; i < numberOfProcesses; ++i)
{
if(p[i].burstTime < smallest_burst)
{
smallest_burst = p[i].burstTime;
smallest_burst_index = i;
}
if(p[i].arrivalTime < smallest_arrivalTime)
{
smallest_arrivalTime = p[i].arrivalTime;
smallest_arrivalTime_index = i;
}
}
//display
cout << endl;
cout<<"Process" << "\t" << "Burst" << "\t" << "Arrival" << endl;
for(int i=0; i<numberOfProcesses; i++)
{
cout <<"P" << p[smallest_arrivalTime_index].processId << "\t" << p[smallest_arrivalTime_index].burstTime << "\t" << p[smallest_arrivalTime_index].arrivalTime << endl;
cout <<"P" << p[smallest_burst_index].processId << "\t" << p[smallest_burst_index].burstTime << "\t" << p[smallest_burst_index].arrivalTime << endl;
// here I need to print smaller burst time
}
return 0;
}
I have problem with printing. I want first to print the first arrival and then the lowest burst time. then I want to print the rest of process depending on lower burst time.
ex:
process burst time arrival time
p0 5 5
p1 3 1
p2 4 0
p3 9 2
result should be, p2(because first arrived), p1,p0 and p3
What you should do is set up a sort function that accepts an array of your structure. Sort the array based on any passed criteria (burst time, arrival time, process number) and then display that. You can also display the quickest arriving process/fastest burst time in your program using what you already have. Otherwise, you'll have to use a sort algorithm based on positioning just the first arrived process first, then the fastest burst time second, and sort the last ones. IMHO, that's greatly over complicating your program than it needs to be.