Vla_DOS

Untitled

Jan 3rd, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4.  
  5. int sum_diagonal(int **a, int N) {
  6.     int s, i, j;
  7.     s = 0;
  8.     for (i=0; i< N; i++)
  9.         s += a[i][i];
  10.     return s;
  11. }
  12. void Input(int **a, int N){
  13.     for (int i=0; i< N; i++) {
  14.         for (int j=0; j< N; j++) {
  15.             a[i][j] = rand() % 10;
  16.         }
  17.     }
  18. }
  19. void Ouput(int **a, int N){
  20.     for (int i=0; i< N; i++) {
  21.         for (int j=0; j< N; j++) {
  22.             printf("%3d", a[i][j]);
  23.         }
  24.         printf("\n");
  25.     }
  26. }
  27. int main()
  28. {
  29.     int N = 3;
  30.     int ** A = (int**) malloc (sizeof (int*) * N);
  31.     for (int i = 0; i < N; i++) {
  32.       A[i] = (int*) malloc (sizeof (int)*N);
  33.     }
  34.     Input(A, N);
  35.     printf("Matrix A:\n");
  36.     Ouput(A, N);
  37.    
  38.     int M = 4;
  39.     int ** B = (int**) malloc (sizeof (int*) * M);
  40.     for (int i = 0; i < M; i++) {
  41.       B[i] = (int*) malloc (sizeof (int)*M);
  42.     }
  43.     Input(B, M);
  44.     printf("Matrix B:\n");
  45.  
  46.     Ouput(B, M);
  47.     printf("\nСума елементів діагоналей матриці А і В = %d", sum_diagonal(A, N) + sum_diagonal(B, M));
  48.        
  49.    
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment