SlideShare a Scribd company logo
Page 1 of 7


                                        2D array

                                 1-D ARRAY


                                    IMPLEME                      ADDRESS
  DEFINITION                        NTATION                    CALCULATION


                       ROW                         COLUMN
                      MAJOR                         MAJOR




Definition 2 D Array : A 2D array is an array in which each element is itself an
Array. For instance , an array A[M][N] is an M X N matrix.

      Where :
               M = No. of rows
            N = No. of Columns
        M X N = No. of elements.

Implementation of 2-D Array : There are two way to store elements of 2-D array
                                 in Memory .


                    1. Row Major - Where elements are stored row wise.
                    2. Column Major. Where elements are stored Column wise.

Finding The Location(address) of an element in 2-D array :

CASE : 1 . When elements are stored row wise :
Case 1.1 When lower bond is not given.

           A[M][N] or A[M,N]
           Address of A[I][J] or A[I,J] = B+ W[N( I)+J] .
           M= Total No of Rows
           N= Total No of Columns
           I = Expected row
           J = Expected Column
           W = size of each element in byte.

Case 1.2 When lower bound is given.

           A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc]
           Address of A[I][J] or A[I,J] = B+ W[N( I - Lr )+(J-L c )] .



                                 Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 2 of 7


             N= Uc – Lc + 1 (Total No of Columns )
             I = Expected row
             J = Expected Column
             W = size of each element in byte.
             Lr = Lower Bound of row
             Lc= Lower Bound of column
             Uc = Upper Bound of column



CASE : 2 . When elements are stored column wise:
Case 2.1 When lower bond is not given.

            A[M][N] or A[M,N]
            Address of A[I][J] or A[I,J] = B+ W[M(J)+I] .
            M= Total No of Rows
            N= Total No of Columns
            I = Expected row
            J = Expected Column
            W = size of each element in byte.

Case 2.2 When lower bound is given.

            A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc]
            Address of A[I][J] or A[I,J] = B+ W[( I - Lr )+M(J-Lc )] .
            M= Uc – Lc + 1 (Total No of row)
            I = Expected row
            J = Expected Column
            W = size of each element in byte.
            Lr = Lower Bound of row
            Lc = Lower Bound of column
            Uc = Upper Bound of column

Basic Arithmetic Operation On 2 D Array.
-Addition
-Subtraction
-Multiplication

Addition OR Subtraction of two 2D Array : Addition of two 2D array can be performed
when they are equal in size.

Function to find sum of two matrix: A[m1][n1] , B[m2][n2]

 void summatrix(int a[][],int m1, int n1, int b[][], int m2,int n2)
{
int c[m1][n1];
if (m1==m2 && n1==n2)
{
for(int I=0;I<m1;I++)
{
 for(int j=0;j<n1;j++)


                                 Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 3 of 7


{
c[I][J]=a[I][J]+b[I][J]   // c[I][J]=a[I][J]-b[I][J] IN CASE OF SUBSTRACTION;
}
}
else
{

cout<<”Matrix can not be added”;
return;
}

//resultant Matrix
for(int I=0;I<m1;I++)
{
 for(int j=0;j<n1;j++)
{
cout<<c[I][J];
 }
cout<<endl;
}


Multiplication of two 2-D array :
Necessary condition : no. of columns of first matrix must be equal to no of rows
Of second matrix.

        A[m1][n1] , B[m2][n2]
             n1 = m2

Void productmatrix(int a[][],int m1, int n1, int b[][], int m2,int n2)
{int sum ;
int c[m1][n2] ;
if (n1==m2 )
{
for(int I=0;I<m1;I++)
{
 for(int j=0;j<n2;j++)
{
 c[I][J]=0;
for(int k=0;k<n1;k++)
{

c[I][J]=a[I][K]*b[K][J]+c[I][J];
 }
}
}
}
else
{

cout<<”Matrix can not be multiplied ”;


                                   Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 4 of 7


return;
}

//resultant Matrix
for(int I=0;I<m1;I++)
{
 for(int j=0;j<n2;j++)
{
cout<<c[I][J];
 }
cout<<endl;
}

Transpose of matrix : Interchanges of rows and columns of matrix .

void transposematrix(int a[][],int m, int n)
{
int tp[m][n];
for(int I=0;I<m;I++)
{
 for(int j=0;j<n;j++)
{
tp[I][J]=a[J][I];
}
}

//resultant Matrix
for(int I=0;I<n;I++)
{
 for(int j=0;j<m;j++)
{
cout<<c[I][J];
 }
cout<<endl;
}

}


Problems On 2-D array :
Problem 1 : Write a function in C++ which accept an integer array and its size as
arguments and assign the elements into a two dimentional array of integer in the
following format :
   If the array is 1,2,3,4,5,6 The resultant 2D array is Given below :

    1   2   3   4   5   6
    1   2   3   4   5   0
    1   2   3   4   0   0
    1   2   3   0   0   0
    1   2   0   0   0   0
    1   0   0   0   0   0


                                Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 5 of 7




  Sol: void func(int a[] , int size)

  {
  int a2[size][size];
  int I,j;
  for(I=0;I<size;I++)
  {
  for( j=0;j<size;j++)
  {
  if((I+j)>=size)
  {
  a2[I][j]=0;
  }
  else
  {
  a2[I][j]=a[j];
  }
cout<<a2[I][j]<<” “;
  }
  cout<<endl;
  }

  }

   Numerical Problems :
Problem1: Calculate the address of the element a[3][5] of 2-D array a[7][20]
         stored in column wise matrix assume that the base address is 2000, each
         element required 2 bytes of storage. [Ans.2076]         marks 2

Formula used : A[M][N] or A[M,N]
          Address of A[I][J] or A[I,J] = B+ W[M(J)+I] .
          M= Total No of Rows = 7
          N= Total No of Columns=20
          I = Expected row =3
          J = Expected Column        =5
          W = size of each element in byte.=2


            Address of A[3][5]= 2000+2(7*5+3)
                              = 2076.


Problem2: Calculate the address of the element a[2][4] of 2-D array a[5][5]
         stored in row wise matrix assume that the base address is 1000, each
         element required 4 bytes of storage. [Ans.1056]         marks 2

Formula used : A[M][N] or A[M,N]
          Address of A[I][J] or A[I,J] = B+ W[N(I)+J] .
          M= Total No of Rows = 5


                                  Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 6 of 7


            N= Total No of Columns=5
            I = Expected row =2
            J = Expected Column     =4
            W = size of each element in byte.=4


           Address of A[2][4]= 1000+4(5*2+4)
                             = 1056.

Problem3: Each element of an array Data[1..10][1…10] required 8 byte of storage. If
         base address of array Data is 2000 determine the location of
          Data[4][5]. When the array is stored (i) row wise (ii) column wise.
          Ans .(i) 2272 (ii) 2344 Marks-4
Formula used : (i) row wise
           A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc]
          Address of A[I][J] or A[I,J] = B+ W[N( I - Lr )+(J-L c )] .
          N= Uc – Lc + 1 (Total No of Columns ) =10-1+1=10
          I = Expected row =4
          J = Expected Column=5
          W = size of each element in byte.=8
          Lr = Lower Bound of row=1
          Lc= Lower Bound of column=1
          Uc = Upper Bound of column=10

           Address of A[4][5] or A[4,5] = 2000+ 8[10( 4 - 1 )+(5-1 )] .
              = 2272.

                (II) column wise
           A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc]
           Address of A[I][J] or A[I,J] = B+ W[( I - Lr )+M(J-Lc )] .
           M= Uc – Lc + 1 (Total No of row) = 10-1+1=10
           I = Expected row =4
           J = Expected Column=5
           W = size of each element in byte.=8
           Lr = Lower Bound of row=1
           Lc = Lower Bound of column=1
           Uc = Upper Bound of column=10


Address of A[4][5] or A[4,5] = 2000+ 8[( 4 - 1 )+10(5-1 )] .=2344

Problem 4 : If an array B[11][8] is stored is column wise and B[2][2] is stored at   1024
         and B[3][3] is stored at 1084 then find the address of B[5][3].
             Ans. 1094 Marks - 4

           A[M][N] or A[M,N]
           Address of A[I][J] or A[I,J] = B+ W[M(J)+I] .
           M= Total No of Rows
           N= Total No of Columns
           I = Expected row
           J = Expected Column


                                Prepared By Sumit Kumar Gupta, PGT Computer Science
Page 7 of 7


           W = size of each element in byte.

           Address of B[2][2]= B+ W[11*2+2]=1024

              B+24W=1024 ---- equation—1

           Address of B[3][3]= B+ W[11*3+3]=1084
               B+ 36W=1084 equation ---- 2
Solving above these two equation to obtain base address and size of a element .
                 W=5 , B=904

           Address of B[5][3]= 904+ 5[11*3+5]=1094




                             Prepared By Sumit Kumar Gupta, PGT Computer Science

More Related Content

What's hot (18)

PDF
An Efficient Multiplierless Transform algorithm for Video Coding
CSCJournals
 
PDF
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
csandit
 
PDF
Efficient Technique for Image Stenography Based on coordinates of pixels
IOSR Journals
 
PDF
redes neuronais
Roland Silvestre
 
PDF
E-Cordial Labeling of Some Mirror Graphs
Waqas Tariq
 
PDF
129215 specimen-paper-and-mark-schemes
King Ali
 
PDF
Multi-linear algebra and different tensor formats with applications
Alexander Litvinenko
 
PDF
Rough K Means - Numerical Example
Dr.E.N.Sathishkumar
 
PDF
Hbam2011 09
Ardian Sarputra
 
PDF
Radix-3 Algorithm for Realization of Type-II Discrete Sine Transform
IJERA Editor
 
PDF
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
cscpconf
 
PDF
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
IJCSEIT Journal
 
PDF
A novel block cipher involving keys in a key bunch matrix as powers of the pl...
eSAT Journals
 
PDF
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)
ENGR. KADIRI K. O. Ph.D
 
PDF
A novel block cipher involving keys in a key bunch
eSAT Publishing House
 
PDF
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
graphhoc
 
PDF
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
CSCJournals
 
PDF
Finding Neighbors in Images Represented By Quadtree
iosrjce
 
An Efficient Multiplierless Transform algorithm for Video Coding
CSCJournals
 
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
csandit
 
Efficient Technique for Image Stenography Based on coordinates of pixels
IOSR Journals
 
redes neuronais
Roland Silvestre
 
E-Cordial Labeling of Some Mirror Graphs
Waqas Tariq
 
129215 specimen-paper-and-mark-schemes
King Ali
 
Multi-linear algebra and different tensor formats with applications
Alexander Litvinenko
 
Rough K Means - Numerical Example
Dr.E.N.Sathishkumar
 
Hbam2011 09
Ardian Sarputra
 
Radix-3 Algorithm for Realization of Type-II Discrete Sine Transform
IJERA Editor
 
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
cscpconf
 
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
IJCSEIT Journal
 
A novel block cipher involving keys in a key bunch matrix as powers of the pl...
eSAT Journals
 
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)
ENGR. KADIRI K. O. Ph.D
 
A novel block cipher involving keys in a key bunch
eSAT Publishing House
 
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
graphhoc
 
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
CSCJournals
 
Finding Neighbors in Images Represented By Quadtree
iosrjce
 

Viewers also liked (15)

PDF
Pointers
Swarup Kumar Boro
 
PDF
Functions
Swarup Kumar Boro
 
PDF
File handling
Swarup Kumar Boro
 
PDF
1-D array
Swarup Kumar Boro
 
PDF
01 computer communication and networks v
Swarup Kumar Boro
 
TXT
c++ program for Canteen management
Swarup Kumar Boro
 
PDF
Constructor & destructor
Swarup Kumar Boro
 
PDF
C++ revision tour
Swarup Kumar Boro
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
PDF
Computer science study material
Swarup Kumar Boro
 
TXT
c++ program for Railway reservation
Swarup Kumar Boro
 
PDF
Structures in c++
Swarup Kumar Boro
 
File handling
Swarup Kumar Boro
 
01 computer communication and networks v
Swarup Kumar Boro
 
c++ program for Canteen management
Swarup Kumar Boro
 
Constructor & destructor
Swarup Kumar Boro
 
C++ revision tour
Swarup Kumar Boro
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
Computer science study material
Swarup Kumar Boro
 
c++ program for Railway reservation
Swarup Kumar Boro
 
Structures in c++
Swarup Kumar Boro
 
Ad

Similar to 2-D array (20)

PDF
2D Array
Swarup Boro
 
PPTX
arrays.pptx
NehaJain919374
 
PDF
Data Structure Lecture Array and Recursion.pdf
okokji4201
 
PPT
Two Dimensional Array
dincyjain
 
PPTX
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
PPTX
arrays.pptx
HarmanShergill5
 
PPTX
Address_Calculation_Arrays .pptx
miteshchaudhari4466
 
PPT
Chandan
Chandan Walia
 
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
brajmohan21nitj
 
PDF
cluod.pdf
ssuser92d367
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
PPTX
Lecture 4_Linear & Binary search from data structure and algorithm
ArifatunNesa
 
PPTX
polynomial_linked list for electrical engineer
amiyosarkar23
 
PPT
Chapter 10.ppt
MithuBose3
 
PDF
2ds
anumrasheed
 
PPTX
Data Structures - Array presentation .pptx
IshanKapoor26
 
PPTX
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
PPT
02 Arrays And Memory Mapping
Qundeel
 
PPTX
data structure unit -1_170434dd7400.pptx
coc7987515756
 
2D Array
Swarup Boro
 
arrays.pptx
NehaJain919374
 
Data Structure Lecture Array and Recursion.pdf
okokji4201
 
Two Dimensional Array
dincyjain
 
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
arrays.pptx
HarmanShergill5
 
Address_Calculation_Arrays .pptx
miteshchaudhari4466
 
Chandan
Chandan Walia
 
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
brajmohan21nitj
 
cluod.pdf
ssuser92d367
 
Data structure array
MajidHamidAli
 
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
zakiking612
 
Lecture 4_Linear & Binary search from data structure and algorithm
ArifatunNesa
 
polynomial_linked list for electrical engineer
amiyosarkar23
 
Chapter 10.ppt
MithuBose3
 
Data Structures - Array presentation .pptx
IshanKapoor26
 
UNIT-5_Array in c_part1.pptx
sangeeta borde
 
02 Arrays And Memory Mapping
Qundeel
 
data structure unit -1_170434dd7400.pptx
coc7987515756
 
Ad

More from Swarup Kumar Boro (11)

PDF
Arrays and library functions
Swarup Kumar Boro
 
PDF
Function overloading
Swarup Kumar Boro
 
PDF
computer science sample papers 2
Swarup Kumar Boro
 
PDF
computer science sample papers 3
Swarup Kumar Boro
 
PDF
computer science sample papers 1
Swarup Kumar Boro
 
PDF
Boolean algebra
Swarup Kumar Boro
 
PDF
Boolean algebra laws
Swarup Kumar Boro
 
PDF
Oop basic concepts
Swarup Kumar Boro
 
DOCX
Physics
Swarup Kumar Boro
 
PPTX
Physics activity
Swarup Kumar Boro
 
Arrays and library functions
Swarup Kumar Boro
 
Function overloading
Swarup Kumar Boro
 
computer science sample papers 2
Swarup Kumar Boro
 
computer science sample papers 3
Swarup Kumar Boro
 
computer science sample papers 1
Swarup Kumar Boro
 
Boolean algebra
Swarup Kumar Boro
 
Boolean algebra laws
Swarup Kumar Boro
 
Oop basic concepts
Swarup Kumar Boro
 
Physics activity
Swarup Kumar Boro
 

Recently uploaded (20)

PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Quarter1-English3-W4-Identifying Elements of the Story
FLORRACHELSANTOS
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 

2-D array

  • 1. Page 1 of 7 2D array 1-D ARRAY IMPLEME ADDRESS DEFINITION NTATION CALCULATION ROW COLUMN MAJOR MAJOR Definition 2 D Array : A 2D array is an array in which each element is itself an Array. For instance , an array A[M][N] is an M X N matrix. Where : M = No. of rows N = No. of Columns M X N = No. of elements. Implementation of 2-D Array : There are two way to store elements of 2-D array in Memory . 1. Row Major - Where elements are stored row wise. 2. Column Major. Where elements are stored Column wise. Finding The Location(address) of an element in 2-D array : CASE : 1 . When elements are stored row wise : Case 1.1 When lower bond is not given. A[M][N] or A[M,N] Address of A[I][J] or A[I,J] = B+ W[N( I)+J] . M= Total No of Rows N= Total No of Columns I = Expected row J = Expected Column W = size of each element in byte. Case 1.2 When lower bound is given. A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc] Address of A[I][J] or A[I,J] = B+ W[N( I - Lr )+(J-L c )] . Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 2. Page 2 of 7 N= Uc – Lc + 1 (Total No of Columns ) I = Expected row J = Expected Column W = size of each element in byte. Lr = Lower Bound of row Lc= Lower Bound of column Uc = Upper Bound of column CASE : 2 . When elements are stored column wise: Case 2.1 When lower bond is not given. A[M][N] or A[M,N] Address of A[I][J] or A[I,J] = B+ W[M(J)+I] . M= Total No of Rows N= Total No of Columns I = Expected row J = Expected Column W = size of each element in byte. Case 2.2 When lower bound is given. A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc] Address of A[I][J] or A[I,J] = B+ W[( I - Lr )+M(J-Lc )] . M= Uc – Lc + 1 (Total No of row) I = Expected row J = Expected Column W = size of each element in byte. Lr = Lower Bound of row Lc = Lower Bound of column Uc = Upper Bound of column Basic Arithmetic Operation On 2 D Array. -Addition -Subtraction -Multiplication Addition OR Subtraction of two 2D Array : Addition of two 2D array can be performed when they are equal in size. Function to find sum of two matrix: A[m1][n1] , B[m2][n2] void summatrix(int a[][],int m1, int n1, int b[][], int m2,int n2) { int c[m1][n1]; if (m1==m2 && n1==n2) { for(int I=0;I<m1;I++) { for(int j=0;j<n1;j++) Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 3. Page 3 of 7 { c[I][J]=a[I][J]+b[I][J] // c[I][J]=a[I][J]-b[I][J] IN CASE OF SUBSTRACTION; } } else { cout<<”Matrix can not be added”; return; } //resultant Matrix for(int I=0;I<m1;I++) { for(int j=0;j<n1;j++) { cout<<c[I][J]; } cout<<endl; } Multiplication of two 2-D array : Necessary condition : no. of columns of first matrix must be equal to no of rows Of second matrix. A[m1][n1] , B[m2][n2] n1 = m2 Void productmatrix(int a[][],int m1, int n1, int b[][], int m2,int n2) {int sum ; int c[m1][n2] ; if (n1==m2 ) { for(int I=0;I<m1;I++) { for(int j=0;j<n2;j++) { c[I][J]=0; for(int k=0;k<n1;k++) { c[I][J]=a[I][K]*b[K][J]+c[I][J]; } } } } else { cout<<”Matrix can not be multiplied ”; Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 4. Page 4 of 7 return; } //resultant Matrix for(int I=0;I<m1;I++) { for(int j=0;j<n2;j++) { cout<<c[I][J]; } cout<<endl; } Transpose of matrix : Interchanges of rows and columns of matrix . void transposematrix(int a[][],int m, int n) { int tp[m][n]; for(int I=0;I<m;I++) { for(int j=0;j<n;j++) { tp[I][J]=a[J][I]; } } //resultant Matrix for(int I=0;I<n;I++) { for(int j=0;j<m;j++) { cout<<c[I][J]; } cout<<endl; } } Problems On 2-D array : Problem 1 : Write a function in C++ which accept an integer array and its size as arguments and assign the elements into a two dimentional array of integer in the following format : If the array is 1,2,3,4,5,6 The resultant 2D array is Given below : 1 2 3 4 5 6 1 2 3 4 5 0 1 2 3 4 0 0 1 2 3 0 0 0 1 2 0 0 0 0 1 0 0 0 0 0 Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 5. Page 5 of 7 Sol: void func(int a[] , int size) { int a2[size][size]; int I,j; for(I=0;I<size;I++) { for( j=0;j<size;j++) { if((I+j)>=size) { a2[I][j]=0; } else { a2[I][j]=a[j]; } cout<<a2[I][j]<<” “; } cout<<endl; } } Numerical Problems : Problem1: Calculate the address of the element a[3][5] of 2-D array a[7][20] stored in column wise matrix assume that the base address is 2000, each element required 2 bytes of storage. [Ans.2076] marks 2 Formula used : A[M][N] or A[M,N] Address of A[I][J] or A[I,J] = B+ W[M(J)+I] . M= Total No of Rows = 7 N= Total No of Columns=20 I = Expected row =3 J = Expected Column =5 W = size of each element in byte.=2 Address of A[3][5]= 2000+2(7*5+3) = 2076. Problem2: Calculate the address of the element a[2][4] of 2-D array a[5][5] stored in row wise matrix assume that the base address is 1000, each element required 4 bytes of storage. [Ans.1056] marks 2 Formula used : A[M][N] or A[M,N] Address of A[I][J] or A[I,J] = B+ W[N(I)+J] . M= Total No of Rows = 5 Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 6. Page 6 of 7 N= Total No of Columns=5 I = Expected row =2 J = Expected Column =4 W = size of each element in byte.=4 Address of A[2][4]= 1000+4(5*2+4) = 1056. Problem3: Each element of an array Data[1..10][1…10] required 8 byte of storage. If base address of array Data is 2000 determine the location of Data[4][5]. When the array is stored (i) row wise (ii) column wise. Ans .(i) 2272 (ii) 2344 Marks-4 Formula used : (i) row wise A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc] Address of A[I][J] or A[I,J] = B+ W[N( I - Lr )+(J-L c )] . N= Uc – Lc + 1 (Total No of Columns ) =10-1+1=10 I = Expected row =4 J = Expected Column=5 W = size of each element in byte.=8 Lr = Lower Bound of row=1 Lc= Lower Bound of column=1 Uc = Upper Bound of column=10 Address of A[4][5] or A[4,5] = 2000+ 8[10( 4 - 1 )+(5-1 )] . = 2272. (II) column wise A[Lr…Ur][Lc…Uc] or A[Lr : Ur , Lc : Uc] Address of A[I][J] or A[I,J] = B+ W[( I - Lr )+M(J-Lc )] . M= Uc – Lc + 1 (Total No of row) = 10-1+1=10 I = Expected row =4 J = Expected Column=5 W = size of each element in byte.=8 Lr = Lower Bound of row=1 Lc = Lower Bound of column=1 Uc = Upper Bound of column=10 Address of A[4][5] or A[4,5] = 2000+ 8[( 4 - 1 )+10(5-1 )] .=2344 Problem 4 : If an array B[11][8] is stored is column wise and B[2][2] is stored at 1024 and B[3][3] is stored at 1084 then find the address of B[5][3]. Ans. 1094 Marks - 4 A[M][N] or A[M,N] Address of A[I][J] or A[I,J] = B+ W[M(J)+I] . M= Total No of Rows N= Total No of Columns I = Expected row J = Expected Column Prepared By Sumit Kumar Gupta, PGT Computer Science
  • 7. Page 7 of 7 W = size of each element in byte. Address of B[2][2]= B+ W[11*2+2]=1024 B+24W=1024 ---- equation—1 Address of B[3][3]= B+ W[11*3+3]=1084 B+ 36W=1084 equation ---- 2 Solving above these two equation to obtain base address and size of a element . W=5 , B=904 Address of B[5][3]= 904+ 5[11*3+5]=1094 Prepared By Sumit Kumar Gupta, PGT Computer Science