Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
3.3 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

void printMatrix(int **matrix, int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d", matrix[i][j]);
            if (j < cols - 1) printf(" ");
        }
        if (i < rows - 1) printf("\n");
    }
}

int main() {
    int choice, rows, cols;
    printf("Select memory allocation method (1 - Static, 2 - Dynamic 1D, 3 - Dynamic 2D, 4 - Dynamic jagged): ");
    scanf("%d", &choice);
    if (choice < 1 || choice > 4) {
        printf("n/a\n");
        return 1;
    }

    printf("Enter number of rows and columns: ");
    scanf("%d %d", &rows, &cols);
    if (rows < 1 || cols < 1 || rows > MAX_SIZE || cols > MAX_SIZE) {
        printf("n/a\n");
        return 1;
    }

    int staticMatrix[MAX_SIZE][MAX_SIZE];
    int *dynamicMatrix1D = NULL;
    int **dynamicMatrix2D = NULL;
    int **jaggedMatrix = NULL;

    switch (choice) {
        case 1: // Static allocation
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                    scanf("%d", &staticMatrix[i][j]);
            break;
        case 2: // Dynamic 1D array
            dynamicMatrix1D = (int *)malloc(rows * cols * sizeof(int));
            if (!dynamicMatrix1D) {
                printf("n/a\n");
                return 1;
            }
            for (int i = 0; i < rows * cols; i++)
                scanf("%d", &dynamicMatrix1D[i]);
            break;
        case 3: // Dynamic 2D array
            dynamicMatrix2D = (int **)malloc(rows * sizeof(int *));
            if (!dynamicMatrix2D) {
                printf("n/a\n");
                return 1;
            }
            for (int i = 0; i < rows; i++) {
                dynamicMatrix2D[i] = (int *)malloc(cols * sizeof(int));
                if (!dynamicMatrix2D[i]) {
                    printf("n/a\n");
                    return 1;
                }
                for (int j = 0; j < cols; j++)
                    scanf("%d", &dynamicMatrix2D[i][j]);
            }
            break;
        case 4: // Dynamic jagged array
            jaggedMatrix = (int **)malloc(rows * sizeof(int *));
            if (!jaggedMatrix) {
                printf("n/a\n");
                return 1;
            }
            for (int i = 0; i < rows; i++) {
                jaggedMatrix[i] = (int *)malloc(cols * sizeof(int));
                if (!jaggedMatrix[i]) {
                    printf("n/a\n");
                    return 1;
                }
                for (int j = 0; j < cols; j++)
                    scanf("%d", &jaggedMatrix[i][j]);
            }
            break;
    }

    // Output
    switch (choice) {
        case 1:
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    printf("%d", staticMatrix[i][j]);
                    if (j < cols - 1) printf(" ");
                }
                if (i < rows - 1) printf("\n");
            }
            break;
        case 2:
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    printf("%d", dynamicMatrix1D[i * cols + j]);
                    if (j < cols - 1) printf(" ");
                }
                if (i < rows - 1) printf("\
Leave a Comment