0% found this document useful (0 votes)
2 views16 pages

2D Array

The document explains the concept of 2D arrays, which are arrays of arrays, allowing for the storage of data in a tabular format with rows and columns. It covers the declaration, initialization, and access methods for 2D arrays, as well as the differences between row-major and column-major memory storage implementations. Additionally, it provides formulas for calculating the addresses of elements in both storage methods.

Uploaded by

animegamer371524
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

2D Array

The document explains the concept of 2D arrays, which are arrays of arrays, allowing for the storage of data in a tabular format with rows and columns. It covers the declaration, initialization, and access methods for 2D arrays, as well as the differences between row-major and column-major memory storage implementations. Additionally, it provides formulas for calculating the addresses of elements in both storage methods.

Uploaded by

animegamer371524
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2D Arrays

◼ Arrays are not limited to type int; we can have arrays


of... any other type including float, char , double and
even arrays !!!
◼ An array of arrays is called a two-dimensional array
and can be regarded as a table with a number of
rows and columns:

40
2D Arrays
◼ Each row corresponds to marks of a student. Each
column corresponds to marks in a subject.

50 50 50 60
is a 5  4 array: 80 80 88 72
5 rows, 4 columns 49 50 40 60
60 60 60 76
75 80 75 81

41
Declaring 2D Arrays
50 50 50 60
80 80 88 72
49 50 40 60
60 60 60 76
75 80 75 81
◼ This is
an array of size 5 marks[5]
whose elements are arrays of size 4 [4]
whose elements are integer int
◼ Declare it like this: int marks[5][4];

type of element in name of array number number


each slot of rows of columns
42
Initializing 2D arrays
◼ An array may be initialized at the time of declaration:

int marks[5][4] = {
{ 50, 50, 50, 60} ,
{ 80, 80, 88, 72} ,
{ 49, 50, 40, 60} ,
{ 60, 60, 60, 76} ,
{ 75, 80, 75, 81}
};

43
Initializing 2D arrays
◼ An array may be initialized by using a loop

#define ROWS 5
#define COLS 4

int main () {
int i, j;
int marks[5][4];

/* initialize all elements to zeroes: */


for (i=0; i<ROWS; i++)
for (j=0; j<COLS; j++)
marks[i][j] = 0;
}
44
Initializing 2D arrays
◼ An array may be initialized by the user

#define ROWS 5
#define COLS 4

int main () {
int i, j;
int marks[5][4];
for (i=0; i<ROWS; i++)
for (j=0; j<COLS; j++)
scanf("%d", &marks[i][j]);
}

45
Initializing 2D arrays
◼ An integer array may be initialized to all zeros as
follows:
int nums[5][4] = {0};

◼ This only works for zero.

46
Using 2D arrays
◼ To access an element of a 2D array, you need to
specify both the row and the column:
nums[0][0] = 16;

printf("%d", nums[1][2]);

47
Storing Two-Dimensional Array in
memory
◼ A two-dimensional array can be stored in the
memory in two ways:
◼ Row-major implementation
◼ Column-major implementation

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

A00 A01 A02 A10 A11 A12 A20 A21 A22

Row Row Row 49

1 2 3
Row-major Implementation

◼ The storage can be clearly understood by arranging array


as matrix as show bellow:

A00 A01 A02 Row 1

a= A10 A11 A12 Row 2

A20 A21 A22


Row 3

50
Row-major Implementation

◼ Address of elements in row major implementation:


◼ the computer does not keep the track of all elements of
the array, rather it keeps a base address and calculates
the address of required element when needed.
◼ It calculates by the following relation:
address of element a[i][j]=B+W(n(i)+(j))
◼ Where, B=Base address
◼ W=size of each element array element
◼ n=the number of columns

51
Row-major Implementation

◼ A two-dimensional array defined as a[4][5]


requires 4 bytes of storage space for each element.
If the array is stored in row-major form , then
calculate the address of element at location a[2][3]
given base address is 100.
◼ Sol: B=100, i=2, j=3, n=5, W=4(element size)
◼ Address of a[i][j]= B+W(n*(i)+(j))
◼ Address of a[2][3]=100+4(5*2 + (3))
=100+4(13)
=100+52
=152 52
Column-major Implementation

◼ In column major implementation memory


allocation is done column by column that means
first the elements of the complete first column is
stored then elements of complete second column
is stored and so on.
◼ For example an array [3][3] is stored in the
memory as show bellow:

A00 A10 A20 A01 A11 A21 A02 A12 A22

Col Col Col 53

1 2 3
Column-major Implementation

◼ The storage can be clearly understood by arranging array


as matrix as show bellow:

A00 A01 A02

a= A10 A11 A12

A20 A21 A22

Col 1Col 2Col 3

54
Column-major Implementation

◼ Address of elements in column major


implementation:
◼ the computer does not keep the track of all elements of
the array, rather it keeps a base address and calculates
the address of required element when needed.
◼ It calculates by the following relation:
address of element a[i][j]=B+W(i+m*(j))
◼ Where, B=Base address
◼ W=size of each element array element
◼ m=the number of rows
55

You might also like