Загрузка данных


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

#define MAX_SIZE 100

void input_matrix_static(int rows, int cols, int matrix[MAX_SIZE][MAX_SIZE]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void print_matrix(int rows, int cols, int **matrix) {
    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 method, rows, cols;
    
    if (scanf("%d", &method) != 1 || method < 1 || method > 4) {
        printf("n/a");
        return 0;
    }
    
    if (scanf("%d %d", &rows, &cols) != 2 || rows <= 0 || cols <= 0) {
        printf("n/a");
        return 0;
    }
    
    if (method == 1) {
        // Статическое выделение
        if (rows > MAX_SIZE || cols > MAX_SIZE) {
            printf("n/a");
            return 0;
        }
        int matrix[MAX_SIZE][MAX_SIZE];
        input_matrix_static(rows, cols, matrix);
        
        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");
        }
    } else if (method == 2) {
        // Динамический способ 1: массив указателей на строки
        int **matrix = (int**)malloc(rows * sizeof(int*));
        if (matrix == NULL) {
            printf("n/a");
            return 0;
        }
        for (int i = 0; i < rows; i++) {
            matrix[i] = (int*)malloc(cols * sizeof(int));
            if (matrix[i] == NULL) {
                for (int j = 0; j < i; j++) free(matrix[j]);
                free(matrix);
                printf("n/a");
                return 0;
            }
        }
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                scanf("%d", &matrix[i][j]);
            }
        }
        
        print_matrix(rows, cols, matrix);
        
        for (int i = 0; i < rows; i++) free(matrix[i]);
        free(matrix);
    } else if (method == 3) {
        // Динамический способ 2: одномерный массив с индексной арифметикой
        int *matrix = (int*)malloc(rows * cols * sizeof(int));
        if (matrix == NULL) {
            printf("n/a");
            return 0;
        }
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                scanf("%d", &matrix[i * cols + j]);
            }
        }
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                printf("%d", matrix[i * cols + j]);
                if (j < cols - 1) printf(" ");
            }
            if (i < rows - 1) printf("\n");
        }
        
        free(matrix);
    } else if (method == 4) {
        // Динамический способ 3: массив указателей на строки + одна область памяти
        int **matrix = (int**)malloc(rows * sizeof(int*));
        if (matrix == NULL) {
            printf("n/a");
            return 0;
        }
        int *data = (int*)malloc(rows * cols * sizeof(int));
        if (data == NULL) {
            free(matrix);
            printf("n/a");
            return 0;
        }
        for (int i = 0; i < rows; i++) {
            matrix[i] = data + i * cols;
        }
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                scanf("%d", &matrix[i][j]);
            }
        }
        
        print_matrix(rows, cols, matrix);
        
        free(data);
        free(matrix);
    }
    
    return 0;
}