Untitled

 avatar
unknown
plain_text
9 months ago
1.8 kB
10
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> // Turbo C++ screen functions

typedef struct {
    char id[5];
    int deadline;
    int profit;
} Job;

int compare(const void *a, const void *b) {
    Job *job1 = (Job *)a;
    Job *job2 = (Job *)b;
    return job2->profit - job1->profit;
}

int findMaxDeadline(Job jobs[], int n) {
    int max = jobs[0].deadline;
    int i;
    for (i = 1; i < n; i++) {
        if (jobs[i].deadline > max)
            max = jobs[i].deadline;
    }
    return max;
}

void jobSequencing(Job jobs[], int n) {
    int i, j;
    qsort(jobs, n, sizeof(Job), compare);

    int maxDeadline = findMaxDeadline(jobs, n);

    // Turbo C++ does not support variable-length arrays (VLAs)
    // So we define a max size (assumed max deadline)
    char result[10][5];
    int slot[10];

    for (i = 0; i < maxDeadline; i++) {
        slot[i] = 0;
        strcpy(result[i], "");
    }

    int totalProfit = 0;

    for (i = 0; i < n; i++) {
        for (j = jobs[i].deadline - 1; j >= 0; j--) {
            if (slot[j] == 0) {
                slot[j] = 1;
                strcpy(result[j], jobs[i].id);
                totalProfit += jobs[i].profit;
                break;
            }
        }
    }

    printf("Scheduled Jobs: ");
    for (i = 0; i < maxDeadline; i++) {
        if (slot[i]) {
            printf("%s ", result[i]);
        }
    }
    printf("\nTotal Profit: %d", totalProfit);
}

void main() {
    clrscr(); // Clears screen in Turbo C++
   
    Job jobs[] = {
        {"J1", 2, 60},
        {"J2", 1, 100},
        {"J3", 3, 20},
        {"J4", 2, 40},
        {"J5", 1, 20}
    };

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

    jobSequencing(jobs, n);

    getch(); // Waits for key press before closing
}
Editor is loading...
Leave a Comment