PCD 2-D Array
PCD 2-D Array
PCD 2-D Array
2-D Array
double[][] x;
Multidimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
Declaring, Creating, and Initializing Using
Shorthand Notations
You can also use a shorthand notation to declare, create and
initialize a two-dimensional array. For example,
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
This is equivalent to the following statements:
int[][] array = new int[4][3];
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
Lengths of Multidimensional
Arrays
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
array.length
array[0].length
array[1].length
array[2].length
Ragged Arrays
Each row in a two-dimensional array is
itself an array. So, the rows can have
different lengths. Such an array is
known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};
Example 5.7
Adding and Multiplying Two Matrices
TestMatrixOperation Run
Example 5.7 (cont) Adding and
Multiplying Two Matrices
a 11 a 12 a 13 a 14 a 15 b11 b12 b13 b14 b15 c11 c12 c13 c14 c15
a 21 a 22 a 23 a 24 a 25 b 21 b22 b 23 b 24 b 25 c 21 c 22 c 23 c 24 c 25
a 31 a 32 a 33 a 34 a 35 b31 b32 b33 b34 b35 c 31 c 32 c 33 c 34 c 35
a 41 a 42 a 43 a 44 a 45 b 41 b42 b 43 b 44 b 45 c 41 c 42 c 43 c 44 c 45
a 51 a 52 a 53 a 54 a 55 b51 b52 b53 b54 b55 c 51 c 52 c 53 c 54 c 55
cij = ai1b1j+ai2b2j+ai3b3j+ai4b4j+ai5b5j
Example 5.8
Grading Multiple-Choice Test
Objective: write a
Students’ Answers to the Questions:
0 1 2 3 4 5 6 7 8 9 program that grades
Student 0 A B A C C D E E A D multiple-choice test.
Student 1 D B A B C A E E A D
Student 2 E D D A C B E E A D
Student 3 C B A E D C E E A D Key to the Questions:
Student 4 A B D C C D E E A D
Student 5 B B E C C D E E A D 0 1 2 3 4 5 6 7 8 9
Student 6 B B A C C D E E A D
Student 7 Key D B D C C D A E A D
E B E C C D E E A D
Searching Arrays
Searching is the process of looking
for a specific element in an array;
for example, discovering whether a
certain score is included in a list of
scores. Searching, like sorting, is a
common task in computer programming.
There are many algorithms and data
structures devoted to searching. In
this section, two commonly used
approaches are discussed, linear
search and binary search.
Linear Search
The linear search approach compares
the key element, key, with each
element in the array list[]. The
method continues to do so until the
key matches an element in the list or
the list is exhausted without a match
being found. If a match is made, the
linear search returns the index of
the element in the array that matches
the key. If no match is found, the
search returns -1.
Binary Search
For binary search to work, the
elements in the array must already be
ordered. Without loss of generality,
assume that the array is in ascending
order.
e.g. 2 4 7 10 11 45 50 59 60 66 69 70
79
The binary search first compares the
key with the element in the middle of
the array. Consider the following
three cases:
Binary Search, cont.
If the key is less than the middle
element, you only need to search the
key in the first half of the array.
If the key is equal to the middle
element, the search ends with a
match.
If the key is greater than the
middle element, you only need to
search the key in the second half of
the array.
Binary Search, cont.
key = 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
key = 11 mid
Bubble Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
Pass 1: 2, 5, 4, 8, 1, 6, 9
Pass 2: 2, 4, 5, 1, 6, 8, 9
Pass 3: 2, 4, 1, 5, 6, 8, 9
Pass 4: 2, 1, 4, 5, 6, 8, 9
Pass 5: 1, 2, 4, 5, 6, 8, 9
Pass 6: 1, 2, 4, 5, 6, 8, 9