2 D A R R AY S
2-dimensional Array
It is an ordered table of homogeneous
elements.
It is generally referred to as matrix, of
some rows and some columns.
It is also called as a two-subscripted
variable.
For example
int marks[5][3];
float matrix[3][3];
char page[25][80];
2 dimensional Arrays
Declaration
type array_name[row_size]
[column_size];
For example,
int arr [3][5];
arr represents a two dimensional array or table
having 3 rows and 5 columns and it can store 15
integer values.
An array int mark[5][4] is represented as follows
Student
Tests1 [0] Test 2 [1] Test 3 [2] Test 4 [3]
[subscript]
20 (mark[0] 20 (mark[0] 21 (mark[0] 22 (mark[0]
1 [0]
[0]) [1]) [2]) [3])
18 (mark[1] 23 (mark[1] 22 (mark[1] 20 (mark[2]
2 [1]
[0]) [1]) [2]) [3])
11 (mark[2] 22 (mark[2] 15 (mark[2] 16 (mark[2]
3 [2]
[0]) [1]) [2]) [3])
22 (mark[3] 21 (mark[3] 23 (mark[3] 24 (mark[3]
4 [3]
[0]) [1]) [2]) [3])
17 (mark[4] 15 (mark[4] 16 (mark[4] 18 (mark[4]
5 [4]
[0]) [1]) [2]) [3])
2 Dimensional Arrays: Initialization
type array-name [row size] [col size ] ={list of values};
int table [2][3]={0,0,0,1,1,1};
initializes the elements of the first row to zero and the
second row to 1 (bydefault)
Initialization is always done row by row.
OR
int table [2][3]={{0,0,0},{1,1,1}};
OR
int table [2][3]= { {0,0,0},
{1,1,1} };
2 Dimensional Arrays: Initialization
• When array is completely initialized with all values,
need not necessarily specify the first dimension.
int table [][3]= { {0,0,0},
{1,1,1 }
};
• If the values are missing in an initializer, they are set
to zero
int table [2][3]= { {1,1},
{2} };
• To set all elements to zero
int table [3][3]={{0},{0},{0}};
• int table [2][] Not Valid
Row Major and Column Major Reprsentation
• When it comes to organizing and accessing elements in a multi-
dimensional array, two prevalent methods are Row Major Order
and Column Major Order.
• These approaches define how elements are stored in memory and
impact the efficiency of data access in computing.
Row Major:
The elements of an array are stored in a Row-Wise fashion. he elements of
the first row are stored sequentially, followed by the elements of the second
row, and so on.
Row Major Access
To find the address of the element using row-major order uses the
following formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
I= Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume
it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given
assume it as zero),
N = Number of column given in the matrix.
Eg. Given an array, arr[1………10][1………15] with base value 100 and the
size of each element is 1 Byte in memory. Find the address of arr[8]
Address
[6] with theof A[I][J]
help = 210 order.
of row-major
Coloumn Major Access
The elements of the first column are stored sequentially, followed by the
elements of the second column, and so on.
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Eg. Given an array, arr[1………10][1………15] with base value 100 and the
size of each element is 1 Byte in memory. Find the address of arr[8]
[6] with the help of coloumn-major order.
Address of A[I][J] = 157
Read a matrix and display it
int main()
{
int i, j, m, n, a[100] for(i=0;i<m;i++)
[100]; {
printf("enter dimension for(j=0;j<n;j++)
for a:“); {
scanf(“%d %d”,&m,&n); printf(“%d\
t”,a[i][j]);
cout<<"\n enter }
elements\n"; printf(“\n”);
for(i=0;i<m;i++) }
{
for(j=0;j<n;j++) return 0;
{ }
scanf(“%d”,
&a[i][j]);
Addition of two Matrices
#include<stdio.h>
#include<stdlib.h>
if (m!=p||n!=q)
int main()
{
{
printf("cannot add \n“);
int i, j, m, n, p, q, a[10][10], b[10]
exit(0);
[10], c[10][10];
}
printf("enter dimension for a \n“);
//Reading the elements
scanf(“%d %d”,&m,&n);
printf("enter elements for a \n“);
printf("enter dimension for b\n“);
for (i=0;i<m;i++)
scanf(“%d %d”,&p,&q);
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
Matrix Addition
printf("\n enter elements for b\n)"; //Display
for(i=0;i<p;i++) printf("\n final matrix is \n“);
for(j=0;j<q;j++) for(i=0;i<m;i++)
scanf(“%d”, &b[i][j]); for(j=0;j<n;j++)
printf(“%d\t”,c[i][j]);
//Addition printf("\n“);
for(i=0;i<m;i++) }
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
Row Sum & Column Sum of a matrix
int a[10][10]; //Row sum
int rowsum[10], for(i=0;i<m;i++)
{
colsum[10];
rowsum[i]=0;
printf("enter dimension
for(j=0;j<n;j++)
for a \n“); rowsum[i]=rowsum[i]+a[i][j];
scanf(“%d %d”,&m, &n); }
//Reading printf("\n“);
printf("enter elements for
a \n“);
for (i=0;i<m;i++){
Row Sum & Column Sum of a matrix
//Display
//Column sum
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
{
for(j=0;j<n;j++)
colsum[j]=0;
printf("\t %d“,a[i]
for(i=0;i<m;i++)
[j]);
colsum[j]=colsum[j]+a[i][j];
printf(“->“)
}
printf(“%d\
n”,rowsum[i]);
}
printf("\n“);
for(i=0;i<n;i++)
Row Sum & Column Sum of a matrix
Trace and Norm of a Matrix
Trace is sum of principal diagonal elements of a
square matrix.
Norm is Square Root of sum of squares of
int trace=0,
elements of a matrix.
sum=0,i,j,norm; for(i=0;i<m;i++)
int m=3,n=3; {
printf(“enter elements for(j=0;j<n;j++)
for a \n“); sum=sum+a[ i ][ j]*a[ i ]
for (i=0;i<m;i++){ [ j ];
for(j=0;j<n;j++) }
norm=sqrt(sum);
scanf(“%d”,&a[i][j]);
}
printf(“ trace is %d”,
trace );
for(i=0;i<m;i++)
printf(“ norm is %d”,
trace=trace + a[i][i];
norm );
Check whether a given Matrix is Symmetric or not
printf("enter dimension \ for(i=0;i<m;i++){
n“); for(j=0;j<n;j++){
scanf(“%d %d”,&m,&n);
if(m!=n) if (a[ i ][ j ]!=a[ j ][ i ]){
{ printf("\n matrix is
printf("it is not a square \ not
n“); symmetric \n“);
else exit(0); }
{ printf("enter elements \ } }
n“); printf("\n matrix is
for(i=0;i<m;i++) symmetric“);
for(j=0;j<n;j++) }
Exchange the elements of principal diagonal with secondary diagonal
in an N dimensional Square matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j){
temp=arr[i][j];
arr[i][j]=arr[i][n-i-1];
arr[i][n-i-1]=temp;
int main(){ }
int i, j, temp, arr[4][4],n; printf("\nModified Matrix:\
n“);
printf("\nEnter for(i=0;i<n;i++){
dimension: “); for(j=0;j<n;j++)
scanf(“%d”,&n); printf(" “);
Printf(“%d”,arr[i][j]);
printf(“\nEnter printf("\n“);
elements:\n"); }
for(i=0;i<n;i++) return 0;
for(j=0;j<n;j++) }
Exchange the Rows and Columns of a ‘mxn’ matrix
/*read ‘mxn’ matrix */
printf("\nEnter the rows to
exchange: “);
scanf(“%d %d”,&r1,&r2);
/*Row exchange r1 r2 */
for(j=0;j<n;j++) {
temp=arr[r1-1][j];
arr[r1-1][j]=arr[r2-1][j];
arr[r2-1][j]=temp; }
printf(“\nEnter the cols to
exchange: “);
scanf(“%d %d”,&c1,&c2);
/*Column exchange : c1 c2 */
for(i=0;i<m;i++) {
temp=arr[i][c1-1];
arr[i][c1-1]=arr[i][c2-1];
arr[i][c2-1]=temp; }
Multiplication of two Matrices
#include <stdlib.h>
int main(){ int i, j, m, n, p, q;
printf("enter elements for a \
int a[10][10], b[10][10],c[10]
n“);
[10];
for (i=0;i<m;i++)
printf("enter dimension for a \
{
n“);
for(j=0;j<n;j++)
scanf(“%d %d”,&m,&n);
scanf(“%d”,&a[i][j]);
printf("\n enter dimension for b\
}
n“);
printf("\n enter elements for
Scanf(“%d %d”, &p,&q);
b\n“);
if(n!=p){
for(i=0;i<p;i++)
printf("not multiplicable \n“);
{ for(j=0;j<q;j++)
Multiplication of two Matrices
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("\n The product matrix is \n“);
}
for(i=0;i<m;i++){
for(j=0;j<q;j++)
printf(“%d\t“,c[i][j]);
printf("\n“);
}
Multi Dimensional Arrays
In memory the array elements are stored linearly.
08/28/2025 Deparment of CSE 22