Job Sequencing With Deadlines
#include<stdio.h>
#define MAX 100
typedef struct {
int id, profit,
deadline;
} Job;
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;
}
}
void jobSequencing(Job jobs[], int n) {
int slot[MAX] = {0},
profit = 0;
sortJobs(jobs, n);
printf("The
sequence of jobs is: ");
for (int i = 0; i <
n; i++) {
for (int j = jobs[i].deadline; j > 0; j--) {
if (!slot[j]) {
slot[j] = 1;
profit += jobs[i].profit;
printf("J%d
", jobs[i].id);
break;
}
}
}
printf("\nProfit
is %d\n", profit);
}
int main() {
int n;
Job jobs[MAX];
printf("Enter the
number of jobs: ");
scanf("%d",
&n);
for (int i = 0; i <
n; i++) {
printf("Enter Job ID, profit, and deadline for job %d: ", i + 1);
scanf("%d %d %d", &jobs[i].id, &jobs[i].profit,
&jobs[i].deadline);
}
jobSequencing(jobs,
n);
return 0;
}
Comments
Post a Comment