0% found this document useful (0 votes)
12 views7 pages

Understanding Two-Dimensional Arrays

A two-dimensional array, also known as a matrix, is defined by its rows and columns and can be declared using the syntax data_type array_name[row_size][column_size]. These arrays can be initialized in various ways, including row-major and column-major order, and can represent matrices in memory. Additionally, the document provides examples of matrix addition and multiplication using C programming.

Uploaded by

Bidisha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Understanding Two-Dimensional Arrays

A two-dimensional array, also known as a matrix, is defined by its rows and columns and can be declared using the syntax data_type array_name[row_size][column_size]. These arrays can be initialized in various ways, including row-major and column-major order, and can represent matrices in memory. Additionally, the document provides examples of matrix addition and multiplication using C programming.

Uploaded by

Bidisha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Two Dimensional (2D) Array

An array consisting of two subscripts is known as two-dimensional array. These are


often known as array of the array. In two dimensional arrays the array is divided into
rows and columns. These are well suited to handle a table of data.
A two dimensional array is also known as a matrix.
In 2-D array we can declare an array as :
Declaration:-
data_type array_name[row_size][column_size];
Ex:- int arr[3][3];
where first index value shows the number of the rows and second index value shows the number
of the columns in the array.
Initializing two-dimensional arrays:
Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their
declaration with a list of initial values enclosed in braces.
Ex: int a[2][3] = {0,0,0,1,1,1};
initializes the elements of the first row to zero and the secondrow to one. The initialization
is done row by row.
The above statement can also be written as
int a[2][3] = {{ 0,0,0},{1,1,1}};
int arr[4][3] = {{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
by surrounding the elements of each row by braces.
We can also initialize a two-dimensional array in the form of a matrix as shown below
int a[2][3]= {
{0,0,0},
{1,1,1}
};
When the array is completely initialized with all values, explicitly we need not specify the
size ofthe first dimension.
Ex: int a[][3]= {
{0,2,3},
{2,1,2}
};
If the values are missing in an initializer, they are automatically set
to [Link]: int a[2][3]={
{1,1},
{2}
};
Will initialize the first two elements of the first row to one, the first element of the
second row totwo and all other elements to zero.

Explain how two dimensional arrays can be used to represent matrices. (or)

Define an array and how the memory is allocated for a 2D array?

Ans: These are stored in the memory as given below.

 Row-Major order Implementation


 Column-Major order Implementation

In Row-Major Implementation of the arrays, the arrays are stored in the memory in
terms of the row design, i.e. first the first row of the array is stored in the memory then
second and so on. Suppose we have an array named arr having 3 rows and 3 columns
then it can be stored in the memory in the following manner:

int arr[3][3];

arr[0][0] arr[0][1] arr[0][2]

arr[1][0] arr[1][1] arr[1][2]

arr[2][0] arr[2][1] arr[2][2]

Thus an array of 3*3 can be declared as follows :


arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
and it will be represented in the memory with row major implementation as follows :

1 2 3 4 5 6 7 8 9

In Column-Major Implementationof the arrays, the arrays are stored in the memory
in the term of the column design, i.e. the first column of the array is stored in the memory
then the second and so on. By taking above eg. we can show it as follows :

arr[3][3] = { 1, 2, 3,
4, 5, 6,
7, 8, 9 };
and it will be represented in the memory with column major implementation as follows :

1 4 7 2 5 8 3 6 9
Two-dimensional arrays of variable length

In 2-D array we can declare an array as :

Declaration:-

Syntax: data_type array_name[row_size][column_size];

Ex: int arr[3][3] ;

Where first index value shows the number of the rows and second index value shows
the no. of the columns in the array.
These are stored in the memory as given below.

arr[0][0] arr[0][1] arr[0][2]

arr[1][0] arr[1][1] arr[1][2]

arr[2][0] arr[2][1] arr[2][2]

Initialization :-

To initialize values for variable length arrays we can use scanf statement and loop
constructs.

Ex:-

for (i=0; i<3; i++)

for(j=0; j<3; j++)

scanf(“%d”, &arr[i][j]);

Define multi-dimensional arrays? How to declare multi-dimensional arrays?

Ans: Multidimensional arrays are often known as array of the arrays. In


multidimensional arrays the array is divided into rows and columns, mainly while
considering multidimensional arrays we will be discussing mainly about two
dimensional arrays and a bit about three dimensional arrays.

Syntax: data_type array_name[size1][size2][size3] ----- [sizeN];


In 2-D array we can declare an array as :

int arr[3][3] = { 1, 2, 3,
4, 5, 6,
7, 8, 9
};
where first index value shows the number of the rows and second index value shows
the numberof the columns in the array.
In 3-D we can declare the array in the following manner:

int arr[3][3][3] =
{ 1, 2, 3,
4, 5, 6,
7, 8, 9,

10, 11, 12,


13, 14, 15,
16, 17, 18,

19, 20, 21,


22, 23, 24,
25, 26, 27
};
/* here we have divided array into grid for sake of convenience as in above
declaration we havecreated 3 different grids, each have rows and columns */

If we want to access the element the in 3-D array we can do it as follows :

printf("%d", a[2][2][2]);
/* its output will be 26, as a[2][2][2] means first value in [] corresponds to the grid no.
i.e. 3 and
the second value in [] means third row in the corresponding grid and last [] means third
column
*/
Ex:
int arr[3][5][12];

float table[5][4][5][3];

arr is 3D array declared to contain 180 (3*5*12) int type elements. Similarly table is a
4D arraycontaining 300 elements of float type.
Write a program to perform matrix addition.

#include<stdio.h>

void main()
{
int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q;
printf("\n Enter the size of Matrix A:");
scanf("%d%d", &m,&n);
printf("\n Enter the size of Matrix B:");
scanf("%d%d", &p,&q);
if(m!=p || n!=q)
{
printf("Matrix addition not possible.");
}
else
{
printf(" Enter the Matrix A values:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf(" Enter the Matrix B values:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);

for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];

printf("\n The Matrix A is\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}

printf("\n The Matrix B is\n");


for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf(" %d",b[i][j]);
}
printf("\n");
}
printf("\n The Resultant Matrix C is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",c[i][j]);
}
printf("\n");
}

}
}

OUTPUT:
Enter the size of Matrix A:2 3
Enter the size of Matrix B:2 3
Enter the Matrix A values:1
2
3
4
5
6

Enter the Matrix B values:6


5
4
3
2
1

The Matrix A is
123
456
The Matrix B is
654
321
The Output Matrix C is
777
777
Write a program to perform matrix multiplication

#include<stdio.h>
void main()
{
int a[10][10], b[10][10], c[10][10], i, j, k, m1, n1, m2, n2;
printf("\ Enter the size of Matrix A:");
scanf("%d%d", &m1,&n1);
printf("\ Enter the size of Matrix B:");
scanf("%d%d", &m2,&n2);
if(n1!=m2)
{
printf("Matrix Multiplication not possible.");
}
else
{
printf(" Enter the Matrix A values:\n");
for(i=0; i<m1; i++)
for(j=0; j<n1; j++)
scanf("%d", &a[i][j]);
printf(" Enter the Matrix B values:\n");
for(i=0; i<m2; i++)
for(j=0; j<n2; j++)
scanf("%d", &b[i][j]);

//Matrix Multiplication Code

for(i=0; i<m1; i++)


{
for(j=0; j<n2; j++)
{
c[i][j]=0;
for(k=0; k<n1; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}

printf("\n The Matrix A is\n");


for(i=0; i<m1; i++)
{
for(j=0; j<n1; j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
printf("\n The Matrix B is\n");
for(i=0; i<m2; i++)
{
for(j=0; j<n2; j++)
{
printf(" %d", b[i][j]);
}
printf("\n");
}
printf("\n The Resultant Matrix is\n");
for(i=0; i<m1; i++)
{
for(j=0; j<n2; j++)
{
printf(" %d", c[i][j]);
}
printf("\n");
}
}
}

You might also like