Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://vk.com/evgenykravchenko0
- ___ ___ ___
- / /\ ___ / /\ / /\
- / /:/_ /__/\ / /:/_ / /:/_
- / /:/ /\ \ \:\ / /:/ /\ / /:/ /\
- / /:/ /:/_ \ \:\ / /:/_/::\ / /:/ /:/_
- /__/:/ /:/ /\ ___ \__\:\ /__/:/__\/\:\ /__/:/ /:/ /\
- \ \:\/:/ /:/ /__/\ | |:| \ \:\ /~~/:/ \ \:\/:/ /:/
- \ \::/ /:/ \ \:\| |:| \ \:\ /:/ \ \::/ /:/
- \ \:\/:/ \ \:\__|:| \ \:\/:/ \ \:\/:/
- \ \::/ \__\::::/ \ \::/ \ \::/
- \__\/ ~~~~ \__\/ \__\/
- ___
- /__/\ ___ ___
- \ \:\ / /\ / /\
- \ \:\ / /:/ / /:/
- _____\__\:\ /__/::\ /__/::\
- /__/::::::::\ \__\/\:\__ \__\/\:\__
- \ \:\~~\~~\/ \ \:\/\ \ \:\/\
- \ \:\ ~~~ \__\::/ \__\::/
- \ \:\ /__/:/ /__/:/
- \ \:\ \__\/ \__\/
- \__\/
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- #include<math.h>
- #define A_elem *(*(a + size * i) + j)
- #define B_elem *(*(b + size * i) + j)
- #define C_elem *(*(c + size * i) + j)
- #define E_elem *(*(e + size * i) + j)
- int size = 0;
- int **a = NULL;
- int **b = NULL;
- int **c = NULL;
- int **e = NULL;
- int read_matrix ( char *input_name);
- int print_matrix ( int **matrix, int size);
- int calc_B ( int size);
- int calc_E ( int size);
- int calc_C ( int **matrix_A, int **matrix_B, int **matrix_E, int size);
- int write_matrix ( char *output_name, int **matrix_C, int size);
- int main()
- {
- char *input_name;
- char *output_name;
- input_name = (char*)malloc(20 * sizeof(char));
- output_name = (char*)malloc(20 * sizeof(char));
- printf("Enter name of file with matrix A : ");
- scanf("%s", input_name);
- read_matrix(input_name);
- print_matrix(a, size);
- calc_B(size);
- print_matrix(b, size);
- calc_E(size);
- print_matrix(e, size);
- calc_C(a, b, e, size);
- print_matrix(c, size);
- printf("\n Enter name of output file to write result : ");
- scanf("%s", output_name);
- write_matrix(output_name, c, size);
- return 0;
- }
- int read_matrix ( char *input_name)
- {
- FILE *file;
- file = fopen(input_name, "r");
- if (file == NULL)
- {
- printf("Can't create file \n");
- return 1;
- }
- else
- {
- printf("File %s successfully opened or created. \n", input_name);
- printf("Program calc matrix sum : C = 2A + B + 2E ,where E is the identity matrix \n");
- fscanf(file, " %d ", &size);
- a = (int**)malloc(size * sizeof(int*));
- printf("\n ** Matrix A ** \n");
- for ( int i = 0; i < size; i++)
- {
- *(a + size * i) = (int*)malloc(size * sizeof(int));
- for ( int j = 0; j < size; j++)
- {
- fscanf(file, "%d", (*(a + size * i) + j));
- }
- }
- }
- return 0;
- }
- int print_matrix ( int **matrix, int size)
- {
- for ( int i = 0; i < size; i++)
- {
- for ( int j = 0; j < size; j++)
- {
- printf(" %5d ", *(*(matrix + size * i) + j));
- }
- printf("\n");
- }
- return 0;
- }
- int calc_B ( int size)
- {
- b = (int**)malloc(size * sizeof(int*));
- printf("\n ** Matrix B ** \n");
- for ( int i = 0; i < size; i++)
- {
- *(b + size * i) = (int*)malloc(size * sizeof(int));
- for ( int j = 0; j < size; j++)
- {
- B_elem = (i + j) / (2 + (i - j) * (i - j));
- }
- }
- return 0;
- }
- int calc_E ( int size)
- {
- e = (int**)malloc(size * sizeof(int*));
- printf("\n ** Matrix E ** \n");
- for ( int i = 0, j = 0; i < size, j < size; i++, j++)
- {
- *(e + size * i) = (int*)malloc(size * sizeof(int));
- E_elem = 1;
- }
- return 0;
- }
- int calc_C ( int **matrix_A, int **matrix_B, int **matrix_E, int size)
- {
- c = (int**)malloc(size * sizeof(int*));
- printf("\n ** Matrix C ** \n");
- for ( int i = 0; i < size; i++)
- {
- *(c + size * i) = (int*)malloc(size * sizeof(int));
- for ( int j = 0; j < size; j++)
- {
- C_elem = 2 * A_elem + B_elem + 2 * E_elem;
- }
- }
- return 0;
- }
- int write_matrix ( char *output_name, int **matrix_C, int size)
- {
- FILE *resfile;
- resfile = fopen(output_name, "w");
- if (resfile == NULL)
- {
- printf("\n Can't create file \n");
- return 1;
- }
- else
- {
- printf("\n File %s successfully created", output_name);
- for ( int i = 0; i < size; i++)
- {
- for ( int j = 0; j < size; j++)
- {
- fprintf(resfile, "%5d ", C_elem);
- }
- fprintf(resfile, "\n");
- }
- printf("\n Result successfully recorded \n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement