• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Pointer » Multi-Dimensional Arrays in c++
Next →
← Prev

Multi-Dimensional Arrays in c++

By Dinesh Thakur

Multi-dimensional arrays can be described as ‘arrays of arrays’, that is, each element of the array is itself an array. A multi-dimensional array of dimension n is a collection of items that are accessed with the help of n subscript values.

Generally, the arrays of three or more dimensions are not used because of their huge memory requirements and the complexities involved in their manipulation.

Two Dimensional Array: A two-dimensional array is the simplest form of a. multi-dimensional array that requires two subscript values to access an array element. two-dimensional arrays are useful when data being processed can be arranged in the form of rows and columns (matrix form). The syntax for declaring a two-dimensional array is

data_type array_name [row_size] [column_size] ;

For example, an array a of type int having three rows and two columns can be declared using this statement.

int a[3][2];

Initialization of Two-Dimensional Array: Like a single-dimensional array, a two-dimensional array can also be declared and initialized at the same time. To understand the concept of two-dimensional array initialization, consider this statement.

int a[3] [2]={{101,51},{l02,67},{l03,76} }

In this statement, an array a of type int having three rows and two columns is declared and initialized. This type of initialization is generally used to increase the readability. However, the array a can also be initialized using this statement.

int a[] [2]={101,51,l02,67,l03,76};

In this statement, the first two elements are stored in the first row, next two in the second row and so on. However, this method is least used and some of the c++ compilers may issue a warning also. Note that while initializing a two-dimensional array, mentioning the row size is optional, however, the column size must be specified.

Accessing Two-Dimensional Array Elements: Once a two-dimensional array is declared and initialized, the value stored in the array elements can be accessed using two subscripts. The syntax for accessing the two dimensional array elements is

array_name [row] [column]

The first subscript value (row) specifies the row number and the second subscript value (column) specifies the column number. Both the subscript values specify the position of the array element within the array.

For example, the elements of array a (declared earlier) are referred to as a [0] [0], a [0] [1], a [1] [0], a [1] [1], a [2] [0] and a [2] [1] respectively.

Generally, two-dimensional arrays are represented with the help of a matrix. However, in actual implementation, two-dimensional arrays are always allocated contiguous blocks of memory. Figure shows the matrix and memory representation of two-dimensional array a.

Matrix and memory representation of a

Manipulation of Two-Dimensional Array Elements: The values stored in a two-dimensional array can be manipulated in many ways. Some of the common operations that can be performed on a two-dimensional array include finding the sum of row elements, column elements and diagonal elements, finding the maximum and minimum values, etc.

To understand the concept of operations on two-dimensional array, consider this example.

Example : A program to calculate sum of two matrices

#include<iostream>
#include<iomanip> // for setw()
using namespace std;
int main () {
  int mat1 [3][3]={ {3,4,5}, // initializing elements
     {3,2,7}, // of first matrix
     {7,8,2} };
  int mat2 [3][3]={ {2,4,7}, // initializing elements
    {1,2,2}, // of second matrix
    {5,3,1} };
  int mat3 [3] [3], i, j ;
  for (i=0;i<3;i++) //calculating sum of two matrices
    for (j =0; j <3; j ++) / / and storing in third matrix
      mat3 [i] [j] =mat1 [i] [j ]+mat2[i] [j] ;
      cout<<"The resultant matrix is : \n";
      for(i=0;i<3;i++) //displaying result matrix {
        for (j =0; j <3; j ++)
          cout<<setw(4)<<mat3[i] [j];
         cout<<endl;
}
         return 0;
}

The output of the program is

The resultant matrix is

5              8              12

4              4              9

12            11            3

In this example, the elements of two matrices mat1 and mat2 are added and stored in a third matrix mat 3.

Array of Strings: An array of strings is simply a two-dimensional array of characters where each row Strings’ represents a string. An array of strings is declared and initialized in the same way as any. Two dimensional array of type int, float, etc. To understand the concept of declaring and initializing an array of strings, consider this statement.

char names [3][8]={“Nikhl“,“Raman”,"Nandita"};

In this statement, an array of strings names is declared and initialized with string constants. Note that, the first subscript (3 in this case) in the array specifies the maximum number of strings in the array and the second subscript (8 in this case) specifies the maximum length of each string including the null character.

Array_name[i];

To access a particular string in an array of strings, only one subscript is required. The syntax for accessing a string in an array of strings is where i denote string number in the array. For example, the second string (that is “Raman”) in array of strings name s is accessed by names [1]. The memory representation of the array of strings names is shown in Figure.

Memory Representation of names

To understand the concept of array of strings, consider this example.

Example : A program to reverse each string in an array of strings

#include<iostream>
#include<cstring> // for strrev() function
using namespace std;
canst int str = 3; //number of strings
canst int max=10; // maximum length of each string
int main () {
  char names [str][max]={"akshay","smith",“raman",
} ;
 for(int i=0; i<str; i++) {
   cout<<"Reverse of "<<names[i]<<"is;
   cout<<strrev(names[i])<<"\n";
   return 0;
}

The output of the program is

Reverse of akshay is yahska

Reverse of smith is htims

Reverse of raman is namar

In this example, an array of strings names is declared and initialized with string constants. Each string is accessed using names [i] and the reverse of each string is displayed using the strrev () function.

You’ll also like:

  1. PHP Arrays Numeric, Associative and Multi-Dimensional
  2. Single-Dimensional Arrays in c++
  3. C++ Declaration of One Dimensional Arrays
  4. Three-Dimensional Arrays in C
  5. Pointer to Three-Dimensional Arrays in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW