Two Dimensional Arrays
Two Dimensional Arrays
Two-Dimensional Arrays
A 2-dimensional array is an array of arrays. In other words, it is an array where each member of
the array is also an array.
Two-dimensional array is made of rows and columns. Each column represents one category of
data that everyone of the rows shares with the other rows. To declare it, use double pair of a
opening and closing square brackets.
Data_Type NameOfArray[ROWS][COLUMNS];
int TwoDArray[5][5];
1 / 11
Two Dimensional Arrays
You can initialize an array the same way you would proceed the a one-dimensional array:
simply provide a list of values in the curly brackets.
A multidimensional array is represented as an algebraic matrix as MxN. This means that the
array is made of M rows and N columns. Total number of elements of a multidimensional array
can be calculated by multiply the number of rows by the number of columns. Therefore a 2x3
array contains 2*3=6 elements.
Based on this, when initializing a 2-dimensional array, make sure you provide a number of
values that is less than or equal to the total number of elements.
Here is an example:
To locate a member of the array, this time, each must be identified by its double index. The first
member is indexed at [0][0]. The second is at [0][1]. For a 2x3 array as this one, the 5th member
is at [1][1]. You can use this same approach to display the values of the members of the array.
Here is an example:
#include <iostream.h>
int main()
{
// A 2-Dimensional array
2 / 11
Two Dimensional Arrays
Output:
int items[2][3] = {
3 / 11
Two Dimensional Arrays
{ 1,2,3},{4,5,6}
};
#include <iostream.h>
int main()
{
// A 2-Dimensional array
int items[2][3] = {
{ 1,2,3},
{ 4,5,6}
};
#include <iostream.h>
#define N 3
#define M 3
4 / 11
Two Dimensional Arrays
int main()
{
int a1[N][M],s;
int i,j;
cout<<"Enter Elements of Array";
for(i=0;i<N;i++)
for(j=0;j<M;j++)
cin>>a1[i][j];
// Now we will find sum of diagonal elements
for(i=0;i<N;i++)
for(j=0;j<M;j++)
{
if(i==j)
s=s+ a1[i][j];
}
cout<<endl<<"Sum of diagonal elements of a1 and a2 is "<<s;
return 0;
}
#include <iostream.h>
#define N 3
#define M 3
int main()
5 / 11
Two Dimensional Arrays
for(i=0;i<N;i++)
for(j=0;j<M;j++)
cin>>a1[i][j];
for(i=0;i<N;i++)
for(j=0;j<M;j++)
{
if(max<a1[i][j])
6 / 11
Two Dimensional Arrays
max=a1[i][j];
}
return 0;
}
Finding Minimum element in MXN Array
#include <iostream.h>
#define N 3
#define M 3
int main()
{
int a1[N][M];
7 / 11
Two Dimensional Arrays
for(i=0;i<N;i++)
for(j=0;j<M;j++)
cin>>a1[i][j];
for(i=0;i<N;i++)
for(j=0;j<M;j++)
{ if(min>a1[i][j])
min=a1[i][j];
8 / 11
Two Dimensional Arrays
}
cin>>min;
return 0;
Because strings are in fact sequences of characters, we can represent them also as plain
arrays of char elements.
char Ayan[20];
is an array that can store up to 20 elements of type char. In this array, we can store sequences
of characters up to 20 characters long. But we can also store shorter sequences. For example,
Ayan could store at some point in a program either the sequence "Hello" or the sequence
"Happy Diwali", since both are shorter than 20 characters.
Therefore, since the array of characters can store shorter sequences than its total length, a
9 / 11
Two Dimensional Arrays
special character is used to signal the end of the valid sequence: the null character, whose
literal constant can be written as ' ' (backslash, zero).
Our array of 20 elements of type char, called Ayan, can be represented storing the characters
sequences "Hello" and "Happy Diwali" as:
Notice how after the valid content a null character (' ') has been included in order to indicate
the end of the sequence.
In this case we would have declared an array of 6 elements of type char initialized with the
characters that form the word "Hello" plus a null character ' ' at the end.
Arrays of char elements have an additional method to initialize their values: using string
literals.String literals enclosed between double quotes always have a null character (' ')
automatically appended at the end by the compiler. Therefore we can initialize the array of char
elements called Ayan with a null-terminated sequence of characters by either one of these two
methods:
In both cases the array of characters Ayan is declared with a size of 6 elements of type char:
the 5 characters that compose the word "Hello" plus a final null character (' ') which specifies
the end of the sequence and that, in the second case, when using double quotes (") it is
10 / 11
Two Dimensional Arrays
appended automatically.
Null-terminated sequences of characters are the natural way of treating strings in C++.For
example, cin and cout support null-terminated sequences as valid containers for sequences of
characters, so they can be used directly to extract strings of characters from cin or to insert
them into cout. For example:
As you can see, we have declared three arrays of char elements. The first two were initialized
with string literal constants, while the third one was left uninitialized. In the first two arrays the
size was implicitly defined by the length of the literal constant they were initialized to. While for
name we have explicitly specified that it has a size of 80 chars.
11 / 11