Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views2 pages

Job Sequencing

The document contains a C++ program that implements a job scheduling algorithm based on profit maximization. It sorts jobs by profit in descending order and schedules them within their deadlines, calculating the total number of jobs completed and the total profit earned. Finally, it outputs the total jobs done, total profit, and the scheduled job IDs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Job Sequencing

The document contains a C++ program that implements a job scheduling algorithm based on profit maximization. It sorts jobs by profit in descending order and schedules them within their deadlines, calculating the total number of jobs completed and the total profit earned. Finally, it outputs the total jobs done, total profit, and the scheduled job IDs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

struct Job {
int id;
int deadline;
int profit;
};

// Manual sort: descending order of profit


void sortJobs(Job jobs[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (jobs[j].profit < jobs[j + 1].profit) {
Job temp = jobs[j];
jobs[j] = jobs[j + 1];
jobs[j + 1] = temp;
}
}
}
}

int main() {
Job jobs[] = {
{1, 2, 100},
{2, 1, 19},
{3, 2, 27},
{4, 1, 25},
{5, 3, 15}
};

int n = sizeof(jobs) / sizeof(jobs[0]);

sortJobs(jobs, n);

int maxDeadline = 0;
for (int i = 0; i < n; i++) {
if (jobs[i].deadline > maxDeadline)
maxDeadline = jobs[i].deadline;
}

int slot[10]; // Assuming deadline won't exceed 10


for (int i = 0; i <= maxDeadline; i++)
slot[i] = -1;

int count = 0, totalProfit = 0;

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


for (int j = jobs[i].deadline; j > 0; j--) {
if (slot[j] == -1) {
slot[j] = jobs[i].id;
totalProfit += jobs[i].profit;
count++;
break;
}
}
}

cout << "Total Jobs Done: " << count << endl;
cout << "Total Profit: " << totalProfit << endl;

cout << "Jobs scheduled: ";


for (int i = 1; i <= maxDeadline; i++) {
if (slot[i] != -1)
cout << slot[i] << " ";
}
cout << endl;

return 0;
}

You might also like