Multidimensional Array | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

A multi-dimensional array is an array of arrays. 2-dimensional arrays are the most commonly used. They are used to store data in a tabular manner.
Consider the following 2D array, which is of size 3 x 5. For an array of size N×MN \times M, the rows and columns are numbered from 00 to N1N - 1 and columns are numbered from 00 to M1M - 1, respectively. Any element of the array can be accessed by arr[i][j] where 0i<N0 \leq i < N and 0j<M0 \leq j < M. For example, in the following array, the value stored at arr[1][3] is 14.
Multidimensional Array | Programming and Data Structures - Computer Science Engineering (CSE)

2D array declaration

To declare a 2D array, you must specify the following:

  • Row-size: Defines the number of rows
  • Column-size: Defines the number of columns
  • Type of array: Defines the type of elements to be stored in the array, i.e., either a number, character, or other such datatype.

A sample form of declaration is as follows:
type arr[row_size][column_size];

A sample C array is declared as follows:

c
int arr[3][5];

2D array initialization

An array can either be initialized during or after declaration.
The format of initializing an array during declaration is as follows:
type arr[row_size][column_size] = {{elements}, {elements} ... };

An example in C is given below:

c
int arr[3][5] = {{5, 12, 17, 9, 3}, {13, 4, 8, 14, 1}, {9, 6, 3, 7, 21}};

Initializing an array after declaration can be done by assigning values to each cell of the 2D array, as follows:

c
type arr[row_size][column_size]; arr[i][j] = 14;

A C example of initializing an array after declaration by assigning values to each cell of a 2D array is as follows:

c
int arr[3][5]; arr[0][0] = 5; arr[1][3] = 14;

This is quite naive and not usually used. Instead, the array elements are read from stdin.

Processing 2D arrays

The most basic form of processing is to loop over the array and print all its elements, which can be done as follows:

c
type arr[row_size][column_size] = {{elements}, {elements} ... }; for(i = 0; i < row_size; i++) for(j = 0; j < column_size; j++) print arr[i][j];

A C example of looping over the array and printing all its elements is as follows:

c
#include <stdio.h> int main() { // Array declaration and initialization int arr[3][5] = {{5, 12, 17, 9, 3}, {13, 4, 8, 14, 1}, {9, 6, 3, 7, 21}}; // Iterate over the array for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { // Print out each element printf("%d ", arr[i][j]); } // Print new line character after the row is printed in the above loop printf("\n"); } return 0; }

These methods of declaration, initialization, and processing can be extended to 3D or higher-dimensional arrays.

The document Multidimensional Array | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Multidimensional Array - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a multidimensional array in programming?
Ans. A multidimensional array is an array that contains more than one dimension, allowing you to store data in a grid-like format. For example, a two-dimensional array can be visualized as a table with rows and columns, where each element can be accessed using multiple indices.
2. How do you declare a multidimensional array in C or C++?
Ans. In C or C++, you can declare a multidimensional array by specifying the type followed by the array name and dimensions. For example, to declare a two-dimensional array of integers with 3 rows and 4 columns, you can write: `int array[3][4];`.
3. What are the common use cases of multidimensional arrays?
Ans. Multidimensional arrays are commonly used in applications like image processing, mathematical computations, and data representation where data can be organized in multiple dimensions, such as matrices or grids.
4. How can you access elements in a multidimensional array?
Ans. To access elements in a multidimensional array, you use multiple indices corresponding to each dimension. For example, in a two-dimensional array `array[i][j]`, `i` refers to the row and `j` refers to the column of the element you want to access.
5. What is the difference between a one-dimensional array and a multidimensional array?
Ans. A one-dimensional array is a linear collection of elements that can be accessed using a single index, while a multidimensional array allows for multiple indices to access elements, providing a more complex structure suitable for representing data in two or more dimensions.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Previous Year Questions with Solutions

,

Extra Questions

,

MCQs

,

Viva Questions

,

Multidimensional Array | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

,

past year papers

,

Free

,

study material

,

Summary

,

pdf

,

mock tests for examination

,

shortcuts and tricks

,

ppt

,

practice quizzes

,

Objective type Questions

,

Multidimensional Array | Programming and Data Structures - Computer Science Engineering (CSE)

,

Multidimensional Array | Programming and Data Structures - Computer Science Engineering (CSE)

,

video lectures

,

Exam

,

Sample Paper

,

Semester Notes

;