Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void condition(){
  5.     printf("A real square matrix of order 2n is given. The numbers denote submatrices of order n\n");
  6.     printf(" -------\n");
  7.     printf("| 1 | 2 |\n");
  8.     printf(" ------- \n");
  9.     printf("| 3 | 4 |\n");
  10.     printf(" ------- \n");
  11.     printf("Get a new matrix:\n");
  12.     printf(" -------\n");
  13.     printf("| 4 | 3 |\n");
  14.     printf(" ------- \n");
  15.     printf("| 1 | 2 |\n");
  16.     printf(" -------\n");
  17. }
  18. void outputMatrix(int** mass, int size){
  19.     for (int i = 0; i < size; i++){
  20.         for (int j = 0; j < size; j++) {
  21.             printf( "%d \t", mass[i][j]);
  22.         }
  23.         printf("\n");
  24.     }
  25.  
  26. }
  27. void swap(int** mass,int size){
  28.     int temp;
  29.     int center;
  30.     center = size / 2;
  31.     for (int i = 0; i < (size - center); i++) {
  32.         for (int j = 0; j < (size - center); j++) {
  33.             temp = mass[j][i];
  34.             mass[j][i] = mass[j + center][i + center];
  35.             mass[j + center][i + center] = temp;
  36.         }
  37.     }
  38.     for (int i = size - 1; i >= center ; i--) {
  39.         for (int j = 0; j < (center); j++) {
  40.             temp = mass[j][i];
  41.             mass[j][i] = mass[j + center][i - center];
  42.             mass[j + center][i - center] = temp;
  43.         }
  44.     }
  45.     printf("The resulting matrix: \n");
  46.     outputMatrix(mass,size);
  47. }
  48. void outputInFile(int** mass,int size){
  49.     char nameOfFile[20];
  50.     puts("Enter the name of the file you want to display data in:\n");
  51.     scanf("%s",nameOfFile);
  52.     strcat(nameOfFile,".txt");
  53.     FILE* out = fopen(nameOfFile, "w+");
  54.     fprintf(out,"%s \n","The resulting matrix: ");
  55.     for(int i = 0; i < size; i++ ){
  56.         for(int j = 0; j < size; j++ ){
  57.             fprintf(out,"%d \t",mass[i][j]);
  58.             }
  59.         fprintf(out,"\n");
  60.         }
  61.     printf("Matrix successfully written to file %s",&nameOfFile);
  62.     fclose(out);
  63. }
  64. int main() {
  65.     int size;
  66.     char nameOfFile[20];
  67.     condition();
  68.     printf("Enter the name of the file from which you want to read the information: \n");
  69.     scanf("%s",nameOfFile);
  70.     strcat(nameOfFile,".txt");
  71.     FILE* in = fopen(nameOfFile, "r+");
  72.     fscanf(in,"%d", &size);
  73.     if (size % 2 == 0) {
  74.         int **mass = (int **) malloc(size * sizeof(int *));
  75.         for (int i = 0; i < size; i++) {
  76.             mass[i] = (int *) malloc(size * sizeof(int));
  77.             for (int j = 0; j < size; j++) {
  78.                 fscanf(in, "%d", &mass[i][j]);
  79.             }
  80.         }
  81.         fclose(in);
  82.         printf("The original matrix of order 2n:\n");
  83.         outputMatrix(mass, size);
  84.         swap(mass, size);
  85.         outputInFile(mass, size);
  86.     }else{
  87.         printf("Mistake! Modify the file, it should only contain numbers,\n"
  88.                " and the size of the matrix should be 2n");
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement