Two Dimensional Arrays: Mirza Mohammad Lutfe Elahi
Two Dimensional Arrays: Mirza Mohammad Lutfe Elahi
Outline
• 2D Arrays
Searching
• Traversing an array to locate a particular item
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
4 7 8 10 14 21 22 36 54 71 85 92
first mid last
22>21 6 8 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
4 7 8 10 14 21 22 36 54 71 85 92
first mid last
6 6 7 22<54
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
4 7 8 10 14 21 22 36 54 71 85 92
22==22
CSE 115 Programming Language I ECE@NSU
8
Sorting
• Sorting is the ordering of a collection of data
• It is a common activity in data management
• Many programs execute faster if the data they process
are sorted before processing begins
• Other programs produce more understandable output
if the data displayed is sorted
• Many sorting algorithms have been designed
• We shall consider the Selection Sort method.
Two-Dimensional Arrays
• A 2D array is a contiguous collection of elements of
the same type, that may be viewed as a table
consisting of multiple rows and multiple columns.
Column
0 1 2 3
0
Row 1
2
2D Array Declaration
• A 2D array is declared by specifying the type of
element, the name of the variable, followed by the
number of rows and the number of columns
• As with 1D arrays, it is a good practice to declare the
row and column sizes as named constants:
#define ROWS 3 Column
#define COLS 4 0 1 2 3
0
. . .
Row 1
int table[ROWS][COLS]; 2
Both rows and columns are indexed from zero.
2D Array Declaration
• A 2D array element is indexed by specifying its
row and column indices.
• The following statement stores 64 in the cell
with row index 1, and column index 3:
0 1 2 3
table[1][3] = 51; 0
1 51
• Here is another example: 2
0 1 2 3
table[2][3] = 0
table[1][3] + 6; 1 51
2 57
CSE 115 Programming Language I ECE@NSU
16
Initialization 2D Arrays
• As with 1-D arrays, you can declare and initialize a 2D array at
the same time.
• A nested list is used, where each inner list represents a row. For
example:
int table[][4] = {{1,2,2,5},{3,4,6},{5,6,7,9}};
0 1 2 3
It is ok to omit the number 0 1 2 2 5
of rows but not the 1 3 4 6 0
number of columns 2 5 6 7 9
• If you provide less values than the declared size, the remaining
cells are set to zero (NOT a good practice!)
Processing 2D Arrays
• To process a 2D array, we use a nested loop, and
traverse the 2D array either row-wise or column-wise
• To process the elements row-wise, we write:
for(i = 0; i < ROWS; i++)
for(j = 0; j < COLS; j++) {
/* process table[i][j] */
}
• To process the elements column-wise, we write:
for(j = 0; j < COLS; j++)
for(i = 0; i < ROWS; i++) {
/* process table[i][j] */
}
CSE 115 Programming Language I ECE@NSU
18
2D Array as a Parameter
• As with 1D arrays, it is possible to declare a function
that takes a 2D array as a parameter.
• The size of the first dimension (number of rows) need
not be specified in the 2D array parameter.
• However, the size of the second dimension (columns)
must be specified as a constant.
• One solution is to use a named constant defining the
maximum number of columns and use additional
parameters for the actual size of the 2D array:
void read_2d(double a[][COLS], int r, int c);
CSE 115 Programming Language I ECE@NSU
19
int i, j;
int i, j;
int i, j;
return 0;
}
CSE 115 Programming Language I ECE@NSU
24
Sample Run