Question

In: Computer Science

In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...

In C++, assuming you have the following incomplete code:

#include<iostream>
#include <unistd.h>
using namespace std;
// Structure for storing the process data
struct procData
{
char pname[5]; // Name of a process
int arrivt; //Arrival time of a process
int pburst; // Burst time of a process
int endtime; // Exit time/ Leaving time of a process
int remburst; // Remaining burst time of a process
int readyFlag; // boolean, Flag for maintaining the process status
};
// Global variable
int qTime; // Time quantum
// Function prototypes
procData* procIPData(int ); // Input the processes data in the global array
void calculate(procData*, int); // Calculate the average waiting and turn around time
void display_waittime(procData*, int); // Display the average and waiting and turn around time
int main()
{
procData *q;
int noProcess; // Number of processes
cout << "Enter Total no. of processes" << endl;
cin >> noProcess;
q = procIPData(noProcess);
calculate(q, noProcess);
display_waittime(q, noProcess);
return 0;
}

procData* procIPData(int noProc)
{
procData *a = new procData[noProc];
for (int i = 0; i < noProc; i++)
{
cout << "Enter process name:" << endl;
cin >> a[i].pname;
cout << "Enter process burst time:" << endl;
cin >> a[i].pburst;
cout << "Enter process arrival time:" << endl;
cin >> a[i].arrivt;
a[i].remburst = a[i].pburst;
a[i].endtime = 0;
a[i].readyFlag = 0;
}
cout << "Enter the time quantum/Time Slice:" << endl;
cin >> qTime;
return a;
}
void calculate(procData* a, int noProc)
{
// To be completed
}
void display_waittime(procData* a, int noProc)
{
int totalTA = 0, totalWait = 0, ta = 0, tw = 0;
for (int i = 0; i < noProc; i++)
{
ta = a[i].endtime - a[i].arrivt;
tw = ta - a[i].pburst;
cout << endl << "Waiting time for Process " << a[i].pname << " is " << tw << endl;
totalTA = totalTA + ta;
totalWait = totalWait + tw;
}
cout << "Average waiting time = " << ((float) totalWait / (float) noProc) << endl;
cout << "Average turnaround time= " << ((float) totalTA / (float) noProc) << endl;
}

Complete the C++ program so that it displays the Gantt chart, average waiting time and average turnaround time for the list of process using pre-emptive Round-robin CPU scheduling strategy. The program takes the CPU burst times and arrival times for the given list of processes as input.

Solutions

Expert Solution

The solution for above problem is as follows:-

Code-

#include<iostream>
#include <unistd.h>
#include<algorithm>
using namespace std;
// Structure for storing the process data
struct procData
{
   char pname[5]; // Name of a process
   int arrivt; //Arrival time of a process
   int pburst; // Burst time of a process
   int endtime; // Exit time/ Leaving time of a process
   int remburst; // Remaining burst time of a process
   int readyFlag; // boolean, Flag for maintaining the process status
};
// Global variable
int qTime; // Time quantum
// Function prototypes
procData* procIPData(int noProc); // Input the processes data in the global array
void calculate(procData*, int); // Calculate the average waiting and turn around time
void display_waittime(procData*, int); // Display the average and waiting and turn around time
int main()
{
   procData *q;
   int noProcess; // Number of processes
   cout << "Enter Total no. of processes" << endl;
   cin >> noProcess;
   q = procIPData(noProcess);
   calculate(q, noProcess);
   display_waittime(q, noProcess);
   return 0;
}

procData* procIPData(int noProc)
{
   procData *a = new procData[noProc];
   for (int i = 0; i < noProc; i++)
   {
       cout << "Enter process name:" << endl;
       cin >> a[i].pname;
       cout << "Enter process burst time:" << endl;
       cin >> a[i].pburst;
       cout << "Enter process arrival time:" << endl;
       cin >> a[i].arrivt;
       a[i].remburst = a[i].pburst;
       a[i].endtime = 0;
       a[i].readyFlag = 0;
   }
   cout << "Enter the time quantum/Time Slice:" << endl;
   cin >> qTime;
   return a;
}
bool compare(procData a,procData b){
   return a.arrivt<b.arrivt;
}
void calculate(procData* a, int noProc)
{
   sort(a,a+noProc,compare);//Sorting processes accoridng to arrival time;
   int time,count,flag=0,remain=noProc;
   for(time=0,count=0;remain!=0;)
   {
       if(a[count].remburst<=qTime && a[count].remburst>0)
       {
           time+=a[count].remburst;
           a[count].remburst=0;
           flag=1;
       }
       else if(a[count].remburst>0)
       {
           a[count].remburst-=qTime;
           time+=qTime;
       }
       if(a[count].remburst==0 && flag==1)
       {
           remain--;
           a[count].endtime=time;
           flag=0;
       }
       if(count==noProc-1)
           count=0;
       else if(a[count+1].arrivt<=time)
           count++;
       else
           count=0;
   }
}
void display_waittime(procData* a, int noProc)
{
   int totalTA = 0, totalWait = 0, ta = 0, tw = 0;
   for (int i = 0; i < noProc; i++)
   {
       ta = a[i].endtime - a[i].arrivt;
       tw = ta - a[i].pburst;
       cout << endl << "Waiting time for Process " << a[i].pname << " is " << tw << endl;
       totalTA = totalTA + ta;
       totalWait = totalWait + tw;
   }
   cout << "Average waiting time = " << ((float) totalWait / (float) noProc) << endl;
   cout << "Average turnaround time= " << ((float) totalTA / (float) noProc) << endl;
}

Code screenshots-

Outputs-

Feel free to comment for any issues, do rate the answer positively


Related Solutions

Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT