C - Input and Print Four Dimensional Array (4D Matrix) Code Example

The code for Input and Print Four Dimensional Array (4D Matrix)

#include <stdio.h>

int main(void) {
  int i, j, k, l, size;

  // 4D Array declaration
  int arr[2][2][2][2];

  // size of array
  size = 2;

  // elements input
  arr[0][0][0][0] = 1;
  arr[0][0][0][1] = 2;
  arr[0][0][1][0] = 3;
  arr[0][0][1][1] = 4;
  arr[0][1][0][0] = 5;
  arr[0][1][0][1] = 6;
  arr[0][1][1][0] = 7;
  arr[0][1][1][1] = 8;
  arr[1][0][0][0] = 9;
  arr[1][0][0][1] = 10;
  arr[1][0][1][0] = 11;
  arr[1][0][1][1] = 12;
  arr[1][1][0][0] = 13;
  arr[1][1][0][1] = 14;
  arr[1][1][1][0] = 15;
  arr[1][1][1][1] = 16;

  // Printing the array
  for (i = 0; i < size; i++) {
    for (j = 0; j < size; j++) {
      for (k = 0; k < size; k++) {
        for (l = 0; l < size; l++) {
          printf("arr[%d][%d][%d][%d] : %d ", i, j, k, l,
                 arr[i][j][k][l]);
          printf("\n");
        }
      }
    }
  }

  return (0);
}


/*
Output:
arr[0][0][0][0] : 1 
arr[0][0][0][1] : 2 
arr[0][0][1][0] : 3 
arr[0][0][1][1] : 4 
arr[0][1][0][0] : 5 
arr[0][1][0][1] : 6 
arr[0][1][1][0] : 7 
arr[0][1][1][1] : 8 
arr[1][0][0][0] : 9 
arr[1][0][0][1] : 10 
arr[1][0][1][0] : 11 
arr[1][0][1][1] : 12 
arr[1][1][0][0] : 13 
arr[1][1][0][1] : 14 
arr[1][1][1][0] : 15 
arr[1][1][1][1] : 16
*/
Code by IncludeHelp, on August 11, 2022 20:42

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.