Advertisement
Guest User

Untitled

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