select first process arrived, sjf in scheduling process

Hello,

i'm writing a code for non-preemptive shortest job first.

here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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;
      }


but i'm getting this result.

http://3rbc.net/up/uploads/13451406381.jpg

any help? I think i'm very close

thank you
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
 
for(int i = 0; i < numberOfProcesses; i++)

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?
still waiting :)
and still waiting :)
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.
Ok here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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];

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

1
2
     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;


this prints same data

how to solve?
any help please?
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.
I don't know how to do that :(
any help?
Topic archived. No new replies allowed.