worst fit program in c

Here is an example of a worst fit program in C:

#include<stdio.h>

int main() {
    int blockSize[10], processSize[10], blockNum, processNum, i, j, allocation[10];
    for (i = 0; i < 10; i++) {
        allocation[i] = -1;
    }
    printf("Enter the number of blocks: ");
    scanf("%d", &blockNum);
    printf("Enter the size of each block: ");
    for (i = 0; i < blockNum; i++) {
        scanf("%d", &blockSize[i]);
    }
    printf("Enter the number of processes: ");
    scanf("%d", &processNum);
    printf("Enter the size of each process: ");
    for (i = 0; i < processNum; i++) {
        scanf("%d", &processSize[i]);
    }
    for (i = 0; i < processNum; i++) {
        int worstFitIndex = -1;
        for (j = 0; j < blockNum; j++) {
            if (blockSize[j] >= processSize[i]) {
                if (worstFitIndex == -1) {
                    worstFitIndex = j;
                }
                else if (blockSize[j] > blockSize[worstFitIndex]) {
                    worstFitIndex = j;
                }
            }
        }
        if (worstFitIndex != -1) {
            allocation[i] = worstFitIndex;
            blockSize[worstFitIndex] -= processSize[i];
        }
    }
    printf("\nProcess No.\tProcess Size\tBlock No.\n");
    for (i = 0; i < processNum; i++) {
        printf("%d\t\t%d\t\t", i + 1, processSize[i]);
        if (allocation[i] != -1) {
            printf("%d\n", allocation[i] + 1);
        }
        else {
            printf("Not Allocated\n");
        }
    }
    return 0;
}

Explanation: 1. We begin by including the necessary header file stdio.h for input/output operations. 2. In the main() function, we declare several variables: blockSize and processSize arrays to store the sizes of blocks and processes, blockNum and processNum to store the number of blocks and processes, i and j as loop counters, and allocation array to store the allocation information. 3. We initialize the allocation array with -1, which indicates that no block is allocated to any process initially. 4. We prompt the user to enter the number of blocks and their sizes using printf() and scanf() functions. 5. Similarly, we prompt the user to enter the number of processes and their sizes. 6. In the next loop, we iterate over each process and find the worst fit block for it. 7. We use the worstFitIndex variable to keep track of the index of the worst fit block. Initially, it is set to -1. 8. For each process, we iterate over each block and check if the block size is greater than or equal to the process size. 9. If a block is found that meets the condition, we check if it is a worse fit than the current worst fit block. If so, we update worstFitIndex to the index of the current block. 10. After finding the worst fit block for a process, we check if a valid worst fit block is found (worstFitIndex != -1). 11. If a valid worst fit block is found, we update the allocation array to store the index of the worst fit block for the current process. We also update the size of the block by subtracting the process size from it. 12. Finally, we display the allocation details for each process using a loop and printf() function. 13. The program ends by returning 0 from the main() function.

This program implements the worst fit algorithm for memory allocation. It allocates each process to the block with the largest available size that can accommodate it. If no such block is available, the process is not allocated to any block. The program provides an output that shows the process number, process size, and the block number it is allocated to, or "Not Allocated" if it is not allocated to any block.