Advertisement
Guest User

Untitled

a guest
Oct 29th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define lim 21
  4.  
  5. void clear(){
  6.   char ch='q';
  7.   while (ch!=' '){
  8.     ch = getchar();
  9.     if (ch=='\n') break;
  10.   }
  11. }
  12.  
  13.  
  14.  
  15. void printfMatr(double **matr,int n){
  16.     for(int i=0;i<n;i++){
  17.         printf("\n");
  18.         for (int j=0;j<n;j++){
  19.             printf("%8.2lf \t",matr[i][j]);
  20.         }
  21.     }  
  22.     printf("\n");
  23. }
  24.  
  25. int main(){
  26.     printf("Введите размер квадратного массива:\n ");
  27.     int i,n,j,k;
  28.    double **A,**B,**C,test,tmp,m,q;
  29.     test = scanf("%d", &n);
  30.      if ( n >=lim || n<1) test = 0;
  31.     while (test != 1) {
  32.       clear();
  33.       printf("Введите размер квадратного массива :\n");
  34.       test = scanf("%d", &n);
  35.       if ( n >= lim || n<1) test = 0;
  36.      }
  37.       clear();
  38.  
  39.    A =(double**)malloc(sizeof (double*)*n);
  40.     for( i=0;i<n;i++){
  41.         A[i]=(double*)malloc(sizeof(double)*n);
  42.     }
  43.    B =(double**)malloc(sizeof(double*)*n);
  44.     for( i=0;i<n;i++){
  45.         B[i]=(double*)malloc(sizeof(double)*n);
  46.     }
  47.    C =(double**)malloc(sizeof(double*)*n);
  48.     for( i=0;i<n;i++){
  49.         C[i]=(double*)malloc(sizeof(double)*n);
  50.     }
  51.  
  52.     printf("Введите элементы матрицы\n");
  53.  
  54.    for (i = 0; i < n; i++)                                     
  55.     {
  56.         for (j = 0; j < n; j++)
  57.         {
  58.             test = scanf("%lf", &A[i][j]);
  59.             while (test != 1) {
  60.                 clear();
  61.                 test = scanf("%lf", &A[i][j]);
  62.             }
  63.             clear();
  64.         }
  65.     }  
  66.  
  67.     printf("Вывод элементов матрицы\n");
  68.  
  69.     printfMatr(A,n);
  70.    
  71.      for(int i=0;i<n;i++){
  72.         for (int j=0;j<n;j++){
  73.             B[i][j]=A[i][j];
  74.         }
  75.      }
  76.  
  77.  
  78.    
  79.      
  80.    
  81.    
  82.     for (int i=0; i<n; i++)
  83.         for (int j=0; j < n/2; j++)
  84.         {
  85.             float tmp = B[i][j];
  86.             B[i][j] = B[i][n-j-1];
  87.             B[i][n-j-1] = tmp;
  88.         }
  89.  
  90.     for(i = 0; i < n; i++){
  91.     for(j = 0; j < n; j++)
  92.     {
  93.         C[i][j] = 0;
  94.         for(k = 0; k < n; k++){
  95.         C[i][j] += A[i][k] * B[k][j];
  96.  
  97.         }
  98.     }
  99.     }
  100.    
  101.  
  102.  
  103.         printf("Вывод элементов матриц B и C:\n");
  104.  
  105.     printfMatr(B,n);
  106.    
  107.     printfMatr(C,n);
  108.  
  109.      for( i=0;i<n;i++){
  110.         free(A[i]);
  111.     free(A);
  112.      }
  113.  
  114.      for( i=0;i<n;i++){
  115.         free(B[i]);
  116.     free(B);
  117.      }
  118.  
  119.      for( i=0;i<n;i++){
  120.         free(C[i]);
  121.     free(C);
  122.      }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement