Lesson 2:: Instructor: Email: FB Messenger
Lesson 2:: Instructor: Email: FB Messenger
LESSON 2:
INTRODUCTION
List – one of the simplest and most common types of data. An ordered set consisting of a variable
number of elements to which additions and deletions may be made, if applicable.
Linear list – a list which displays the relationship of physical adjacency. A finite sequence of simple data
items or records.
Days of the Week (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
Values in a Deck of Cards (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace)
Floors of a Building (Basement, Lobby, Mezzanine, First, Second, Third)
ARRAY
➢ May be defined as an ordered collection of data items of the same type referred to collectively
by a single name.
➢ Individual data items in an array are called elements.
➢ Array elements are ordered according to subscripts (an integer from 1 to n); for this reason
they are sometimes called subscripted variables.
Dimensionality of an Array
➢ The number of subscripts of an array determines its dimensionality.
➢ One – dimensional, two – dimensional and multi – dimensional arrays.
The following pseudo code illustrates the process of reading the elements of an array from left
to right. The algorithm Read_Arr accepts the following parameters as input:
Read_Arr (Arr1, N)
{
Set Ctr to 0 Algorithm for reading the elements of a
While (Ctr < N) One-Dimensional Array
{
Print Arr1[Ctr]
Increment Ctr
}
}
Copy_Arr (Arr1, N)
{
Set Ctr to 0
While (Ctr < N)
{
Set Arr2[Ctr] to Arr1[Ctr]
Increment Ctr
}
}
TWO-DIMENSIONAL ARRAYS
➢ An array with two subscripts.
➢ The first subscript is called the row and the second subscript is referred to as the column.
The following is a list of the basic operations that can be performed on two-dimensional
arrays:
1. Retrieving the element in the array
2. Reading all the elements in the array row-wise (or column-wise)
3. Storing a new value
4. Deleting the element
5. Copying an array
Read_TwoDim (Arr1, I, J)
{
Set ctr1 to 0
While (ctr1 < I)
{
Set ctr2 to 0
While (ctr2 < J)
{
Print (Arr1[ctr1][ctr2])
Increment ctr2
}
Increment ctr1
}
}
Algorithm for Copying a Two-Dimensional Array by Row Algorithm for Copying a Two-Dimensional Array by Column
MATRICES
Magic Square
➢ An n x n matrix of the integers 1 to n2 such that the sum of every row, column and diagonal
is the same.
➢ For example, the figure below shows the magic square for a 3 x 3 matrix where the common
sum is 15.
Sum of Diagonals: 1) 6 + 5 + 4 = 15
2) 8 + 5 + 2 = 15
Column Column Column SUM
1 2 3
Magic Square for N=3
Row 1 6 1 8 15
Row 2 7 5 3 15
Row 3 2 9 4 15
SUM 15 15 15
Sparse Matrix
Transpose Matrix
➢ A “copy” of a particular matrix wherein the elements in the [ i, j ] position are transferred
to the [ j, i ] position.
➢ To illustrate this concept, let us consider the matrix below which represents a matrix
called Arr1.
Column 1 Column 2 Column 3 Column Column
4 5
Row 1 1 2 3 4 5
Row 2 6 7 8 9 10
Row 3 11 12 13 14 15
Row 4 16 17 18 19 20
The pseudocode Transpose computes for the transpose of a matrix. It accepts as input
the following information:
1. Arr1 The source array
2. m The number of rows in the matrix
3. n The number of columns in the matrix
Transpose ( Arr1, I, J )
{
Define Arr2 with size [ J x I ]
Set ctr1 to 0
While ( ctr1 < I )
{
Set ctr2 to 0
While ( ctr2 < J )
{
Set Arr2[ctr2][ctr1] to Arr1[ctr1][ctr2]
Increment ctr2
}
Increment ctr1
}
}
1. Modify the algorithm of reading the elements of a one-dimensional array, this time make the
algorithm storing new N elements. (20 pts.)
2. Modify the algorithm of reading the elements of a one-dimensional array, this time make the
algorithm deleting N elements. (20 pts.)
3. Modify the algorithm of sorting the elements of a one-dimensional array in ascending order,
this time the algorithm should arrange the elements in descending order. (20 pts.)
4. Modify the algorithm of reading a two-dimensional array in row-wise, this time make the
algorithm reading the elements in column-wise method. (20 pts.)
5. Using the simple rule for generating a magic square suggested by H. Coxeter, show the magic
square of 5x5 and 7x7 matrix. (20 pts.)