Question

In: Computer Science

In C++, Complete the Code & Show the output. Schedule the following process using Shortest Job...

In C++, Complete the Code & Show the output.

Schedule the following process using Shortest Job First Scheduling algorithm

Porcress Burst time Arrival time
1 8 0
2 2 0
3 1 0
4 4 0

Compute the following and show the output

a) Individual Waiting time & Turnaround time

b) Average Waiting time & Turnaround time

c) Display the Gantt chart (Order of Execution)   

#include

using namespace std;

//structure for every process

struct Process {

int pid; // Process ID

int bt; // Burst Time

int art; // Arrival Time

};

// Soring Process based on Burst Time Descending Order

void sort(Process a[],int n) {

//-------------Sorting

// Write Code Here

}

// function to find the waiting time for all processes

void WaitingTime(Process proc[], int n, int wt[])

{

// Write Code Here

}

// function to calculate turn around time

void TurnAroundTime(Process proc[], int n, int wt[], int tat[])

{

// Write Code Here

}

int main() {

const int n=4;

Process proc[] = { { 1, 8, 0 },

{ 2, 2, 0 },

{ 3, 1, 0 },

{ 4, 4, 0 } };

int wt[n], tat[n], total_wt = 0,total_tat = 0;

// Sort Processes based on Burst Time

sort(proc,n);

// Function to find waiting time of all processes

WaitingTime(proc, n, wt);

// Function to find turn around time for all processes

TurnAroundTime(proc, n, wt, tat);

  

// Write Code Here

cout<<"\n\nOrder of Execution ";

for (int i = 0; i < n; i++) {

cout<<"P"<";

}

return 0;

}

Solutions

Expert Solution

The above described code is good,but I have made a less complex and better solution for the given problem. Hope  that answers the problem described.

#include<iostream>
#include<conio.h>
using namespace std;

void SearchStack01(int pnt,int tm);
void SearchStack02(int pnt, int tm);
void AddQue(int pnt);
int at[50], bt[50], ct[50]={0}, qt, rqi[50]={0}, c=0, st, flg=0, tm=0, noe=0, pnt=0, btm[50]={0}, tt, wt;
float att, awt;

main(){

cout<<"ROUND ROBIN ALGO : INPUT 5 PROCESSES\n";
for(int x=0;x<5;x++){
    cout<<"\nProcess "<<x+1;
    cout<<"\nAT=";
    cin>>at[x];
    cout << "BT=";
    cin>>bt[x];
    btm[x]=bt[x];}

cout<<"\nEnter time quantum:";
cin>>qt;

system("CLS");

cout<<endl<<"GANTT CHART"<<endl<<at[0];
do{
    if(flg==0){
       st=at[0];
       //---ReduceBT
       if(btm[0]<=qt){
          tm=st+btm[0];
          btm[0]=0;
          SearchStack01(pnt,tm);}
       else{
          btm[0]=btm[0]-qt;
          tm=st+qt;
          SearchStack01(pnt,tm);
          AddQue(pnt);}
    }//if
 
    else{
       pnt=rqi[0]-1;
       st=tm;
       //---DeleteQue
       for(int x=0;x<noe && noe!=1;x++){
          rqi[x]=rqi[x+1];}
       noe--;
       //---ReduceBT
       if(btm[pnt]<=qt){
          tm=st+btm[pnt];
          btm[pnt]=0;
          SearchStack02(pnt, tm);}
       else{
         btm[pnt]=btm[pnt]-qt;
          tm=st+qt;
          SearchStack02(pnt, tm);
          AddQue(pnt);}
    }//else

    //AssignCTvalue
    if(btm[pnt]==0){
       ct[pnt]=tm;
    }//if

   flg++;
   cout<<"]-P"<<pnt+1<<"-["<<tm;

}while(noe!=0);

cout<<"\n\nPROCESS\t AT\t BT\t CT\t TT\t WT\n";
for(int x=0;x<5;x++){
    tt=ct[x]-at[x];
    wt=tt-bt[x];
    cout<<"P"<<x+1<<" \t "<<at[x]<<" \t "<<bt[x]<<" \t "<<ct[x]<<" \t "<<tt<<" \t "<<wt<<"\n";
    awt=awt+wt;
    att=att+tt;
}//for

cout<<"\nAVERAGE TT: "<<att/5<<"\nAVERAGE WT: "<<awt/5;

}//main

void SearchStack01(int pnt,int tm){
    for(int x=pnt+1;x<5;x++){
       if(at[x]<=tm){
          rqi[noe]=x+1;
          noe++;}
    }//for
}//void

void SearchStack02(int pnt, int tm){
    for(int x=pnt+1;x<5;x++){
       //---CheckQue
       int fl=0;
       for(int y=0;y<noe;y++){
          if(rqi[y]==x+1){
             fl++;}}
       if(at[x]<=tm && fl==0 && btm[x]!=0){
          rqi[noe]=x+1;
          noe++;}
    }//for
}//void

void AddQue(int pnt){
    rqi[noe]=pnt+1;
    noe++;
}//void

Related Solutions

Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list...
Please complete following c++ code asap using following prototypes complete each missing part / Linked list operations int getLength() const {return length;} void insertNode(College); bool deleteNode(string); void displayList() const; bool searchList(string, College &) const; }; main.cpp /*   Build and procees a sorted linked list of College objects. The list is sorted in ascending order by the college code. Assume that the college code is unique. */ #include <iostream> #include <fstream> #include <string> #include "LinkedList.h" using namespace std; void buildList(const string...
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
how to send background process to foreground process using c code?
how to send background process to foreground process using c code?
Determine the output using C++ of the following code fragment i) double *pt; double a[3] =...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] = {1.2, 2.3, 3.4}; pt = &a[1]; pt += 1; cout << *pt << endl; ii) int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { cout << num << "bb"; num *= 3; } cout << endl; i++; }
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code...
Using C# Windows App Form Create a simple calculator using +,-,*,/ Please show code GUI Code for calculator menus radio button input text boxes
Show the output of the following code. Assume the node is in the usual info-link form...
Show the output of the following code. Assume the node is in the usual info-link form with info of the type int. Also, list and ptr are reference variables of the type Node. list = new Node( ); list.info = 88; ptr = new Node( ); ptr.info = 41; ptr.link = null; list.link = ptr; ptr = new Node( ); ptr.info = 99; ptr.link = list; list = ptr; ptr = new Node( ); ptr.info = 19; ptr.link = list.link;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT