Module-3-Multidimensional-Array
Module-3-Multidimensional-Array
MULTIDIMENSIONAL ARRAY
▪ The multi-dimensional array is that array in which data is
arranged in the form of array of arrays. The multi-dimensional
array can have as many dimensions as it required.
▪ So, two dimensional and three dimensional arrays are
commonly used.
LET US HAVE A LOOK AT THE SYNTAX:
datatype array_name [a1][a2][a3]…[an];
TWO - DIMENSIONAL ARRAYS
Syntax:
datatype arrayname[row][col];
Example:
int score[2][3];
The two-dimensional array score[r][c] is an integer data type and it can hold only an integer data with a
maximum of 6 values ( [2] X [3] ). here is the individual value of array variable score[2][3].
Page 1 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)
score[0][0] = 10
score[0][1] = 80
score[0][2] = 30
score[1][0] = 40
score[1][1] = 50
score[1][2] = 90
The first index-number of two dimensional arrays specifies the maximum row(r), while the second index-number
specifies the maximum column(c). The value of each array is in between the intersection of row and column.
int r,c,n[3][3],sum;
for(r=0;r<=2;r++)
{
for(c=0;c<=2;c++)
{
cout << "num " << r << " of " << c << ": ";
cin >> n[r][c];
}
cout << "------------------------\n";
}
for(r=0;r<=2;r++)
{
sum=0;
cout << " sum of row# " << r << ": ";
for(c=0;c<=2;c++)
{
cout << n[r][c] << ",";
sum = sum + n[r][c];
}
cout << " = " << sum << endl;
}
return 0;
Output:
num 0 of 0: 1
num 0 of 1: 2
num 0 of 2: 3
Page 2 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)
------------------------
num 1 of 0: 4
num 1 of 1: 5
num 1 of 2: 6
------------------------
num 2 of 0: 7
num 2 of 1: 8
num 2 of 2: 9
------------------------
sum of row# 0: 1,2,3, = 6
sum of row# 1: 4,5,6, = 15
sum of row# 2: 7,8,9, = 24
Initializing Arrays
Example:
in Variables;
int sum = 0;
in Arrays;
int n[5];
n[0]=1;
n[1]=10;
n[2]=15;
n[3]=20;
n[4]=11;
int n[1][4];
n[0][0]= 20;
n[0][1]= 25;
n[0][2]= 30;
n[0][3]= 35;
Page 3 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)
or if same value;
int num[100],n;
for(n=0;n<=99;n++)
{
num[n] = 0;
}
1. Explicit Array Sizing – means defining the size of an array by specifying a numerical constant within the square
bracket that explicitly specifies the size of that array.
int num[3]={2,4,6};
float ave[5] = {90.3, 75.79, 74.5, 80.1, 80.8};
char s1[5]={'h','e','l','l','o'};
char burger[12]={‘c’,’h’, ‘e’, ‘e’, ‘s’};
char s2[12] = "cheeseburger";
string str1[5]={"red","blue","yellow","green", “black”};
2. Implicit Array Sizing – the size of the array is indicated implicitly by the number of elements on the right side
of the assignment operator, which means that the square brackets are empty.
int num[]={2,4,6};
char s1[]={'h','e','l','l','o'};
char s3[]="hello world";
string str1[]={"red","blue","yellow","green"};
int num[3][2]={
{1,2},
{10,20},
{100,200}
};
Page 4 of 5
Lesson 3: Multidimensional Array CC123 ( C ) – Intermediate Programming (Advanced C++)
Prepared by:
Page 5 of 5