After implementing suggestions from my previous question and after modifying I have written this code. Function sortAccordingArrivalTime()
sorts arrivalTime
burstTime
and priority
according to arrival time. I have tried to enter Arrival Time, Burst Time and Priority in respective order for each process at a time. Help me to improve this code and optimize it with more use of C++11 and C++14.
scheduling.h
#ifndef SCHEDULING_H_
#define SCHEDULING_H_
#include <vector>
using uint = unsigned int;
class Scheduling
{
uint currActiveProcessID;
uint timeCounter = 0;
double avgWaitingTime;
double avgTurnAroundTime;
std::vector<uint> arrivalTime;
//When process start to execute
std::vector<uint> burstTime;
//process wait to execute after they have arrived
std::vector<uint> waitingTime;
//total time taken by processes
std::vector<uint> turnArountTime;
public:
Scheduling(uint num = 0);
Scheduling(const Scheduling&) = delete;
Scheduling &operator=(const Scheduling&) = delete;
Scheduling(Scheduling&&) = delete;
Scheduling &operator=(Scheduling&&) = delete;
~Scheduling() = default;
void calcWaitingTime();
void calcTurnAroundTime();
void printInfo();
private:
void sortAccordingArrivalTime();
};
#endif
priority.cpp
#include <iostream>
#include <vector>
#include <algorithm> // std::find
#include <iterator> // std::begin, std::end
#include <limits> //std::numeric_limits
#include "scheduling.h"
using uint = unsigned int;
std::vector<uint> priority;
Scheduling::Scheduling(uint n): waitingTime(n, 0)
{
arrivalTime.reserve(n);
burstTime.reserve(n);
waitingTime.reserve(n);
turnArountTime.reserve(n);
priority.reserve(n);
std::cout << "Enter Arrival Time, Burst Time, Priority in respective ";
std::cout << "order (eg 2 15 4)\n";
std::cout << "Lower integer has higher priority";
for (uint i = 0; i < n; i++)
{
uint arrivalVal, burstVal, priorityVal;
std::cout << "\nProcess " << i+1 << ": ";
std::cin >> arrivalVal >> burstVal >> priorityVal;
arrivalTime.push_back(arrivalVal);
burstTime.push_back(burstVal);
priority.push_back(priorityVal);
}
}
void Scheduling::sortAccordingArrivalTime()
{
for (std::size_t i = 0; i < arrivalTime.size(); i++)
{
for (std::size_t j = i+1; j < arrivalTime.size(); j++)
{
if (arrivalTime[i] > arrivalTime[j])
{
uint temp = arrivalTime[i];
arrivalTime[i] = arrivalTime[j];
arrivalTime[j] = temp;
temp = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = temp;
temp = priority[i];
priority[i] = priority[j];
priority[j] = temp;
}
}
}
}
void Scheduling::calcWaitingTime()
{
std::vector<uint> burstTimeCopy;
std::copy(burstTime.begin(), burstTime.end(),
std::back_inserter(burstTimeCopy));
//If entered arrival time are not sorted
if (! (std::is_sorted(arrivalTime.begin(), arrivalTime.end())) )
{
sortAccordingArrivalTime();
}
while (!(std::all_of(burstTimeCopy.begin(),
burstTimeCopy.end(), [] (uint e) { return e == 0; })))
{
auto maxArrivalTime = std::max_element(arrivalTime.begin(),
arrivalTime.end());
if (timeCounter <= *maxArrivalTime)
{
uint maxPriority = std::numeric_limits<uint>::max();
for (std::size_t i = 0; i < burstTimeCopy.size(); i++)
{
if (burstTimeCopy[i] != 0 && priority[i] < maxPriority
&& arrivalTime[i] <= timeCounter)
{
maxPriority = priority[i];
currActiveProcessID = i;
}
}
burstTimeCopy[currActiveProcessID] -= 1;
for (std::size_t i = 0; i < burstTimeCopy.size(); i++)
{
if (timeCounter >= arrivalTime[i] && i != currActiveProcessID
&& burstTimeCopy[i] != 0)
{
waitingTime[i] += 1;
}
}
timeCounter++;
}
else
{
uint maxPriority = std::numeric_limits<uint>::max();
for (std::size_t i = 0 ; i < burstTimeCopy.size(); i++)
{
if (burstTimeCopy[i] != 0 && priority[i] < maxPriority)
{
maxPriority = priority[i];
currActiveProcessID = i;
}
}
for (std::size_t i = 0; i < burstTimeCopy.size(); i++)
{
if (i != currActiveProcessID && burstTimeCopy[i] != 0)
{
waitingTime[i] += burstTimeCopy[currActiveProcessID];
}
}
timeCounter += burstTimeCopy[currActiveProcessID];
burstTimeCopy[currActiveProcessID] = 0;
}
}
uint sum = 0;
for (auto element: waitingTime)
{
sum += element;
}
avgWaitingTime = sum / waitingTime.size();
}
void Scheduling::calcTurnAroundTime()
{
uint sum = 0;
for (std::size_t i = 0; i < arrivalTime.size(); i++)
{
uint val = burstTime[i] + waitingTime[i];
turnArountTime.push_back(val);
sum += val;
}
avgTurnAroundTime = sum / turnArountTime.size();
}
void Scheduling::printInfo()
{
std::cout << "ProcessID\tArrival Time\tBurst Time\tPriority\tWaiting Time";
std::cout << "\tTurnaround Time\n";
for (std::size_t i = 0; i < arrivalTime.size(); i++)
{
std::cout << i+1 << "\t\t" << arrivalTime[i] << "\t\t" << burstTime[i];
std::cout << "\t\t" << priority[i] << "\t\t" << waitingTime[i];
std::cout << "\t\t" << turnArountTime[i] << '\n';
}
std::cout << "Average Waiting Time : " << avgWaitingTime << '\n';
std::cout << "Average Turn Around Time : " << avgTurnAroundTime << '\n';
}
int main()
{
int num;
std::cout << "Enter the number of processes\n";
std::cin >> num;
Scheduling prioritySchedule(num);
prioritySchedule.calcWaitingTime();
prioritySchedule.calcTurnAroundTime();
prioritySchedule.printInfo();
}