Multi 您所在的位置:网站首页 arrays Multi

Multi

2022-05-30 22:45| 来源: 网络整理| 查看: 265

Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.

how-to-work-with-multidimensional-array-in-c-programming

C allows for arrays of two or more dimensions. A two-dimensional (2D) array is an array of arrays. A three-dimensional (3D) array is an array of arrays of arrays.

In C programming an array can have two, three, or even ten or more dimensions. The maximum dimensions a C program can have depends on which compiler is being used.

More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays.

How to Declare a Multidimensional Array in C

A multidimensional array is declared using the following syntax:

type array_name[d1][d2][d3][d4]………[dn];

Where each d is a dimension, and dn is the size of final dimension.

Examples:

int table[5][5][20]; float arr[5][6][5][6][5];

In Example 1:

int designates the array type integer. table is the name of our 3D array. Our array can hold 500 integer-type elements. This number is reached by multiplying the value of each dimension. In this case: 5x5x20=500.

In Example 2:

Array arr is a five-dimensional array. It can hold 4500 floating-point elements (5x6x5x6x5=4500).

Can you see the power of declaring an array over variables? When it comes to holding multiple values in C programming, we would need to declare several variables. But a single array can hold thousands of values.

Note: For the sake of simplicity, this tutorial discusses 3D arrays only. Once you grab the logic of how the 3D array works then you can handle 4D arrays and larger.

Scroll to ContinueRead More From Owlcationbiography-of-della-street-perry-masons-more-than-secretaryBiography of Della Street, Perry Mason’s More Than Secretaryprofessor-beringers-lying-stonesProfessor Beringer's Lying Stoneshow-to-answer-the-impossible-question-on-the-edexcel-gcse-maths-paperHow to Answer the 'Impossible' Question on the Edexcel GCSE Maths Paper 2022Explanation of a 3D Array

Let's take a closer look at a 3D array. A 3D array is essentially an array of arrays of arrays: it's an array or collection of 2D arrays, and a 2D array is an array of 1D array.

It may sound a bit confusing, but don't worry. As you practice working with multidimensional arrays, you start to grasp the logic.

The diagram below may help you understand:

3D Array Conceptual View

3D Array Conceptual View

3D array memory map.

3D array memory map.

Initializing a 3D Array in C

Like any other variable or array, a 3D array can be initialized at the time of compilation. By default, in C, an uninitialized 3D array contains “garbage” values, not valid for the intended use.

Let’s see a complete example on how to initialize a 3D array:

Declaration and Initialization 3D Array#include #include void main() { int i, j, k; int arr[3][3][3]= { { {11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27, 28, 29} }, { {31, 32, 33}, {34, 35, 36}, {37, 38, 39} }, }; clrscr(); printf(":::3D Array Elements:::\n\n"); for(i=0;i

In the code above we have declared a multidimensional integer array named “arr” which can hold 3x3x3 (or 27) elements.

We have also initialized the multidimensional array with some integer values.

As I said earlier, a 3D array is an array of 2D arrays. I have divided elements accordingly for easy understanding. Looking at the C code sample above,

In lines 9-13, 14-18, and 19-23, each block is a 2D array. Collectively, lines 2-24 make a 3D array.

To call values from the array, imagine the 3D array above as a collection of tables. Each nested bracket cluster is a table with rows and columns. To access or store any element in a 3D array you need to know its table number, row number, and column number.

An example: You need to access value 25 from the above 3D array. So, first check the table: in this case, 25 is in table 1 (remember: tables, rows, columns are counted starting at 0, so the second table is table 1). Once you find the table number now check which row of that table has the value and then check the column number. So applying above logic, 25 located in table 1, row 1, and column 1, hence the address is arr[1][1][1]. Print this address and you will get the output: 25.

The Conceptual Syntax of a 3D Array in C

The conceptual syntax for 3D array is this:

data_type array_name[table][row][column];

If you want to store values in any 3D array point first to table number, then row number, and lastly to column number.

Some hypothetical examples:

arr[0][1][2] = 32;arr[1][0][1] = 49;

Storing Values in a Continuous Location Using a Loop

The pointer syntax above assigns values to a particular location of an array, but if you want to store values in multiple locations automataically then you should use a loop.

Here is an example using the for loop command:

#include #include void main() { int i, j, k, x=1; int arr[3][3][3]; clrscr(); printf(":::3D Array Elements:::\n\n"); for(i=0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有