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.

Recommended Answers

All 15 Replies

What exactly do you want that if statement to do?

ok, I want first to select the first process according to the first arrived time, in other words, the smallest arrival time must be seleced, then I want select the rest according to the smallest burst time.

here is my code now.

#include<iostream>
using namespace std;

struct process{
    int processId;
    int burstTime;
    int arrivalTime;
    // int arrivedFirst;
};

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


      //sort
      for(int i=0; i<numberOfProcesses; i++){
          for(int j=0; j<numberOfProcesses; j++){
            for(int arrival=1; arrival<numberOfProcesses; arrival++){
            p[i].arrivalTime = p[0].arrivalTime;
                if(p[arrival].arrivalTime<p[j].arrivalTime){
                    p[0].arrivalTime = p[arrival].arrivalTime;

                }
            }

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

Its not necessary to sort the array because you can sort on only one of the structure members. Just delete all that code from lines 39 to 66, its wrong anyway.

Instead of sorting, just loop through the array and find the indices with the smallest value. Do you know how to do that with a simple integer array? If yes, then this should be a piece of cake for you because its the same idea, except that you need to find two values, not just one. You will need 2 integers, one to hold the index of where the smallest burst time was found, and the other to find the index of where the smallest time was found. You can find both values in the same loop.

Ancient Dragon could you please explain more in codes? thanks

int Smallest(int Array[], int size)
{
    if (size == 1)                  
        return Array[0];

    int A = Array[0];                  
    for (int I = 0; I < size; I++)     
        A = Array[I] < A ? Array[I] : A;

    return A;
}


int Smallest(std::vector<int> Array)
{
    if (Array.size() == 1)
        return Array[0];

    int A = Array[0];
    for (int I = 0; I < Array.size(); I++)
        A = Array[I] < A ? Array[I] : A;

    return A;
}
struct process{
    int processId;
    int burstTime;
    int arrivalTime;
};
const int maxprocesses = 10;
struct process p[maxprocesses];

...
...
// initialize variables
int smallest_burst = p[0].burstTime;
int smallest_burst_index = 0;
int smallest_arrivalTime = p[0].arrivalTime;
int smallest_arrivalTime_index = 0;

for(int i = 1; i < maxprocesses; ++i)
{
   if( p[i].burstTime < smallest_burst)
   {
       smallest_burst = p[i].burstTime;
       smallest_burst_index = i;

   }
   // now do the same test for arrivalTime
}

please see my code now

#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;
    const int maxprocesses = 10;
    struct process p[maxprocesses];



    // 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 < maxprocesses; ++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++){
      // here i want to print the smallest arrivaltime first,
      // as you see i write like below but doen't work. 

          if(smallest_arrivalTime)
          {
            cout <<"P" << smallest_arrivalTime_index << "\t" << p[i].burstTime << "\t" << smallest_arrivalTime <<endl;

          }
            cout <<"P" << p[i].processId << "\t" << p[i].burstTime << "\t" << p[i].arrivalTime <<endl;
      }

      return 0;
      }

please see line 69

i'm still waiting for a help :)

display it like thiks example: smallest arrival time is
p[smallest_arrivalTime_index].arrivalTime;

Also -- delete the loop on line 66 and if statement on line 70, there is no reason for them. All you want left there is line 72 and line 75.

Ancient Dragon I want first to select the first process that arrive ((lowest arrival must be selected))

after that, i must select process based on shorter burst time. that's it

becuase i think you didn't understand my question very well :)

If you want to show all processes in ascending order by burst time then sort them by burst time. No need to sort by arrival time, just get it as previously posted.

cout << "Sortest Arrival Time: " << p[smallest_arrivalTime_index].arrivalTime << '\n';
cout << "Burst Times: \n";
for(int i = 0; i < maxprocesses; ++i)
{
   if( i != smallest_arrivalTime_index )
       cout << p[i].burstTime << '\n';///

}

please see the result.

this is all the 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;
    const int maxprocesses = 10;
    struct process p[maxprocesses];



    // 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 < maxprocesses; ++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;
      cout << "Sortest Arrival Time: " << p[smallest_arrivalTime_index].arrivalTime << '\n';
        cout << "Burst Times: \n";
        for(int i = 0; i < maxprocesses; ++i)
        {
           if( i != smallest_arrivalTime_index )
               cout << p[i].burstTime << '\n';///

        }



      return 0;
      }

You are very close -- only five problems marked with // <<<< in the code below

#include<iostream>
using namespace std;

struct process{
    int processId;
    int burstTime;
    int arrivalTime;
};

int main(){
    int numberOfProcesses;
    const int maxprocesses = 10;        //<<<<<<< these two lines were declared in the wrong place
    struct process p[maxprocesses];     //<<<<<<< ^^^^

     //number of process
    cout <<"please enter the number of process: ";
    cin >> numberOfProcesses;



    // initialize variables
    int smallest_burst = p[0].burstTime;
    int smallest_burst_index = 0;
    int smallest_arrivalTime = 0;
    int smallest_arrivalTime_index = 0; // <<<< Can not be initialized here



     // 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;
      }
    smallest_arrivalTime = p[0].arrivalTime; // <<<<<< Initialize variable

       //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;
      cout << "Sortest Arrival Time: " << p[smallest_arrivalTime_index].arrivalTime << '\n';
        cout << "Burst Times: \n";
        for(int i = 0; i < numberOfProcesses; ++i) // <<<<<<<<<<<<<
        {
           if( i != smallest_arrivalTime_index )
               cout << p[i].burstTime << '\n';///

        }



      return 0;
      }

i'm sorry i'm late. I really don't know how to correct the code. can you do it for me?

becuase I think the mistakes are so easy :)

thank you

please any one can help?

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.