Skip to main content
added 13 characters in body
Source Link
coder
  • 2.5k
  • 4
  • 32
  • 60
#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
                     && iarrivalTime[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();
}
#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
                     && 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();
}
#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();
}
deleted 32 characters in body
Source Link
coder
  • 2.5k
  • 4
  • 32
  • 60
#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 (burstTimeCopy.empty() || !(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
                     && 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();
}
#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 (burstTimeCopy.empty() || !(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
                     && 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();
}
#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
                     && 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();
}
deleted 31 characters in body
Source Link
coder
  • 2.5k
  • 4
  • 32
  • 60
#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));
    std::vector<uint>::iterator it;
    //If entered arrival time are not sorted
    if (! (std::is_sorted(arrivalTime.begin(), arrivalTime.end())) )
    {
        sortAccordingArrivalTime();
    }

    while (burstTimeCopy.empty() || !(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
                     && 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();
}
#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));
    std::vector<uint>::iterator it;
    //If entered arrival time are not sorted
    if (! (std::is_sorted(arrivalTime.begin(), arrivalTime.end())) )
    {
        sortAccordingArrivalTime();
    }

    while (burstTimeCopy.empty() || !(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
                     && 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();
}
#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 (burstTimeCopy.empty() || !(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
                     && 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();
}
Source Link
coder
  • 2.5k
  • 4
  • 32
  • 60
Loading