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 following 2D array, which is of the size 3 x 5. For an array of size N x M , the rows and columns are numbered from θ to N - 1 and columns are numbered from θ to M - 1, respectively. Any element of the array can be accessed byarr[i][j]  where θ < i < N and θ < j M. For example, in the following array, the value stored at  is 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:
int arr[3][5];

  1. 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:
    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 2D array, as follows.
    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:
    int arr[3][5];
    arr[θ][θ] = 5;
    arr[1][3] = 14;
    This is quite naive and not usually used. Instead, the array elements are read from STDIN.
  2. 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:
    type arr[row_size][column_size] = {{elements}, {elements} ... }
    for i from θ to row_size
        for j from θ to column_size
            print arr[i][j]
    A C++ example of looping over the array and printing all its elements is as follows:
    #include <iostream>
    using namespace std;
    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=θ; i<3; i++)
        {
            for(int j=θ; j<5; j++)
            {
                // Print out each element
                cout << arr[i][j];
            }
            // Print new line character after the row is printed in above loop
            cout << endl;
        }
        return θ;
    }
    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 a data structure that can store values in multiple dimensions, such as rows and columns. It is essentially an array of arrays, where each element can be accessed using multiple indices.
2. How are multidimensional arrays represented in memory?
Ans. Multidimensional arrays are typically represented as a contiguous block of memory, with elements arranged in a specific order based on the dimensions. For example, a two-dimensional array can be represented as a linear sequence of values, with each row stored one after the other.
3. What are the advantages of using multidimensional arrays?
Ans. Multidimensional arrays provide a way to organize and manipulate data in multiple dimensions, making it easier to represent complex structures. They are particularly useful for tasks such as storing matrices, images, or tabular data where values have multiple dimensions.
4. How can multidimensional arrays be accessed and modified in programming languages?
Ans. Multidimensional arrays can be accessed and modified using multiple indices. For example, in a two-dimensional array, the element at a specific row and column can be accessed using array[row][column]. Values can be assigned to these elements in a similar manner.
5. Can multidimensional arrays have different sizes for each dimension?
Ans. Yes, multidimensional arrays can have different sizes for each dimension. This allows for more flexibility in representing data structures with varying dimensions. For example, a three-dimensional array can have different lengths for each dimension, such as [3][4][2], representing a 3D grid with varying sizes along each axis.
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

Sample Paper

,

Semester Notes

,

video lectures

,

Exam

,

pdf

,

Summary

,

past year papers

,

Extra Questions

,

shortcuts and tricks

,

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

,

Important questions

,

Previous Year Questions with Solutions

,

MCQs

,

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

,

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

,

study material

,

practice quizzes

,

Objective type Questions

,

Viva Questions

,

Free

,

mock tests for examination

,

ppt

;