Guest User

Untitled

a guest
Feb 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. /*
  2. JTSK-320112
  3. a2_p4.c
  4. Taiyr Begeyev
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. void readMatrices(int*** arr, int rows, int columns, int depth) {
  10.     int i, j, k;
  11.     for (i = 0; i < rows; i++) {
  12.         for (j = 0; j < columns; j++) {
  13.             for (k = 0; k < depth; k++) {
  14.                 scanf("%d", &arr[i][j][k]);
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20. void printMatrices(int*** arr, int rows, int columns, int depth) {
  21.     int i, j, k;
  22.     for (i = 0; i < depth; i++) {
  23.         printf("Section %d:\n", i + 1);
  24.         for (j = 0; j < columns; j++) {
  25.             for (k = 0; k < rows; k++) {
  26.                 printf("%d ",arr[k][j][i]);
  27.             }
  28.             printf("\n");
  29.         }
  30.     }
  31. }
  32.  
  33. int main() {
  34.     //our axes
  35.     int rows, columns, depth;
  36.     scanf("%d %d %d", &rows, &columns, &depth);
  37.  
  38.     //dynamically allocate memory for rows
  39.     int*** massive = (int***) malloc(sizeof(int**) * rows);
  40.    
  41.     //check memory validality
  42.     if (massive == NULL) {
  43.         printf("Error occured. Problems with the allocation\n");
  44.         exit(1);
  45.     }
  46.  
  47.     //dynamically allocate memory for columns and depth
  48.     //and check memory validality
  49.     int i, j;
  50.     for (i = 0; i < columns; i++) {
  51.         massive[i] = (int**) malloc(sizeof(int*) * columns);
  52.         if (massive[i] == NULL) {
  53.             printf("Error occured. Problems with allocation matrices \n");
  54.             exit(1);
  55.         }
  56.         for (j = 0; j < depth; j++) {
  57.             massive[i][j] = (int*) malloc(sizeof(int) * depth);
  58.             if (massive[i][j] == NULL) {
  59.                 printf("Error occured. Problems with allocation matrices \n");
  60.                 exit(1);
  61.             }
  62.         }
  63.     }
  64.  
  65.     //read matrix from the output
  66.     // and 2D-sections of the 3D-array which are parallel to the “XOY axis”
  67.    
  68.     readMatrices(massive, rows, columns, depth);
  69.     printMatrices(massive, rows, columns, depth);
  70.  
  71.     //deallocation
  72.     for(i = 0; i < columns; i++) {
  73.         for (j = 0; j < depth; j++) {
  74.             free(massive[i][j]);
  75.         }
  76.     }
  77.  
  78.     for (i = 0; i < columns; i++) {
  79.         free(massive[i]);
  80.     }
  81.  
  82.     free(massive);
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment