SlideShare une entreprise Scribd logo
1  sur  7
Télécharger pour lire hors ligne
Page 1 of 7

2D array
1-D ARRAY

ADDRESS
CALCULATION

IMPLEME
NTATION

DEFINITION
ROW
MAJOR

COLUMN
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]
}
}
else
{

// c[I][J]=a[I][J]-b[I][J] IN CASE OF SUBSTRACTION;

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
1
1
1
1
1

2
2
2
2
2
0

3
3
3
3
0
0

4
4
4
0
0
0

5
5
0
0
0
0

6
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
and B[3][3] is stored at 1084 then find the address of B[5][3].
Ans. 1094 Marks - 4

1024

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

Contenu connexe

Tendances

Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in CSmit Parikh
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Array in c programming
Array in c programmingArray in c programming
Array in c programmingMazharul Islam
 
Presentation on array
Presentation on array Presentation on array
Presentation on array topu93
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in javaTalha mahmood
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queueRojan Pariyar
 

Tendances (20)

Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
Stack
StackStack
Stack
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
ARRAY
ARRAYARRAY
ARRAY
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
deque and it applications
deque and it applicationsdeque and it applications
deque and it applications
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 

En vedette

02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
Theory and application of Isotachophoresis and Isoelectric focussing
Theory and application of Isotachophoresis and Isoelectric focussingTheory and application of Isotachophoresis and Isoelectric focussing
Theory and application of Isotachophoresis and Isoelectric focussingkvineetha8
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Isotachophoresis & ief(iso electric focusing
Isotachophoresis  &  ief(iso electric focusingIsotachophoresis  &  ief(iso electric focusing
Isotachophoresis & ief(iso electric focusingceutics1315
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional arrayRajendran
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
2 d electrophoresis
2 d electrophoresis2 d electrophoresis
2 d electrophoresisRahul Ghalme
 
ISOELECTRIC FOCUSING PPT - SLIDE SHARE
ISOELECTRIC FOCUSING PPT - SLIDE SHAREISOELECTRIC FOCUSING PPT - SLIDE SHARE
ISOELECTRIC FOCUSING PPT - SLIDE SHAREAditi Chaturvedi
 
Two dimensional gel electrophoresis
Two dimensional gel electrophoresisTwo dimensional gel electrophoresis
Two dimensional gel electrophoresisShyam K Uthaman
 
Isoelectric Focusing
Isoelectric FocusingIsoelectric Focusing
Isoelectric FocusingKaleem Iqbal
 
2 d gel electrophoresis
2 d gel electrophoresis2 d gel electrophoresis
2 d gel electrophoresisruks143
 
C programming - String
C programming - StringC programming - String
C programming - StringAchyut Devkota
 
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
 
C programming string
C  programming stringC  programming string
C programming stringargusacademy
 

En vedette (20)

02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Theory and application of Isotachophoresis and Isoelectric focussing
Theory and application of Isotachophoresis and Isoelectric focussingTheory and application of Isotachophoresis and Isoelectric focussing
Theory and application of Isotachophoresis and Isoelectric focussing
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Isotachophoresis & ief(iso electric focusing
Isotachophoresis  &  ief(iso electric focusingIsotachophoresis  &  ief(iso electric focusing
Isotachophoresis & ief(iso electric focusing
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
2 d electrophoresis
2 d electrophoresis2 d electrophoresis
2 d electrophoresis
 
ISOELECTRIC FOCUSING PPT - SLIDE SHARE
ISOELECTRIC FOCUSING PPT - SLIDE SHAREISOELECTRIC FOCUSING PPT - SLIDE SHARE
ISOELECTRIC FOCUSING PPT - SLIDE SHARE
 
Two dimensional gel electrophoresis
Two dimensional gel electrophoresisTwo dimensional gel electrophoresis
Two dimensional gel electrophoresis
 
String functions in C
String functions in CString functions in C
String functions in C
 
String c
String cString c
String c
 
Isoelectric Focusing
Isoelectric FocusingIsoelectric Focusing
Isoelectric Focusing
 
C string
C stringC string
C string
 
Strings in C
Strings in CStrings in C
Strings in C
 
2 d gel electrophoresis
2 d gel electrophoresis2 d gel electrophoresis
2 d gel electrophoresis
 
C programming - String
C programming - StringC programming - String
C programming - String
 
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...
 
C if else
C if elseC if else
C if else
 
C programming string
C  programming stringC  programming string
C programming string
 

Similaire à 2D Array Address Calculation

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
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
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
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...nsitlokeshjain
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDev Chauhan
 
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfQuicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfanupamfootwear
 
Array 2
Array 2Array 2
Array 2Abbott
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
2D-Array How is the address of an individual element of a 2-dimensiona.docx
2D-Array How is the address of an individual element of a 2-dimensiona.docx2D-Array How is the address of an individual element of a 2-dimensiona.docx
2D-Array How is the address of an individual element of a 2-dimensiona.docxcarold12
 

Similaire à 2D Array Address Calculation (20)

2-D array
2-D array2-D array
2-D array
 
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
 
1D Array
1D Array1D Array
1D Array
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
2D arrays
2D arrays2D arrays
2D arrays
 
Arrays
ArraysArrays
Arrays
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
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
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCEDATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
Merge sort
Merge sortMerge sort
Merge sort
 
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdfQuicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
 
Array 2
Array 2Array 2
Array 2
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
Arrays
ArraysArrays
Arrays
 
2D-Array How is the address of an individual element of a 2-dimensiona.docx
2D-Array How is the address of an individual element of a 2-dimensiona.docx2D-Array How is the address of an individual element of a 2-dimensiona.docx
2D-Array How is the address of an individual element of a 2-dimensiona.docx
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 

Plus de Swarup Boro

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Swarup Boro
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsSwarup Boro
 
Array using recursion
Array using recursionArray using recursion
Array using recursionSwarup Boro
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++Swarup Boro
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids Swarup Boro
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloadingSwarup Boro
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sortSwarup Boro
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbersSwarup Boro
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a numberSwarup Boro
 
Canteen management program
Canteen management programCanteen management program
Canteen management programSwarup Boro
 
C++ program using class
C++ program using classC++ program using class
C++ program using classSwarup Boro
 
Railway reservation
Railway reservationRailway reservation
Railway reservationSwarup Boro
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 

Plus de Swarup Boro (20)

Concatenation of two strings using class in c++
Concatenation of two strings using class in c++Concatenation of two strings using class in c++
Concatenation of two strings using class in c++
 
Program: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 studentsProgram: Inheritance in Class - to find topper out of 10 students
Program: Inheritance in Class - to find topper out of 10 students
 
Array using recursion
Array using recursionArray using recursion
Array using recursion
 
Binary addition using class concept in c++
Binary addition using class concept in c++Binary addition using class concept in c++
Binary addition using class concept in c++
 
Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids                                 Study of Diffusion of solids in Liquids
Study of Diffusion of solids in Liquids
 
Program using function overloading
Program using function overloadingProgram using function overloading
Program using function overloading
 
Program to sort array using insertion sort
Program to sort array using insertion sortProgram to sort array using insertion sort
Program to sort array using insertion sort
 
Program to find the avg of two numbers
Program to find the avg of two numbersProgram to find the avg of two numbers
Program to find the avg of two numbers
 
Program to find factorial of a number
Program to find factorial of a numberProgram to find factorial of a number
Program to find factorial of a number
 
Canteen management program
Canteen management programCanteen management program
Canteen management program
 
C++ program using class
C++ program using classC++ program using class
C++ program using class
 
Railway reservation
Railway reservationRailway reservation
Railway reservation
 
Boolean
BooleanBoolean
Boolean
 
Classes
ClassesClasses
Classes
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Pointers
PointersPointers
Pointers
 
Queue
QueueQueue
Queue
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Stack
StackStack
Stack
 

Dernier

Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 

Dernier (20)

Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 

2D Array Address Calculation

  • 1. Page 1 of 7 2D array 1-D ARRAY ADDRESS CALCULATION IMPLEME NTATION DEFINITION ROW MAJOR COLUMN 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] } } else { // c[I][J]=a[I][J]-b[I][J] IN CASE OF SUBSTRACTION; 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 1 1 1 1 1 2 2 2 2 2 0 3 3 3 3 0 0 4 4 4 0 0 0 5 5 0 0 0 0 6 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 and B[3][3] is stored at 1084 then find the address of B[5][3]. Ans. 1094 Marks - 4 1024 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