0% found this document useful (0 votes)
86 views48 pages

4-5. Two Dimensional Array-1

The document discusses two-dimensional arrays including declaration, initialization, implementation in memory, addressing, passing to functions, and multi-dimensional arrays. Key points covered are row-major vs column-major implementation, calculating element addresses, and example code.

Uploaded by

Pratham Agarwal
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)
86 views48 pages

4-5. Two Dimensional Array-1

The document discusses two-dimensional arrays including declaration, initialization, implementation in memory, addressing, passing to functions, and multi-dimensional arrays. Key points covered are row-major vs column-major implementation, calculating element addresses, and example code.

Uploaded by

Pratham Agarwal
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
You are on page 1/ 48

B.

TECH III SEM CSE


ACADEMIC YEAR: 2022-2023

Course Name: Data Structure and Algorithm


Topic: 2-Dimensional Arrays
Course code : CS 2103
Credits : 4
Mode of delivery : Hybrid (Power point presentation)
Faculty : Mr. Satpal Singh Kushwaha
Email-id : [email protected]
Assignment
quiz
Mid term examination – II Assessment criteria’s
End term Examination
2 D A R R AY S

9/6/2022 CSE 1001 Department of CSE 3


Two-Dimensional Array
◼ Two-dimensional arrays are as a grid.
◼ Two dimensional array can be declare as follows:

data_type array_name [row size] [column size];


◼ the first number in brackets is the number of rows,

and second number in brackets is the numbers of


column.
◼ So the upper left corner of any grid would be

element [0][0].
Two-Dimensional Array
◼ Declaration of two-dimensional array:
int arr[3][3];
◼ Here, this example is a graphical or pictorial
representation of 2-D array.
0 1 2
Arr[0][0] Arr[0][1] Arr[0][2]
0

1 Arr[1][0] Arr[1][1] Arr[1][2]

Arr[2][0] Arr[2][1] Arr[2][2]


2
Two-Dimensional Array
◼ Initialization of Two dimensional arrays:
int arr[2][3]={
{10,20,30},
{40,50,60}
};
◼ Bellow show the data in 2-D grid.
0 1 2

0 10 20 30

1 40 50 60
2 Dimensional Arrays
Initialization of two dimensional arrays
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.
Initialization is always done row by row.
The above statement can be equivalently written as
int table [2][3]={{0,0,0},{1,1,1}};
OR in matrix form it can be written as
int table [2][3]= { {0,0,0},
{1,1,1} };

9/6/2022 CSE 1001 Department of CSE 7


2 Dimensional Arrays
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}
};
will initialize the first two elements of the first row to 1, the first
element of the second row to two, and all other elements to zero.
To set all elements to zero
int table [3][3]={{0},{0},{0}};

9/6/2022 CSE 1001 Department of CSE 8


Two-Dimensional Array : Example
Void main()
{
int arr[2][3]={ {10,20,30}, {40,50,60} };
int i,j;
for (i=0;i<2;i++)
{ printf(“\n”);
for(j=0;j<3;j++)
{
printf(“%d ”, arr[i][j]);
}
}
}
Implementation of
Two-Dimensional Array in memory
◼ A two-dimensional array can be implemented in a
programming language in two ways:
◼ Row-major implementation

◼ Column-major implementation
Two-Dimensional Arrays:
Row-major Implementation
◼ Row-major implementation is a linearization
technique in which elements of array are read
from the keyboard row-wise that means the
complete first row is stored then the complete
second row is stored and so on.
◼ For example an array [3][3] is stored in the
memory as show bellow:

A00 A01 A02 A10 A11 A12 A20 A21 A22

Row 1 Row 2 Row 3


Two-Dimensional Arrays
Row-major Implementation
◼ The storage can be clearly understood by arranging array
as matrix as show bellow:

A00 A01 A02 Row 1

A10 A11 A12 Row 2


a=
A20 A21 A22 Row 3
Two-Dimensional Arrays:
Row-major Implementation
◼ Address of elements in row major implementation:
◼ the computer does not keep the track of all elements of
the array, rather it keeps a base address and calculates
the address of required element when needed.
◼ It calculates by the following relation:
address of element
a[i][j]=B+W(n(i-LR)+(j-LC))
Two-Dimensional Arrays:
Row-major Implementation
Address of element
a[i][j]=B+W(n(i-LR)+(j-LC))
◼ Here, B=Base address

◼ W=size of each element array element

◼ n=the number of column (UC-LC+1)

◼ LR the lower bound of row

◼ LC is lower bound of column


A[1………10] [1………15]
◼ UR upper bound of row
LR UR LC UC
◼ UC upper bound of column
Two-Dimensional Arrays:
Row-major Implementation
◼ Example 1: A two-dimensional array defined as a[4:7,-1:3]
requires 2 bytes of storage space for each element. If the
array is stored in row-major form , then calculate the
address of element at location a[6,2] given base address is
100.
◼ Sol: B=100, LR =4, LC =-1, UR =7, UC =3,W=2(int size)
◼ i=6,j=2 and n(no. of col)= UC - LC +1=3-(-1)+1=5
◼ Address of a[i][j]= B+W(n(i- LR)+(j- LC))
◼ Address of a[6][2]=100+2(5(6-4) + (2-(-1))
=100+(2(5*2+3)
=100+26
=126
Two-Dimensional Arrays:
Column-major Implementation
◼ In column major implementation memory
allocation is done column by column that means
first the elements of the complete first column is
stored then elements of complete second column
is stored and so on.
◼ For example an array [3][3] is stored in the
memory as show bellow:

A00 A10 A20 A01 A11 A21 A02 A12 A22

Col 1 Col 2 Col 3


Two-Dimensional Arrays
Column-major Implementation
◼ The storage can be clearly understood by arranging array
as matrix as show bellow:

A00 A01 A02

A10 A11 A12


a=
A20 A21 A22

Col 1 Col 2 Col 3


Two-Dimensional Arrays:
Column-major Implementation
◼ Address of elements in Column major
implementation:
◼ It calculates by the following relation:
address of element a[i][j]=B+W(m(j-LC)+(i-LR))
Two-Dimensional Arrays:
Column-major Implementation
Address of element a[i][j]=B+W(m(j-LC)+(i-LR))

◼ Here, B=Base address


◼ W=size of each element array element
◼ m=the number of row (UR-LR+1)
◼ LR the lower bound of row
◼ LC is lower bound of column
◼ UR upper bound of row
◼ UC upper bound of column
Two-Dimensional Arrays:
Column-major Implementation

◼ Example 1: a two-dimensional array defined as


a[-20:20,10:35] requires one bytes of storage
space for each element. If the array is stored in
column-major form , then calculate the address
of element at location a[0,30] given base
address is 500.
Two-Dimensional Arrays:
Column-major Implementation
◼ Sol: B=500, LR=-20, LC=10, UR=20, UC=35
◼ W=1 byte
◼ i=0,j=30 and m (no. of row)= UR - LR +1
=20-(-20)+1=41
◼ Address of a[i][j]= B+W(m(j-LC)+(i-LR))
◼ Address of a[0][30]=500+1(41(30-10)+(0-(-20))
=500+1(41*20+20)
=500+1(820+20)
=500+840
=1340
Multi-Dimensional Array

◼ N-Dimensional Arrays: The N-Dimensional array is basically


an array of arrays. As 1-D arrays are identified as a single index, 2-
D arrays are identified using two indices, similarly, N-
Dimensional arrays are identified using N indices. A multi-
dimensional array is declared as follows:

◼ The general form of a multi-dimensional array is:

type array_name[s1][s2][s3]…..[sn];
Multi-Dimensional Array
Suppose 3-D array can be group of an array of arrays.
For example : int a[2][3][4];
Here 3-D array which is a collection of four 2-D arrays each
contain 2 rows and 3 column.
N-Dimensional Array
N-Dimensional Array
N-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array

Exercise:

Suppose multi-dimensional arrays A and B are declared using


A (-2 : 2, 2 : 22) and B (1 : B, -5 : 5, - 10 : 5)
•Find the length of each dimension and the number of elements in A and B.
•Considers the element B[3, 3, 3] in B. Find the effective indices E1, E2,
E3 and the address of the element, assuming Base (B) = 400 and there are
w = 4-word memory location. Assume the array in column major form.
Multi-Dimensional Array

(a) The length of a dimension is obtained by


Length = Upper Bound - Lower bound + 1
Hence, the lengths 4 of the dimension of A are,
L1=2-(-2)+ 1=5
L2=22-2+1=21
Accordingly A has 21.5 = 105 elements.
The lengths Li of the dimension of B are,
L1=8-1+1=8
L2=5-(-5)+ 1=11
L3=5-(-10)+1=16
Therefore, B has 8.11.16 = 1408 elements.
Multi-Dimensional Array

(b) The effective index Ei is obtained from Ei=ki- LB,


where ki is the given index and LB, is the lower bound. Hence,
E1= 3 - 1= 2
E2 = 3 - (-5) = 8
E3 =3 -(-10)= 13
The address depends on whether the programming language stores B in row-major order or
column major order.
Assuming B is stored in column major order.
(E3L2+E2)L1+E1 = 1208 + 2 =1210

Therefore, LOC(B[3,3,3]) = 400+4(1210) = 400 +4840 = 5240


Passing Arrays To Functions:
One-Dimensional Arrays:Example
void show (int a[],int s)
{ int i;
for (i=0;i<s;i++)
{
printf(“\n[%d] value is:%d”,a[i]);
}
}
void main()
{
int arr[5]={10,20,30,40,50};
show(arr,5);
}
Passing Arrays To Functions:
Two-Dimensional Arrays:Example
void show (int a[][3],int r,int c)
{
int i,j;
for (i=0;i<r;i++)
{
printf(“\n”);
for(j=0;j<c;j++)
{
printf(“%d ”, arr[i][j]);
}
}
}
Passing Arrays To Functions:
Two-Dimensional Arrays:Example
void main()
{
int arr[2][3]={ {10,20,30}, {40,50,60} };
show(arr,2,3);
}
Read a matrix and display it
int main()
{ for(i=0;i<m;i++)
int i, j, m, n, a[100][100]; {
for(j=0;j<n;j++)
printf("enter dimension 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]);
}
}
9/6/2022 CSE 1001 Department of CSE 37
Addition of two Matrices
if (m!=p||n!=q)
#include<stdio.h>
#include<stdlib.h> {
int main() printf("cannot add \n“);
{ exit(0); }
int i, j, m, n, p, q, a[10][10], //Reading the elements
b[10][10], c[10][10]; printf("enter elements for a \n“);
printf("enter dimension for a \n“); for (i=0;i<m;i++)
scanf(“%d %d”,&m,&n); for(j=0;j<n;j++)
printf("enter dimension for b\n“); scanf(“%d”,&a[i][j]);
scanf(“%d %d”,&p,&q);

9/6/2022 38
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]); {
//Addition for(j=0;j<n;j++)
for(i=0;i<m;i++)
for(j=0;j<n;j++) printf(“%d\t”,c[i][j]);
c[i][j]=a[i][j]+b[i][j]; printf("\n“);
}

9/6/2022 CSE 1001 Department of CSE 39


Row Sum & Column Sum of a matrix
//Row sum
int a[10][10];
for(i=0;i<m;i++)
int rowsum[10], colsum[10]; {
printf("enter dimension for a \n“); rowsum[i]=0;
scanf(“%d %d”,&m, &n); for(j=0;j<n;j++)
rowsum[i]=rowsum[i]+a[i][j];
//Reading
}
printf("enter elements for a \n“);
printf("\n“);
for (i=0;i<m;i++){
for(j=0;j<n;j++)
scanf(“%d”, &a[i][j]);
}
9/6/2022 CSE 1001 Department of CSE 40
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][j]);
for(i=0;i<m;i++)
printf(“->“)
colsum[j]=colsum[j]+a[i][j];
printf(“%d\n”,rowsum[i]);
}
}
printf("\n“);

for(i=0;i<n;i++)
printf("\t %d”,colsum[i]);
9/6/2022 CSE 1001 Department of CSE 41
Problems on
2 D A R R AY

9/6/2022 CSE 1001 Department of CSE 42


1. 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 elements of a matrix.

int trace=0, sum=0,i,j,norm;


int m=3,n=3; for(i=0;i<m;i++)
printf(“enter elements for a \n“); {
for (i=0;i<m;i++){ for(j=0;j<n;j++)
for(j=0;j<n;j++) sum=sum+a[ i ][ j]*a[ i ][ j ];
}
scanf(“%d”,&a[i][j]);
norm=sqrt(sum);
}
printf(“ trace is %d”, trace );
for(i=0;i<m;i++)
printf(“ norm is %d”, norm );
trace=trace + a[i][i];

9/6/2022 CSE 1001 Department of CSE 43


2. Check whether a given Matrix is Symmetric or not

printf("enter dimension \n“); for(i=0;i<m;i++){


scanf(“%d %d”,&m,&n); for(j=0;j<n;j++){
if(m!=n) if (a[ i ][ j ]!=a[ j ][ i ]){
{ printf("\n matrix is not
printf("it is not a square \n“); symmetric \n“);
else exit(0); }
{ printf("enter elements \n“); } }
for(i=0;i<m;i++) printf("\n matrix is symmetric“);
for(j=0;j<n;j++) }
scanf(“%d’,&a[i][j]);
9/6/2022 CSE 1001 Department of CSE 44
3. 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“);
for(i=0;i<n;i++){
printf("\nEnter dimension: “); for(j=0;j<n;j++)
scanf(“%d”,&n); printf(" “);
Printf(“%d”,arr[i][j]);
printf(“\nEnter elements:\n"); printf("\n“);
for(i=0;i<n;i++) }
for(j=0;j<n;j++) return 0;
scanf(“%d”,&arr[i][j]); }
9/6/2022 CSE 1001 Department of CSE 45
4. 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; }
9/6/2022 CSE 1001 Department of CSE 46
5. Multiplication of two Matrices
printf("enter elements for a \n“);
#include <stdlib.h>
for (i=0;i<m;i++)
int main(){ int i, j, m, n, p, q;
{
int a[10][10], b[10][10],c[10][10];
for(j=0;j<n;j++)
printf("enter dimension for a \n“);
scanf(“%d”,&a[i][j]);
scanf(“%d %d”,&m,&n);
}
printf("\n enter dimension for b\n“);
printf("\n enter elements for b\n“
scanf(“%d %d”, &p,&q);
for(i=0;i<p;i++)
if(n!=p){
{ for(j=0;j<q;j++)
printf("not multiplicable \n“);
scanf(“%d”,&b[i][j]);
exit(0); }
CSE 1001 Department of CSE
} 9/6/2022 47
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++)
printf("\n The product matrix is \n“);
c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<m;i++){
}
for(j=0;j<q;j++)
}
printf(“%d\t“,c[i][j]);
printf("\n“);
}

CSE 1001 Department of CSE 9/6/2022 48

You might also like