🎾Практика. Матрицы

#include <stdio.h>
#include <malloc.h>

double **matrix_malloc(int rows, int cols);
int scan_int(int *rows, int *cols);
void scan_value(double **matrix, int rows, int cols);
void print_value(double **matrix, int rows, int cols);
void free_memory(double **matrix, int rows);
double **square_matrix(double **matrix, int rows);
double **work_with_matrix(double **matrix,int rows,int cols);
double **not_square_matrix(double **matrix,int rows,int cols);
void swap(double *a, double *b);

int main()
{
    int rows, cols;
    if (scan_int(&rows, &cols)==1){
        double **matrix=matrix_malloc(rows, cols);
        scan_value(matrix, rows, cols);
        matrix = square_matrix(matrix, rows);
        print_value(matrix, rows, cols);
        free_memory(matrix, rows);
    }
    return 0;
}

int scan_int(int *rows, int *cols){
    int flag=1;
    printf("Please enter numbers of rows and cols:\n");
    scanf_s("%d %d", rows, cols);
    while (*rows<=0 || *cols<=0){
        flag=0;
        printf("Error. Enter numbers again.");
        scanf("%d %d", rows, cols);
    }
    return flag;
}

double **matrix_malloc(int rows, int cols){
    double **arr_p=(double **) malloc (rows*sizeof(double*));
    if (arr_p!=NULL){
        for (int i=0; i< rows;i++){
            *(arr_p+i)=(double *) malloc (cols*sizeof(double));
            if (*(arr_p+i)==NULL){
              free_memory(arr_p, rows);
              arr_p=NULL;
              printf("Memory allocation");
              break;
            }
          }
    } else{
        printf("Memory allocation");
    }
    return arr_p;
}

void free_memory(double **matrix, int rows){
    for (int i=0; i<rows;i++){
          free(*(matrix+i));
    }
    free(matrix);
}

void swap(double *a, double *b){
    double c=0;
    c=*a;
    *a=*b;
    *b=c;
}

double **square_matrix(double **matrix, int rows){
    for (int i=0;i<rows-1;i++){
        for (int j=0;j<rows-1-i;j++){
            swap(*(matrix+i)+j, *(matrix+rows-1-j)+rows-1-i);
        }
    }
    return matrix;
}

void scan_value(double **matrix, int rows, int cols){
    printf("lets go, bitches\n");
    for (int i=0;i<rows;++i){
        for (int j=0;j<cols;++j){
            scanf_s("%lf", *(matrix+i)+j);
        }
    }
}

void print_value(double **matrix, int rows, int cols){
    printf("Values of matrix:\n");
    for (int i=0;i<rows;++i){
        for (int j=0;j<cols;++j){
            printf_s("%lf\t", *(*(matrix+i)+j));
        }
        printf("\n");
    }
}

Last updated