Quick actions

cmd+k|ctrl+k

Navigation

Languages

Min-min and Max-Min

Snippet info

Language

C

Visibility

public

Author

fcb.satadrud10s

Created

2023-04-24T20:35:59.648775Z

Updated

2023-04-24T20:35:59.648775Z

#include <stdio.h>
#include <stdlib.h>

#define MAX_MACHINES 10
#define MAX_TASKS 10

// function prototypes
void min_min(int n_machines, int n_tasks, int task_times[MAX_MACHINES][MAX_TASKS]);
void max_min(int n_machines, int n_tasks, int task_times[MAX_MACHINES][MAX_TASKS]);

int main()
{
    int n_machines, n_tasks, i, j;
    int task_times[MAX_MACHINES][MAX_TASKS];
    int choice;

    // get user input for number of machines and tasks
    printf("Enter the number of machines: ");
    scanf("%d", &n_machines);
    printf("Enter the number of tasks: ");
    scanf("%d", &n_tasks);

    // get user input for task times for each machine
    for (i = 0; i < n_machines; i++)
    {
        printf("Enter task times for machine %d:\n", i + 1);
        for (j = 0; j < n_tasks; j++)
        {
            printf("Task %d: ", j + 1);
            scanf("%d", &task_times[i][j]);
        }
    }

    // display menu
    printf("\nTask scheduling algorithms:\n");
    printf("1. Min-Min\n");
    printf("2. Max-Min\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice)
    {
    case 1:
        min_min(n_machines, n_tasks, task_times);
        break;
    case 2:
        max_min(n_machines, n_tasks, task_times);
        break;
    default:
        printf("Invalid choice.\n");
        break;
    }

    return 0;
}

void min_min(int n_machines, int n_tasks, int task_times[MAX_MACHINES][MAX_TASKS])
{
    int i, j, machine, task;
    int scheduled[MAX_TASKS] = {0};
    int completion_time[MAX_MACHINES] = {0};

    printf("\nScheduling using Min-Min algorithm...\n");

    for (i = 0; i < n_tasks; i++)
    {
        int min_time = 99999;

        for (j = 0; j < n_machines; j++)
        {
            if (!scheduled[j] && task_times[j][i] < min_time)
            {
                machine = j;
                task = i;
                min_time = task_times[j][i];
            }
        }

        scheduled[machine] = 1;
        completion_time[machine] += task_times[machine][task];
        printf("Task %d scheduled to machine %d.\n", task + 1, machine + 1);
    }

    printf("Completion times:\n");
    for (i = 0; i < n_machines; i++)
    {
        printf("Machine %d: %d\n", i + 1, completion_time[i]);
    }
}

void max_min(int n_machines, int n_tasks, int task_times[MAX_MACHINES][MAX_TASKS])
{
    int i, j, machine, task;
    int scheduled[MAX_TASKS] = {0};
    int completion_time[MAX_MACHINES] = {0};

    printf("\nScheduling using Max-Min algorithm...\n");

    for (i = 0; i < n_tasks; i++)
    {
        int max_time = -99999;

        for (j = 0; j < n_machines; j++)
        {
            if (!scheduled[j] && task_times[j][i] > max_time)
            {
                machine = j;
                task = i;
                max_time = task_times[j][i];
            }
        }

        scheduled[machine] = 1;
        completion_time[machine] += task_times[machine][task];
        printf("Task %d scheduled to machine %d.\n", task + 1, machine + 1);
    }

    printf("Completion times:\n");
    for (i = 0; i < n_machines; i++)
    {
        printf("Machine %d: %d\n", i + 1, completion_time[i]);
    }
}
INFO