C 3D Arrays 🎯

beginner
13 min

C 3D Arrays 🎯

Welcome back to CodeYourCraft! Today, we're diving into the world of three-dimensional (3D) arrays in C. These are a natural extension of two-dimensional arrays and are incredibly useful for storing and manipulating complex data structures. Let's get started!

Understanding 3D Arrays 📝

A 3D array, also known as a triple array, is a data structure that consists of a series of arrays stored in a single unit. Each array inside the 3D array is one-dimensional and can be accessed using multiple indices.

Here's a simple representation:

markdown
index 0 index 1 index 2 index 0 | a000 | a001 | a002 | index 1 | a100 | a101 | a102 | ... | ... | ... | ... |

In this example, a000, a101, and so on, are elements of the 3D array. We can access any element by specifying its three indices.

Declaring a 3D Array 💡

To declare a 3D array in C, you first need to specify its size. Each dimension must have a different size. Here's an example:

c
int array3D[numRows][numCols][numDepth];

In this example, numRows, numCols, and numDepth are the number of elements in each dimension, respectively.

Initializing a 3D Array 📝

To initialize a 3D array, you can assign values to each element one by one, or you can use a loop to fill the array more efficiently. Here's an example of both methods:

One by One Initialization

c
int array3D[2][3][2] = { { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }, { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} } };

Loop Initialization

c
int array3D[2][3][2]; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { for (int k = 0; k < 2; ++k) { array3D[i][j][k] = i * 3 + j * 2 + k + 1; } } }

Accessing Elements of a 3D Array 💡

To access an element in a 3D array, you use all three indices, like so:

c
printf("%d\n", array3D[1][1][1]); // Output: 11

Practical Application 🎯

3D arrays can be used in various scenarios, such as storing a 3D grid, simulating a 3D world, or implementing a multi-dimensional search. Let's implement a simple 3D array usage – a 3D game of Tic-Tac-Toe!

Tic-Tac-Toe in 3D 💡

c
#include <stdio.h> #define ROWS 3 #define COLS 3 #define DEPTH 3 int board3D[ROWS][COLS][DEPTH]; int currentPlayer = 1; // 1 for 'X', 2 for 'O' void initializeBoard() { for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { for (int k = 0; k < DEPTH; ++k) { board3D[i][j][k] = 0; } } } } int checkWin(int row, int col, int depth) { for (int i = 0; i < DEPTH; ++i) { if (board3D[row][col][i] == 0) return 0; if (board3D[row][col][i] == board3D[row + 1][col][i] && board3D[row][col][i] == board3D[row + 2][col][i]) return 1; if (board3D[row][col][i] == board3D[row][col + 1][i] && board3D[row][col][i] == board3D[row][col + 2][i]) return 1; if (board3D[row][col][i] == board3D[row + 1][col + 1][i] && board3D[row + 2][col + 2][i] == board3D[row][col][i]) return 1; } return 0; } void printBoard() { printf("\n"); for (int i = 0; i < ROWS; ++i) { for (int j = 0; j < COLS; ++j) { for (int k = 0; k < DEPTH; ++k) { printf("%d ", board3D[i][j][k]); } printf("\n"); } printf("\n"); } } void placeMark(int row, int col, int depth) { board3D[row][col][depth] = currentPlayer; currentPlayer = (currentPlayer == 1) ? 2 : 1; } int main() { initializeBoard(); int row, col, depth; printf("Welcome to 3D Tic-Tac-Toe!\n"); do { printBoard(); printf("\nEnter row, column, and depth (0,0,0 to exit): "); scanf("%d %d %d", &row, &col, &depth); if (row >= 0 && row < ROWS && col >= 0 && col < COLS && depth >= 0 && depth < DEPTH) { if (board3D[row][col][depth] == 0) { placeMark(row, col, depth); if (checkWin(row, col, depth)) { printf("\nPlayer %d wins!\n", currentPlayer); } } else { printf("\nThis cell is already occupied. Try another one.\n"); } } else { printf("\nInvalid input. Try again.\n"); } } while (row >= 0 && row < ROWS && col >= 0 && col < COLS && depth >= 0 && depth < DEPTH); printf("\nGame over. Thanks for playing!\n"); return 0; }

Quiz 📝

Quick Quiz
Question 1 of 1

How would you initialize a 3D array with 5 rows, 6 columns, and 4 depths?