SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

An Efficient Multiplierless Transform algorithm for Video Coding
An Efficient Multiplierless Transform algorithm for Video CodingAn Efficient Multiplierless Transform algorithm for Video Coding
An Efficient Multiplierless Transform algorithm for Video CodingCSCJournals
 
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...csandit
 
Efficient Technique for Image Stenography Based on coordinates of pixels
Efficient Technique for Image Stenography Based on coordinates of pixelsEfficient Technique for Image Stenography Based on coordinates of pixels
Efficient Technique for Image Stenography Based on coordinates of pixelsIOSR Journals
 
E-Cordial Labeling of Some Mirror Graphs
E-Cordial Labeling of Some Mirror GraphsE-Cordial Labeling of Some Mirror Graphs
E-Cordial Labeling of Some Mirror GraphsWaqas Tariq
 
129215 specimen-paper-and-mark-schemes
129215 specimen-paper-and-mark-schemes129215 specimen-paper-and-mark-schemes
129215 specimen-paper-and-mark-schemesKing Ali
 
Multi-linear algebra and different tensor formats with applications
Multi-linear algebra and different tensor formats with applications Multi-linear algebra and different tensor formats with applications
Multi-linear algebra and different tensor formats with applications Alexander Litvinenko
 
Radix-3 Algorithm for Realization of Type-II Discrete Sine Transform
Radix-3 Algorithm for Realization of Type-II Discrete Sine TransformRadix-3 Algorithm for Realization of Type-II Discrete Sine Transform
Radix-3 Algorithm for Realization of Type-II Discrete Sine TransformIJERA Editor
 
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...cscpconf
 
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SETPROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SETIJCSEIT Journal
 
A novel block cipher involving keys in a key bunch matrix as powers of the pl...
A novel block cipher involving keys in a key bunch matrix as powers of the pl...A novel block cipher involving keys in a key bunch matrix as powers of the pl...
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)
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)ENGR. KADIRI K. O. Ph.D
 
A novel block cipher involving keys in a key bunch
A novel block cipher involving keys in a key bunchA novel block cipher involving keys in a key bunch
A novel block cipher involving keys in a key buncheSAT Publishing House
 
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORKMETRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORKgraphhoc
 
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...CSCJournals
 
Finding Neighbors in Images Represented By Quadtree
Finding Neighbors in Images Represented By QuadtreeFinding Neighbors in Images Represented By Quadtree
Finding Neighbors in Images Represented By Quadtreeiosrjce
 

Mais procurados (18)

An Efficient Multiplierless Transform algorithm for Video Coding
An Efficient Multiplierless Transform algorithm for Video CodingAn Efficient Multiplierless Transform algorithm for Video Coding
An Efficient Multiplierless Transform algorithm for Video Coding
 
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
NEW NON-COPRIME CONJUGATE-PAIR BINARY TO RNS MULTI-MODULI FOR RESIDUE NUMBER ...
 
Efficient Technique for Image Stenography Based on coordinates of pixels
Efficient Technique for Image Stenography Based on coordinates of pixelsEfficient Technique for Image Stenography Based on coordinates of pixels
Efficient Technique for Image Stenography Based on coordinates of pixels
 
redes neuronais
redes neuronaisredes neuronais
redes neuronais
 
E-Cordial Labeling of Some Mirror Graphs
E-Cordial Labeling of Some Mirror GraphsE-Cordial Labeling of Some Mirror Graphs
E-Cordial Labeling of Some Mirror Graphs
 
129215 specimen-paper-and-mark-schemes
129215 specimen-paper-and-mark-schemes129215 specimen-paper-and-mark-schemes
129215 specimen-paper-and-mark-schemes
 
Multi-linear algebra and different tensor formats with applications
Multi-linear algebra and different tensor formats with applications Multi-linear algebra and different tensor formats with applications
Multi-linear algebra and different tensor formats with applications
 
Rough K Means - Numerical Example
Rough K Means - Numerical ExampleRough K Means - Numerical Example
Rough K Means - Numerical Example
 
Hbam2011 09
Hbam2011 09Hbam2011 09
Hbam2011 09
 
Radix-3 Algorithm for Realization of Type-II Discrete Sine Transform
Radix-3 Algorithm for Realization of Type-II Discrete Sine TransformRadix-3 Algorithm for Realization of Type-II Discrete Sine Transform
Radix-3 Algorithm for Realization of Type-II Discrete Sine Transform
 
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
EXACT SOLUTIONS OF A FAMILY OF HIGHER-DIMENSIONAL SPACE-TIME FRACTIONAL KDV-T...
 
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SETPROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
PROBABILISTIC INTERPRETATION OF COMPLEX FUZZY SET
 
A novel block cipher involving keys in a key bunch matrix as powers of the pl...
A novel block cipher involving keys in a key bunch matrix as powers of the pl...A novel block cipher involving keys in a key bunch matrix as powers of the pl...
A novel block cipher involving keys in a key bunch matrix as powers of the pl...
 
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)
INTRODUCTION TO CIRCUIT THEORY (A PRACTICAL APPROACH)
 
A novel block cipher involving keys in a key bunch
A novel block cipher involving keys in a key bunchA novel block cipher involving keys in a key bunch
A novel block cipher involving keys in a key bunch
 
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORKMETRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
METRIC DIMENSION AND UNCERTAINTY OF TRAVERSING ROBOTS IN A NETWORK
 
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
Novel Parallel - Perfix Structure Binary to Residue Number System Conversion ...
 
Finding Neighbors in Images Represented By Quadtree
Finding Neighbors in Images Represented By QuadtreeFinding Neighbors in Images Represented By Quadtree
Finding Neighbors in Images Represented By Quadtree
 

Destaque (15)

Pointers
PointersPointers
Pointers
 
Unit 3
Unit  3Unit  3
Unit 3
 
Functions
FunctionsFunctions
Functions
 
File handling
File handlingFile handling
File handling
 
1-D array
1-D array1-D array
1-D array
 
01 computer communication and networks v
01 computer communication and networks v01 computer communication and networks v
01 computer communication and networks v
 
Stack
StackStack
Stack
 
c++ program for Canteen management
c++ program for Canteen managementc++ program for Canteen management
c++ program for Canteen management
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Queue
QueueQueue
Queue
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Computer science study material
Computer science study materialComputer science study material
Computer science study material
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 

Semelhante a 2-D array

Datastructure tree
Datastructure treeDatastructure tree
Datastructure treerantd
 
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptxAddressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptxAshishPandey502
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 dnikhilarora2211
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPTAlbin562191
 
Array 2
Array 2Array 2
Array 2Abbott
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmitabeasiswa
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
 
Trial kedah 2014 spm add math k2
Trial kedah 2014 spm add math k2Trial kedah 2014 spm add math k2
Trial kedah 2014 spm add math k2Cikgu Pejal
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsemGopi Saiteja
 

Semelhante a 2-D array (20)

2D Array
2D Array2D Array
2D Array
 
1D Array
1D Array1D Array
1D Array
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
 
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptxAddressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
Addressing Formulas for Sparse Matrices Using Column Major in 1D Arrays.pptx
 
array.pptx
array.pptxarray.pptx
array.pptx
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
G029037043
G029037043G029037043
G029037043
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
2D arrays
2D arrays2D arrays
2D arrays
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
 
Array 2
Array 2Array 2
Array 2
 
Basic operations by novi reandy sasmita
Basic operations by novi reandy sasmitaBasic operations by novi reandy sasmita
Basic operations by novi reandy sasmita
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
Trial kedah 2014 spm add math k2
Trial kedah 2014 spm add math k2Trial kedah 2014 spm add math k2
Trial kedah 2014 spm add math k2
 
DFT and IDFT Matlab Code
DFT and IDFT Matlab CodeDFT and IDFT Matlab Code
DFT and IDFT Matlab Code
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
Matrices and determinants-1
Matrices and determinants-1Matrices and determinants-1
Matrices and determinants-1
 

Mais de Swarup Kumar Boro (11)

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2
 
computer science sample papers 3
computer science sample papers 3computer science sample papers 3
computer science sample papers 3
 
computer science sample papers 1
computer science sample papers 1computer science sample papers 1
computer science sample papers 1
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Boolean algebra laws
Boolean algebra lawsBoolean algebra laws
Boolean algebra laws
 
Class
ClassClass
Class
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Physics
PhysicsPhysics
Physics
 
Physics activity
Physics activityPhysics activity
Physics activity
 

Último

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

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