forked from reeucq/advanced-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray3d.java
152 lines (143 loc) · 5.23 KB
/
Array3d.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.Arrays;
import java.util.Scanner;
/**
* The Array3d class represents a 3d array in the form of 2D Array and provides methods to read and print the array.
*/
public class Array3d {
/** Data Members */
private int[] array;
private int rows;
private int cols;
private int depth;
/**
* Constructs a new Array3d object with the specified number of rows, columns and depth.
*
* @param rows the number of rows in the 3D array
* @param cols the number of columns in the 3D array
* @param depth the depth of the 3D array
*/
public Array3d(int rows, int cols, int depth) {
this.rows = rows;
this.cols = cols;
this.depth = depth;
array = new int[rows * cols * depth];
}
/**
* Calculates the row major index of a 3D array element based on its row, column and depth.
*
* @param i The row index of the element.
* @param j The column index of the element.
* @param k The depth index of the element.
* @return The calculated row major index of the element.
*
* How does Mapping Work?
* No. of elements in first dimension 0 to i-1 = i * cols * depth
* No. of elements in second dimension 0 to j-1 = j * depth
* No. of elements up to kth depth in the ith row and jth column: k + 1
* Minus 1 because array index starts from 0
*
* So the Final formula is: i * cols * depth + j * depth + k + 1 - 1 = i * cols * depth + j * depth + k
*/
public int rowMap(int i, int j, int k) {
return i * cols * depth + j * depth + k;
}
/**
* Calculates the column major index of a 3D array element based on its row, column and depth.
*
* @param i The row index of the element.
* @param j The column index of the element.
* @param k The depth index of the element.
* @return The calculated column major index of the element.
*
* How does Mapping Work?
* No. of elements in third dimension 0 to k-1 = k * rows * cols
* No. of elements in second dimension 0 to j-1 = j * rows
* No. of elements up to ith row in the jth column and kth depth: i + 1
* Minus 1 because array index starts from 0
*
* So the Final formula is: k * rows * cols + j * rows + i
*/
public int colMap(int i, int j, int k) {
return k * rows * cols + j * rows + i;
}
/**
* Reads the 3D array from the standard input.
*/
public void readRowMajor() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < depth; k++) {
System.out.print("Enter the element at index (" + i + ", " + j + ", " + k + "): ");
array[rowMap(i, j, k)] = sc.nextInt();
}
}
}
}
/**
* Reads the 3D array from the standard input.
*/
public void readColMajor() {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < depth; k++) {
System.out.print("Enter the element at index (" + i + ", " + j + ", " + k + "): ");
array[colMap(i, j, k)] = sc.nextInt();
}
}
}
}
/**
* Prints the 3D array in row major order.
*/
public void printRowMajor() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < depth; k++) {
System.out.print(array[rowMap(i, j, k)] + " ");
}
System.out.println();
}
System.out.println();
}
}
/**
* Prints the 3D array in column major order.
*/
public void printColMajor() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < depth; k++) {
System.out.print(array[colMap(i, j, k)] + " ");
}
System.out.println();
}
System.out.println();
}
}
/**
* The main method to test the Array3d class.
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
int cols = sc.nextInt();
System.out.print("Enter the depth: ");
int depth = sc.nextInt();
Array3d arr = new Array3d(rows, cols, depth);
Array3d arr1 = new Array3d(rows, cols, depth);
System.out.println("Enter the elements of the 3D array in row major order: ");
arr.readRowMajor();
System.out.println("The 3D array in row major order is: ");
arr.printRowMajor();
System.out.println("Actual Mapping: "+Arrays.toString(arr.array));
System.out.println("Enter the elements of the 3D array in column major order: ");
arr1.readColMajor();
System.out.println("The 3D array in column major order is: ");
arr1.printColMajor();
System.out.println("Actual Mapping: "+Arrays.toString(arr1.array));
}
}