Question

(C++) Given an array with Job_id, Arrival_Time & Duration: Array[5][3] 0 0 100 1 20 80...

(C++)
Given an array with Job_id, Arrival_Time & Duration:

Array[5][3]
0 0 100
1 20 80
2 30 60
3 40 50
4 50 40
Use simple array manipulation in C++ to implement "Shortest Job First" to show arrival start time, finish time and response time for each job.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

// main function definition
int main()
{
// Matrix contains first column process ID, second column arrival time third column duration
int Array[5][3] = {{0, 0, 100}, {1, 20, 80}, {2, 30, 60}, {3, 40, 50}, {4, 50, 40}};

// Declares array to store process process ID, burst time arrival time
// waiting time and turn around time
int processID[5], burstTime[5], arrivalTime[5], waitingTime[5], turnAroundTime[5];
// Variables to store total and average waiting time and turn around time
float avgWaitingTime = 0, avgTurnAroundTime = 0, turnAroundTotal = 0, waitingTotal = 0;
// Temporary variables
int temp, minimum, k = 1, executionTime = 0, turnAround = 0, total = 0;


printf(" ******* SJF Scheduling (Non Preemptive Scheduling) ******** \n");
int numberOfProcess = 5;

// Loops till number of process
for(int r = 0; r < numberOfProcess; r++)
{
// Extracts matrix current row position and 0 column index position data
// and stores it in process ID array
processID[r] = Array[r][0];
// Extracts matrix current row position and 1 column index position data
// and stores it in array time array
arrivalTime[r] = Array[r][1];
// Extracts matrix current row position and 2 column index position data
// and stores it in burst time array
burstTime[r] = Array[r][2];
}// End of for loop

// Sorting process ID, arrival time and burst time According to Arrival Time
// Loops till number of process
for(int r = 0; r < numberOfProcess; r++)
{
// Loops till number of process
for(int c = 0; c < numberOfProcess; c++)
{
// Checks if r index position arrival time is less than
// c index position arrival time
if(arrivalTime[r] < arrivalTime[c])
{
// Swapping process for process ID
temp = processID[c];
processID[c] = processID[r];
processID[r] = temp;

// Swapping process for arrival time
temp = arrivalTime[c];
arrivalTime[c] = arrivalTime[r];
arrivalTime[r] = temp;

// Swapping process for burst time
temp = burstTime[c];
burstTime[c] = burstTime[r];
burstTime[r] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop

// Arranging the table according to Burst time, Execution time and Arrival Time
// Where arrival time <= execution time

// Loops till number of process
for(int c = 0; c < numberOfProcess; c++)
{
// Calculates execution time
executionTime = executionTime + burstTime[c];
// Stores the first burst time as minimum
minimum = burstTime[k];

// Loops till number of process
for(int r = k; r < numberOfProcess; r++)
{
// Checks if the execution time is greater than or equals to
// r index position process arrival time
// and r index position burst time is less than the minimum
if (executionTime >= arrivalTime[r] && burstTime[r] < minimum)
{
// Swapping process for process ID
temp = processID[k];
processID[k] = processID[r];
processID[r] = temp;

// Swapping process for arrival time
temp = arrivalTime[k];
arrivalTime[k] = arrivalTime[r];
arrivalTime[r] = temp;

// Swapping process for burst time
temp = burstTime[k];
burstTime[k] = burstTime[r];
burstTime[r] = temp;
}// End of if condition
}// End of inner for loop
// Increase the counter by one
k++;
}// End of outer for loop

// Initialize waiting time to 0
waitingTime[0] = 0;

// Calculates total waiting time

// Loops till number of process
for(int r = 1; r < numberOfProcess; r++)
{
total = total + burstTime[r - 1];
waitingTime[r] = total - arrivalTime[r];
waitingTotal = waitingTotal + waitingTime[r];
}// End of for loop

// Calculates average waiting time
avgWaitingTime = (waitingTotal / numberOfProcess);

// Calculates total turn around time

// Loops till number of process
for(int r = 0; r < numberOfProcess; r++)
{
turnAround = turnAround + burstTime[r];
turnAroundTime[r] = turnAround - arrivalTime[r];
waitingTotal = waitingTotal + turnAroundTime[r];
}// End of for loop

// Calculates average turn around time
avgTurnAroundTime = (waitingTotal / numberOfProcess);

// Displays heading
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );

// Loops till number of process
for(int r = 0; r < numberOfProcess; r++)
// Displays process ID, burst time, arrival time, waiting time, and turn around time
printf("\n p%d\t %d\t %d\t\t %d\t\t %d",
processID[r], burstTime[r], arrivalTime[r], waitingTime[r], turnAroundTime[r]);

// Displays averages
printf("\n\n AVERAGE WAITING TIME : %f", avgWaitingTime);
printf("\n AVERAGE TURN AROUND TIME : %f",avgTurnAroundTime);
return 0;
}// End of main function

Sample Output:

******* SJF Scheduling (Non Preemptive Scheduling) ********

Process Burst Arrival Waiting Turn-around
p0 100 0 0 100
p4 40 50 50 90
p3 50 40 100 150
p2 60 30 160 220
p1 80 20 230 310

AVERAGE WAITING TIME : 108.000000
AVERAGE TURN AROUND TIME : 282.000000

Add a comment
Know the answer?
Add Answer to:
(C++) Given an array with Job_id, Arrival_Time & Duration: Array[5][3] 0 0 100 1 20 80...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ Insert elements into a partially filled array. a) Create a partially filled array of...

    Using C++ Insert elements into a partially filled array. a) Create a partially filled array of CAPACITY 100. b) Initialize the array with values 10,20,30,40,50,60,70,80,90,100 c) Create a print function for the array. d) Create an insert function which inserts an integer into the array in sorted position. e) Insert the numbers 5, 150 and 55. f) Print the array before and after each insertion. Output Example 10 20 30 40 50 60 70 80 90 100 5 10 20...

  • Question Completion Status: 10 20 30 40 50 60 70 80 90 100 110 120 130...

    Question Completion Status: 10 20 30 40 50 60 70 80 90 100 110 120 130 140 > Moving to another question will save this response. uestion 17 T-6 T-4 Start T-5 T-7 Finish T=5 T-6 Given the network in Figure, the time to complete those activities on the critical path is expected to be OA) 20 B) 22 Oo oo D) 26 A Moving to another question will save this response.

  • 110 110 Vapor C100 100 90 Liquid 80 80 Cycl 0% Tol. 100% 100% Cycl. 0%...

    110 110 Vapor C100 100 90 Liquid 80 80 Cycl 0% Tol. 100% 100% Cycl. 0% Tol. 60 40 60 80 20 40 Mole percent cyclohexane Mole percent toluene 1. Use the phase diagram above to answer the following. Show work on the phase diagram. Starting with a solution that is 80.0 % toluene at 80°C a. At what point does this solution start to boil? b. What is the composition of this vapor? How many plates (steps of condensation...

  • QUESTION 1 Figure 2-5 100 90 80 70 60 50 40 30 20 10 10 20...

    QUESTION 1 Figure 2-5 100 90 80 70 60 50 40 30 20 10 10 20 30 40 50 60 70 80 washe Refer to Figure 2-5. It is possible for this economy to produce O a. 60 dryers and 50 washers. b. 60 dryers and 60 washers. c. 80 dryers and 50 washers. O d. All of the above.

  • $120 Tools $120 Tools . . $100 S, $100 s, Surplus1 Surplus2 S2 $80 $80 Price...

    $120 Tools $120 Tools . . $100 S, $100 s, Surplus1 Surplus2 S2 $80 $80 Price $60 Price $60 $40 $40 $20 D $20 D 0 a 0 20 40 60 80 100 120 20 40 60 80 100 120 Quantity Quantity c. Use a supply and demand diagram to show how that response will change the combined amount of consumer surplus and producer surplus in the market for cashews.

  • 1. Consider the following project. Activity Duration (weeks)5 Immediate Predecessor(s) Cost (S) 1000 | 400 I 20...

    1. Consider the following project. Activity Duration (weeks)5 Immediate Predecessor(s) Cost (S) 1000 | 400 I 200 | 1200 | 500 | 700 | 1200 | 800 | 1800 | 1500 | 400 (a) Construct the activity-on-node (AoN) network for the relations among the activities in the (b) Using the activity-on-node (Ao) network developed in part (a), determine the critical path and the minimum time required to complete the project. Develop the earliest start and finish times, the latest finish...

  • what are possible structures for each spectra 100 MS 80 - 60 Relative Intensity 40 20-...

    what are possible structures for each spectra 100 MS 80 - 60 Relative Intensity 40 20- 0 10 20 30 40 60 70 80 50 m/z 100- 80 Relative Intensity 40 20 0 10 15 20 25 30 35 40 50 55 60 65 70 75 80 45 m/z

  • Canvas XCO Composition as 0 100 20 60 80 40 1 327°C 600 300 Liquid 500...

    Canvas XCO Composition as 0 100 20 60 80 40 1 327°C 600 300 Liquid 500 232C a + L 200 400 183°C 8 Temperature (°C) Temperature (7) 18.3 61.9 97.8 300 100 4+ 200 100 0 O 20 40 60 80 100 (Pb) Composition (wt% Sn) (Sn) 3. A Pb-Sn phase diagram is given Answer the following questions. There is no need to re-draw the diagram in your solution sheet. (a) What is maximum solubility of Sn in Pb...

  • 5. (4 points) The Figure shows the project network with normal durations of the activities (in...

    5. (4 points) The Figure shows the project network with normal durations of the activities (in weeks). The following table shows the normal durations, shortest possible durations, achieved at extra cost, & the acceleration cost per time unit. Activity Immediate predecessor(s) Normal duration Shortest possible duration Unit cost of acceleration ($ in Thousands) 40 C 5 4 50 80 DA 5 1 50 (6,5, 40) (5, 4, 50) Start End D (5, 1, 50) (4, 3, 80) Develop all possible...

  • luuent Name: 1. (100 points) Three processes P1, P2 and P3 with related information are given...

    luuent Name: 1. (100 points) Three processes P1, P2 and P3 with related information are given in the following table: Process Burst Time (ms) Arrival Time (ms) P1 T 0 P2 30 20 P3 20 T is a positive integer (T>0). Please use a non-preemptive shortest job first scheduling algorithm to make Gantt charts, and calculate different waiting times of three processes for different cases. Please write a professional perfect solution with detailed steps. 30

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT