Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Question 1

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;

// Define a Process structure to hold process information


struct Process {
string name;
int executionTime;
int arrivalTime; // Time when the process was added to the queue
int startTime; // Time when the process starts execution
};

// Comparator for sorting processes based on execution time


bool compareProcesses(const Process& p1, const Process& p2) {
return p1.executionTime > p2.executionTime;
}

class Scheduler {
public:
void loadProcessesFromFile(const string& filename);
void executeProcesses();
void printReadyQueue();
void runScheduler();

private:
vector<Process> readyQueue;
int currentTime = 0;
int totalWaitingTime = 0;
int totalTurnaroundTime = 0;

void updateWaitingTime();
};

// Load processes from a file and add them to the ready queue
void Scheduler::loadProcessesFromFile(const string& filename) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error: Could not open the file " << filename << endl;
return;
}

Process process;
while (file >> process.name >> process.executionTime) {
process.arrivalTime = currentTime;
readyQueue.push_back(process);
}

file.close();
// Sort the ready queue based on execution time (highest-execution-time-first)
sort(readyQueue.begin(), readyQueue.end(), compareProcesses);
}

// Print the contents of the ready queue


void Scheduler::printReadyQueue() {
cout << "The contents of the ready queue are:" << endl;
for (const Process& process : readyQueue) {
cout << process.name << ", " << process.executionTime << endl;
}
cout << endl;
}

// Execute the processes in the ready queue


void Scheduler::executeProcesses() {
while (!readyQueue.empty()) {
Process& currentProcess = readyQueue.back();
currentProcess.startTime = currentTime;
cout << "Executing " << currentProcess.name << "..." << endl;

// Simulate the execution of the process for its execution time


while (currentTime - currentProcess.startTime < currentProcess.executionTime) {
currentTime++;
updateWaitingTime();

// Check if 15 time units have passed


if (currentTime % 15 == 0) {
// Pause and check for new processes in the file
cout << "Press Enter to continue...";
cin.ignore(); // Wait for user input (Enter key)
loadProcessesFromFile("readylist.txt"); // Reload processes from file
printReadyQueue();
}
}

// Calculate and print turnaround time


int turnaroundTime = currentTime - currentProcess.arrivalTime;
cout << currentProcess.name << ", " << currentProcess.executionTime << ", "
<< turnaroundTime - currentProcess.executionTime << ", " << turnaroundTime << endl;

// Update total turnaround time


totalTurnaroundTime += turnaroundTime;

// Remove the executed process from the queue


readyQueue.pop_back();
}
}

// Update waiting time for all processes in the queue


void Scheduler::updateWaitingTime() {
for (Process& process : readyQueue) {
if (process.startTime == -1) {
process.startTime = currentTime;
}
}
}

// Run the scheduler


void Scheduler::runScheduler() {
loadProcessesFromFile("readylist.txt");
printReadyQueue();
executeProcesses();

// Calculate and display average waiting time and average turnaround time
int numProcesses = readyQueue.size();
double averageWaitingTime = static_cast<double>(totalWaitingTime) / numProcesses;
double averageTurnaroundTime = static_cast<double>(totalTurnaroundTime) / numProcesses;

cout << "Average Waiting Time: " << averageWaitingTime << endl;
cout << "Average Turnaround Time: " << averageTurnaroundTime << endl;
}
int main() {
Scheduler scheduler;
scheduler.runScheduler();

return 0;
}

Question 2:
#include <iostream>
#include <ctime>

using namespace std;

class Sorter {
public:
void sort(int* data, int size, bool ascending) {
// This is a generic sorting function that can be overridden if needed.
if (ascending) {
sortAscending(data, size);
}
else {
sortDescending(data, size);
}
}

protected:
void sortAscending(int* data, int size) {
// Default ascending sort
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (data[j] > data[j + 1]) {
swap(data[j], data[j + 1]);
}
}
}
}

void sortDescending(int* data, int size) {


// Default descending sort
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (data[j] < data[j + 1]) {
swap(data[j], data[j + 1]);
}
}
}
}
};

class InsertionSorter : public Sorter {


public:
void sort(int* data, int size, bool ascending) {
if (ascending) {
sortAscending(data, size);
}
else {
sortDescending(data, size);
}
}
private:
void sortAscending(int* data, int size) {
// Custom insertion sort for ascending order
for (int i = 1; i < size; i++) {
int key = data[i];
int j = i - 1;
while (j >= 0 && data[j] > key) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = key;
}
}

void sortDescending(int* data, int size) {


// Custom insertion sort for descending order
for (int i = 1; i < size; i++) {
int key = data[i];
int j = i - 1;
while (j >= 0 && data[j] < key) {
data[j + 1] = data[j];
j--;
}
data[j + 1] = key;
}
}
};

class BubbleSorter : public Sorter {


public:
void sort(int* data, int size, bool ascending) {
if (ascending) {
sortAscending(data, size);
}
else {
sortDescending(data, size);
}
}
private:
void sortAscending(int* data, int size) {
// Custom bubble sort for ascending order
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (data[j] > data[j + 1]) {
swap(data[j], data[j + 1]);
}
}
}
}

void sortDescending(int* data, int size) {


// Custom bubble sort for descending order
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (data[j] < data[j + 1]) {
swap(data[j], data[j + 1]);
}
}
}
}
};

class SelectionSorter : public Sorter {


public:
void sort(int* data, int size, bool ascending) {
if (ascending) {
sortAscending(data, size);
}
else {
sortDescending(data, size);
}
}
private:
void sortAscending(int* data, int size) {
// Custom selection sort for ascending order
for (int i = 0; i < size - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < size; j++) {
if (data[j] < data[min_idx]) {
min_idx = j;
}
}
swap(data[i], data[min_idx]);
}
}

void sortDescending(int* data, int size) {


// Custom selection sort for descending order
for (int i = 0; i < size - 1; i++) {
int max_idx = i;
for (int j = i + 1; j < size; j++) {
if (data[j] > data[max_idx]) {
max_idx = j;
}
}
swap(data[i], data[max_idx]);
}
}
};

void printData(int* data, int size) {


for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << endl;
}

void generateRandomData(int* data, int size) {


for (int i = 0; i < size; i++) {
data[i] = rand() % 10000; // Generate random numbers between 0 and 9999
}
}

int main() {
srand(time(0)); // Seed the random number generator

int size;
cout << "Enter the size of the dataset: ";
cin >> size;

int* data = new int[size];


cout << "Enter the dataset elements (or 0 to generate random data): ";

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


cin >> data[i];
}

if (data[0] == 0) {
generateRandomData(data, size);
cout << "Generated random data: ";
printData(data, size);
}

bool ascending;
cout << "Enter the order to sort data (1 for ascending, 0 for descending): ";
cin >> ascending;

int choice;
cout << "Choose a sorting algorithm:\n1. Insertion Sort\n2. Bubble Sort\n3. Selection Sort\n";
cin >> choice;

Sorter* sorter;
switch (choice) {
case 1:
sorter = new InsertionSorter();
break;
case 2:
sorter = new BubbleSorter();
break;
case 3:
sorter = new SelectionSorter();
break;
default:
cout << "Invalid choice. Exiting." << endl;
delete[] data;
return 1;
}
clock_t start_time = clock();
sorter->sort(data, size, ascending);
clock_t end_time = clock();

double execution_time = double(end_time - start_time) / CLOCKS_PER_SEC * 1000; // Milliseconds

cout << "Sorted data: ";


printData(data, size);

cout << "Execution time: " << execution_time << " milliseconds" << endl;

delete[] data;
delete sorter;

return 0;
}

You might also like