0% found this document useful (0 votes)
346 views12 pages

Multidimensional Arrays in C/C++

Multidimensional arrays are arrays of arrays that can be two-dimensional, three-dimensional, or higher. Data is stored in a tabular format, with elements accessed using indexing of each dimension. They are declared with data types and size specifications for each dimension, such as int array[10][20] for a two-dimensional 10x20 array. Elements are initialized in nested braces representing each dimension.

Uploaded by

Bharath Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • Memory Management,
  • Accessing Elements,
  • Data Storage,
  • Console Output,
  • Nested Arrays,
  • Nested Braces,
  • Array Operations,
  • Syntax Examples,
  • Row and Column Indexing,
  • Code Compilation
0% found this document useful (0 votes)
346 views12 pages

Multidimensional Arrays in C/C++

Multidimensional arrays are arrays of arrays that can be two-dimensional, three-dimensional, or higher. Data is stored in a tabular format, with elements accessed using indexing of each dimension. They are declared with data types and size specifications for each dimension, such as int array[10][20] for a two-dimensional 10x20 array. Elements are initialized in nested braces representing each dimension.

Uploaded by

Bharath Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • Memory Management,
  • Accessing Elements,
  • Data Storage,
  • Console Output,
  • Nested Arrays,
  • Nested Braces,
  • Array Operations,
  • Syntax Examples,
  • Row and Column Indexing,
  • Code Compilation

Multidimensional

Arrays
1
• Multidimensional Arrays are array of arrays. They can be two
dimensional, three dimensional and even more.

• Data in Multidimensional Arrays are stored in form of tables.

• General form of declaring N-dimensional arrays:


data_type array_name[size1][size2]....[sizeN];

data_type: Type of data to be stored in the array. Here


data_type is valid C/C++ data type
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions
2
• Let us see some examples
• Two dimensional array
int two[10][20];
• Three dimensional array
int three[10][20][30];

• Size of Multidimensional arrays


Total number of elements that can be stored in a
multidimensional array can be calculated by multiplying the size
of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.

Array int x[5][10][20] can store total (5*10*20) = 1000 elements. 3


• Two Dimensional Arrays
• The basic form of declaring a two-dimensional array of size x,
y:
Syntax:data_type array_name[x][y];

• We can declare a two dimensional integer array ‘x’ of size


10,20 as: int x[10][20];

• Elements in Two Dimensional Arrays are referred by x[i][j]


where i is the row number and j is the column number.

• A Two Dimensional Array can be seen as a table with x rows


and y columns where the row number ranges from 0 to (x-1)
and column number ranges from 0 to (y-1). 4
• There are two ways in which a Two-Dimensional array can
be initialized.

5
• First Method:
• int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
• The above array have 3 rows and 4 columns. The elements in
the braces from left to right are stored in the table also from
left to right. The elements will be filled in the array in the
order, first 4 elements from the left in first row, next 4
elements in second row and so on.

• Second Method:
• int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
• This type of initialization make use of nested braces. Each set
of inner braces represents one row. In the above example
there are total three rows so there are three sets of inner 6
braces.
• Accessing Elements of Two-Dimensional Arrays: Elements in
Two-Dimensional arrays are accessed using the row indexes
and column indexes.
Example:
int x[2][1];
The above example represents the element present in third row
and second column.

7
• Three Dimensional Arrays

8
• Initializing Three-Dimensional Array:
• Initialization in Three-Dimensional array is same as that of Two-
dimensional arrays. The difference is as the number of
dimension increases so the number of nested braces will also
increase.

• First Method:
• int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23};

• Second Method:
• int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }, {
{12,13,14,15}, {16,17,18,19}, {20,21,22,23} } };
9
Program to store and print values in array

#include<stdio.h>
int main(){
int array[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++){
printf("Enter value for array[%d][%d]:", i, j);
scanf("%d",array[i][j]);
}
}
10
printf("array elements:\n");
for(i=0; i<2; i++){
for(j=0;j<3;j++) {
printf("%d ", array[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}

11
Output:
Enter value for array[0][0]:1
Enter value for array[0][1]:2
Enter value for array[0][2]:3
Enter value for array[1][0]:4
Enter value for array[1][1]:5
Enter value for array[1][2]:6
array elements:
123
456

12

Common questions

Powered by AI

Increasing the dimensions of an array requires additional nested braces for initialization. In a three-dimensional array, for instance, using the second method requires sets of braces corresponding to each dimension layer, such as int x[2][3][4] = {{{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}, {{12,13,14,15}, {16,17,18,19}, {20,21,22,23}}} . This means that as the number of dimensions increases, organizing and visualizing the data becomes more complex, requiring careful attention to the alignment of braces and data distribution .

The program in Source 2 demonstrates input and output operations on a two-dimensional array by using nested loops to iterate through the array. It uses scanf() to receive input for each element by specifying the row and column indexes, ensuring data is stored correctly. For output, it prints each element using printf(), again utilizing row and column coordinates, and outputs the elements in a formatted manner . This approach of using loops allows handling of arrays efficiently, regardless of their size, highlighting the capability and flexibility of handling structured data .

Using well-structured nested braces in the initialization of multidimensional arrays enhances code readability and organization. It allows developers to clearly visualize and understand the data structure, as each set of braces can correspond to a logical division within the array. For example, in a three-dimensional array initialization such as int x[2][3][4] = {{{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}, {{12,13,14,15}, {16,17,18,19}, {20,21,22,23}}} , each pair of nested braces delineates a complete unit of the multi-layered array. This not only minimizes errors but also simplifies maintenance and modification .

The first method of initializing a two-dimensional array involves listing out all elements in a single pair of braces, with elements being filled row by row. For example, int x[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} initializes the array with 3 rows and 4 columns by filling the first 4 elements in the first row, the next 4 in the second row, and so forth . The second method uses nested braces to explicitly define each row, such as int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}, which enhances readability by showing clearly the elements of each row through separate braces .

The layout of elements in multidimensional arrays affects performance because elements are stored in a contiguous block of memory. Accessing elements in a manner that follows this memory order (e.g., row-major order for two-dimensional arrays) results in better performance due to favorable cache usage. When data access patterns do not align with this memory layout, it can lead to cache misses and decreased performance . This highlights the importance of considering memory layout when optimizing programs that use multidimensional arrays extensively .

Elements in a two-dimensional array are accessed using two indexes: the row index and the column index. For example, in an array declared as int x[10][20], an element at the intersection of the third row and second column can be accessed using x[2][1] where row indices start at 0 and column indices also start at 0 . This indexing method allows specific location targeting within the array's table-like structure .

The total number of elements in a three-dimensional array can be calculated by multiplying the sizes of all its dimensions. For instance, for the array int x[5][10][20], the total number of elements would be calculated as 5 * 10 * 20 = 1000 elements .

The use of two different initialization methods for three-dimensional arrays impacts complexity and readability significantly. The first method, which provides all elements in a sequential manner, reduces complexity at the cost of readability, as it becomes difficult to visualize the structure. The second method, which uses nested braces, greatly enhances readability by clearly delineating the hierarchical structure of the array's dimensions, aiding comprehension despite increased syntactic complexity . Ultimately, while the first method may suit simple or small arrays, the second method is preferred for clarity in larger, more complex arrays, facilitating code maintenance and collaborative development .

The declaration and initialization of multidimensional arrays provide insights into memory usage by emphasizing the allocation of contiguous memory segments for all elements. Declaring an array such as int x[10][20] in C/C++ implies a block of memory large enough to hold 200 elements of the array's data type is reserved. This contiguous allocation facilitates rapid access and cache efficiency . Moreover, understanding the size allocation through initialization helps programmers anticipate memory requirements and optimize performance in high-demand applications .

Multidimensional arrays can be conceptualized as data structures that offer a structured way to manage data in tabular or matrix form in programming. They help model and solve problems where data naturally fits into rows and columns or higher dimensions, such as in graphics, simulations, and mathematical computations. By organizing data into multiple dimensions, they simplify complex data relationships and enable efficient storage and retrieval, tapping into spatial data representation . The nested array structure extends to conceptual abstraction levels beyond mere data points to complex relationships and manipulations .

You might also like