100% found this document useful (1 vote)
991 views

Array and Algorithms

The document discusses arrays and algorithms for performing operations on linear lists and one-dimensional arrays. It defines a linear list as a sequence of elements and describes common list operations like retrieving, inserting, deleting, and sorting elements. It also defines a one-dimensional array as having a single subscript and describes algorithms for reading elements, storing new values, deleting elements, and sorting an array in ascending order.

Uploaded by

Vilma Solayao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
991 views

Array and Algorithms

The document discusses arrays and algorithms for performing operations on linear lists and one-dimensional arrays. It defines a linear list as a sequence of elements and describes common list operations like retrieving, inserting, deleting, and sorting elements. It also defines a one-dimensional array as having a single subscript and describes algorithms for reading elements, storing new values, deleting elements, and sorting an array in ascending order.

Uploaded by

Vilma Solayao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Arrays Algorithm

INTRODUCTION

One of the simplest and most common type of data is the list. By definition, a list is an ordered set
consisting of a variable number of elements to which additions and deletions mat be made, if
applicable. A list which displays the relationship of physical adjacency is called a linear list.

A linear list is a finite sequence of simple data items or records. Each of the elements in a list
forming a sequence (except for the first and the last elements) has a single successor and a single
predecessor. Some examples of linear lists are:

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)

Floor of a Building (Basement, Lobby, Mezzanine, First, Second, Third)

When the elements of a list are arranged in a sequential fashion, a table is obtained. If the
telephone directory of an enterprise is held in computer memory, it may be stored in a table similar
to the one in the figure below.

Last Name First Name Telephone Extension


Anderson Triccia 4407
Bautista Marjorie 4804
Casado Bianca 2272
Canlas Lorna 7786

Zubiri James 1258

Figure 2-1 Table of Telephone Directory Entries

If we consider a linear list more abstractly, we say that it is either empty or it can be written as:

(a1, a2, a3, …, an)

where a1 to an are elements of some set S.


Linear List Operations

There are variety of operations that may be performed on linear lists. These include:

1. Finding the length of the list, n.


2. Reading the list from left-to-right (or right-to-left).
3. Retrieving the ith element in the list, 1≤ i ≤n.
4. Storing a new value into the ith position, 1 ≤ i ≤n.
5. Inserting a new element at position I, where 1 ≤ i ≤n, causing elements numbered i, i +1,
…, n to become numbered i + 1, i +2, …, n+1.
6. Deleting the element at position i, where 1 ≤ i ≤ n, causing elements numbered i + 1, i + 2,
…,n to become numbered i, i + n,…, n – 1.
7. Sorting the elements in a list into ascending or descending order.
8. Copying a list into another list.
9. Merging two or more lists to form a new list.
10. Splitting a list into several sublists.

DEFINITION
The most common way of representing a linear list is through the use of an array. It is appropriate
that we begin our study of data structures with the array since the array is often the only means
provided in a programming language for structuring data. An array may be defined as an ordered
collection of data items of the same type referred to collectively by a single name (the name of the
array). 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. An array elements is indicated by the array name followed by its subscripts, which
appear inside a pair of square brackets.

Dimensionality of an Array
The number of subscripts of an array determines its dimensionality. An array with only one
subscripts, for example Array1 [j], is called a one-dimensional array. In the same manner, an
array with two subscripts, as in Array1 [i,j], is called two-dimensional array. Arrays with more
than two subscripts are called multi-dimensional arrays.

ONE-DIMENSIONAL ARRAYS
We have defined one-dimensional arrays as an array with only one subscript. Therefore, if Array1 is
a one-dimensional array with 10 elements, these elements may be referenced as:

Array1 [subscript1] where 1 <= subscript1<=10

All operations which may be performed on linear lists may be also be done using arrays.
Retrieving the ith Element of a One-Dimensional Array

Retrieving the ith element of an array simply involves the specification of the array name along with
the subscript of the desired element. For example, let us assume that array Arr1 has 10 elements.
The following statement retrieves the 6th element of Arr1:

Arr1[6] where Arr1 = array name


6 = subscript

Reading the Elements of a One-Dimensional Array from Left to Right

The following pseudocode illustrates the process of reading the elements of an array from left to
right. The algorithm Read_Arr accepts the following parameters as inputs:

1. Arr1 The name of the array whose elements are to be read


2. N The number of elements in the array

Read_Arr uses a variable named Ctr as subscripts.

Read_Arr (Arr1,N)
{
Set Ctr to 1
While (Ctr <= N)
{
Print Arr1[Ctr]
Increment Ctr
}
}

Figure 2-2 Algorithm For Reading The Elements Of A One-Dimensional Array

We will now try to simulate the algorithm Read_Arr by assuming that the array Arr1 contains the
following elements:

Arr1[1] Dasher
Arr1[2] Dancer
Arr1[3] Prancer
Arr1[4] Comet
Arr1[5] Cupid
Arr1[6] Donner
Arr1[7] Blitzen
Arr1[8] Vixen
Figure 2-3 One-Dimensional Array with 8 Elements
Ctr = 1

While loop: 1st Iteration


Print Arr1[1] or Print “Dasher’’
Ctr = 2

While loop: 2nd Iteration


Print Arr1[2] or Print “Dancer’’
Ctr = 3

While loop: 3rd Iteration


Print Arr1[3] or Print “Prancer’’
Ctr = 4

While loop: 4th Iteration


Print Arr1[4] or Print “Comet’’
Ctr = 5

While loop: 5th Iteration


Print Arr1[5] or Print “Cupid’’
Ctr = 6

While loop: 6th Iteration


Print Arr1[6] or Print “Donner’’
Ctr = 7

While loop: 7th Iteration


Print Arr1[7] or Print “Blitzen’’
Ctr = 8

While loop: 8th Iteration


Print Arr1[8] or Print “Vixen’’
Ctr = 9

Summary of Output

Dasher
Dancer
Prancer
Comet
Cupid
Donner
Blitzen
Vixen
Storing a New Value into an Element of a One-Dimensional Array

Storing a value into an element of an array overwrites whatever value the element previously
contained. The process of assigning a value to an array element simply requires referencing the
element’s position and setting its value. Using an array Arr1 which has a size of 10, the following
statement sets the 4th element to “Apples”:

Arr1[4] = “Apples” where: 4 = Element Position


“Apples” = New Value

When storing values in an array, it is always important to note that the subscript used to specify the
element`s position should never exceed the size of the array. Therefore, if the size of an array is n:

1 <= subscript <= n

Deleting an Element from a One-Dimensional Array

The elements of an array cannot be “physically” deleted. This is because the size of an array, once
defined, always remains the same. Instead, array elements may be set to a specific value, for
example, blank, to signify deletion. The following statement “deletes” the 8 th element in an array
called Arr1 which has size of 10 elements:

Arr1[8] = “ ” where 8 = position of element to be “deleted”

Sorting the Elements of a One-Dimensional Array in Ascending Order

Using an array called Arr1 with a size of N, the following pseudocode illustrates the procedure for
sorting an array in ascending order. This algorithm makes use of three other variables:

1. Ctr1 A subscript to an element of the array


2. Ctr2 A subscript to another element of the array
3. Temp A variable used to temporarily hold the value of an element
Sort_Art (Arr1,N)
{
Set Ctr1 to 1
While (Ctr1 <= N-1)
{
Set Ctr2 to Ctr1 + 1
While (Ctr2 <= N)
{
If (Arr1[Ctr1] . Arr1[Ctr2]) then
{
Set Temp to Arr1[Ctr1]
Set Arr1[Ctr1] to Arr1[Ctr2]
Set Arr1[Ctr2] to Temp
}
Increment Ctr2
}
Increment Ctr1
}
}

Figure 2-4 Algorithm For Sorting The Elements of A One-Dimensional Array

Let us assume that Arr1 is an array of size 4 containing the following elements:

Arr[1] Raphaelo
Arr[2] Michaelangelo
Arr[3] Leonardo
Arr[4] Donatello

Figure 2-5 One-Dimensional Array With 4 Elements

Let us now sort the contents of Arr1 using the pseudocode Sort_Arr.

Ctr1 = 1
Outer While Loop: 1st Iteration
Ctr2 = 2

Inner While Loop: 1st Iteration


Temp = “Raphaelo”
Arr1[1] = “Michaelangelo”
Arr1[2] = “Raphaelo”
Ctr2 = 3
Contents of Arr1 after Inner Loop

Arr[1] Michaelangelo
Arr[2] Raphaelo
Arr[3] Leonardo
Arr[4] Donatello

Inner While Loop: 2nd Iteration


Temp = “Michaelangelo”
Arr1[1] = “Leonardo”
Arr1[3] = “Michaelangelo”
Ctr2 = 4

Contents of Arr1 after Inner Loop

Arr[1] Leonardo
Arr[2] Raphaelo
Arr[3] Michaelangelo
Arr[4] Donatello

Inner While Loop: 3rd Iteration


Temp = “Leonardo”
Arr1[1] = “Donatello”
Arr1[4] = “Leonardo”
Ctr2 = 5

Contents of Arr1 after Inner Loop

Arr[1] Donatello
Arr[2] Raphaelo
Arr[3] Michaelangelo
Arr[4] Leonardo

Ctr1 = 2
Outer While Loop: 2nd Iteration
Ctr2 = 3

Inner While Loop: 1sd Iteration


Temp = “Raphaelo”
Arr1[2] = “Michaelangelo”
Arr1[3] = “Raphaelo”
Ctr2 = 4
Contents of Arr1 after Inner Loop

Arr[1] Donatello
Arr[2] Michaelangelo
Arr[3] Raphaelo
Arr[4] Leonardo

Inner While Loop: 2nd Iteration


Temp = “Michaelangelo”
Arr1[2] = “Leonardo”
Arr1[4] = “Michaelangelo”
Ctr2 = 5

Contents of Arr1 after Inner Loop

Arr[1] Donatello
Arr[2] Leonardo
Arr[3] Raphaelo
Arr[4] Michaelangelo

Ctr1 = 3
Outer While Loop: 3rd Iteration
Ctr2 = 4

Inner While Loop: 1st Iteration


Temp = “Raphaelo”
Arr1[3] = “Michaelangelo”
Arr1[4] = “Raphaelo”
Ctr2 = 5
Ctr1 = 4

Final Contents of Arr1


Arr[1] Donatello
Arr[2] Leonardo
Arr[3] Michaelangelo
Arr[4] Raphaelo

Copying a One-Dimensional Array

To illustrate the procedure for copying an array into another array, we will use the two arrays: Arr1
and Arr2. Arr1 will represent the source array; it has a size of N. Arr2 will represent the target array.
Since we are copying the entire Arr1 into Arr2, Arr2 also has a size of N. The algorithm Copy_Arr
uses a variable named Ctr as a subscript.

Copy_Arr (Arr1,N)
{
Set Ctr to 1
While (Ctr <= N)
{
Set Arr2[Ctr] to Arr1[Ctr]
Increment Ctr
}
}

Figure 2-5 Algorithm For Copying A One-Dimensional Array

To simulate the pseudocode Copy_Arr (), we will use the array in figure 2-3 where N = 8.

Ctr = 1
While Loop: 1st Iteration While Loop: 5th Iteration
Arr2[1] = “Dasher” Arr2[5] = “Cupid”
Ctr = 2 Ctr = 6

While Loop: 1st Iteration While Loop: 5th Iteration


Arr2[2] = “Dancer” Arr2[6] = “Donner”
Ctr = 3 Ctr = 7

While Loop: 1st Iteration While Loop: 5th Iteration


Arr2[3] = “Prancer” Arr2[7] = “Blitzen”
Ctr = 4 Ctr = 8

While Loop: 1st Iteration While Loop: 5th Iteration


Arr2[4] = “Comet” Arr2[8] = “Vixen”
Ctr = 5 Ctr = 9

Summary of Output:

Arr[1] Dasher
Arr[2] Dancer
Arr[3] Prancer
Arr[4] Comet
Arr[5] Cupid
Arr[6] Donner
Arr[7] Blitzen
Arr[8] Vixen
Merging Two One-Dimensional Arrays
Given three arrays: Arr1, Arr2 and Arr3, where Arr1 and Arr2 are sorted in ascending order, we can
merge Arr1 and Arr2 into the array Arr3 such that the contents of Arr3 are also sorted in ascending
order: In order to perform this operation, Arr3 would have to be large enough to contain all
elements.

The algorithm Merge_Arr accepts as inputs the arrays Arr1 and Arr2 along with the following
information.

1. N1 The size of array Arr1


2. N2 The size of array Arr2

The algorithm also uses three variables, Ctr1, Ctr2, and Ctr3, as subscripts to the three arrays.
Merge_Arr (Arr1, Arr2, Arr3,N1, N2, N3)
{
Set Ctr1 to 1
Set Ctr2 to 1
Set Ctr3 to 1

While ((Ctr1 <= N1) OR (Ctr2 <= N2) or (Ctr3 <= N3))
{
If (Ctr1 > N1) then /* First Condition */
{
Copy_from_Arr2()
}
Else
{
Figure 2-7 Algorithm
If (Ctr2 > N2) thenFor Merging Two One-Dimensional
/* Second Arrays
Condition */
{
Copy_from_Arr1()
}
Let us now simulate the algorithm Merge_Arr by assuming the following elements:
Else
1. Arr1 is a one-dimensional
{ array of size 4 containing the following elements:
If (Arr1[Ctr1] < Arr2[Ctr2]) then /* Third Condition */
{ Arr1[1] Dopey
Copy_from_Arr1() Arr2[2] Grummpy
} Arr3[3] Happy
Else Arr4[4] Sleepy /* Fourth Condition */
{
Copy_from_Arr2()
Figure
} 2-8 One-Dimensional Array With 4 Elements
}
}
Increment Ctr3
2. Arr2 is one-dimensional array of size 3 containing the following elements:
}
}
Arr2[1] Bashful
Copy_from_Arr1() Arr2[2] Doc
{ Arr2[3] Sneezy
Set Arr3[Ctr3] to Arr1[Ctr1]
Increment Ctr1
} Figure 2-9 One-Dimensional Array With 3 Elements
Copy_from_Arr2()
3. Arr3 is an
{ array of size 7 containing blanks.
Set Arr3[Ctr3] to Arr2[Ctr2]
Increment Ctr2
Ctr1 =}1
Ctr2 = 1
Ctr3 = 1

While Loop: 1st Iteration While Loop: 2nd Iteration


/*Fourth Condition*/ /*Fourth Condition*/
/*Copy_from_Arr2*/ /*Copy_from_Arr2*/
Arr3[1] = “Bashful” Arr3[2] = “Doc”
Ctr2 = 2 Ctr2 = 3
Ctr3 = 2 Ctr3 = 3

While Loop: 3rd Iteration While Loop: 4th Iteration


/*Third Condition*/ /*Third Condition*/
/*Copy_from_Arr1*/ /*Copy_from_Arr1*/
Arr3[3] = “Dopey” Arr3[4] = “Grumpy”
While Loop: 5th Iteration While Loop: 6th Iteration
/*Third Condition*/ /*Third Condition*/
/*Copy_from_Arr1*/ /*Copy_from_Arr1*/
Arr3[5] = “Happy” Arr3[6] = “Sleepy”
Ctr1 = 4 Ctr1 = 5
Ctr3 = 6 Ctr3 = 7

While Loop: 5th Iteration


/*First Condition*/
/*Copy_from_Arr2*/
Arr3[7] = “Sneezy”
Ctr2 = 4
Ctr3 = 8

Contents of Arr3 after Merge_Arr

Arr3[1] Bashful
Arr3[2] Doc
Arr3[3] Dopey
Arr3[4] Grumpy
Arr3[5] Happy
Arr3[6] Sleepy
Arr3[7] Sneezy

Splitting a One-Dimensional Array

The pseudocode for spilitting an array into two or more smaller arrays is better explained
using an example. Let us assume that Arr1 is an array of size N1, where each element is a
record containing the following fields:

1. Student Name
2. Student Number

Let us further suppose that the field Student Number has the format:

YY-nnnnn where: 90 <= YY <= 92


The whole record of any element i may simply be referenced as: Arr1[i]. On the other hand,
each field in element i may be accessed individually as:

Arr1.Studname[i] To retrieve the Student Name field of element i


Arr 1.Studnum[i] To retrieve the Student Number field of element i

The pseudocode N1) will divide Arr1 into the following three arrays:
Split_Arr()
Split_Arr (Arr1,
{
1. Arr90 An array which contain all records whose Student Number field has YY = 90
2. Arr91Set Ctr1An to array
1 which contain all records whose Student Number field has YY = 91
3. Arr92Set Ctr90 to 1
An array which contain all records whose Student Number field has YY = 92
Set Ctr91 to 1
After dividing Arr1 into Arr90,
Set Ctr92 to 1 Arr91, and Arr92, Split_Arr() will print the names of the students
under each newWhile
array(Ctr1
along<= withN1)
appropriate headings.
{
Switch (YY of Arr1.Studenum[Ctr1])
{
Case (“90”)
{
Set Arr90[Ctr90] to Arr1[Ctr1]
Increment Ctr90
}
Case (“91”)
{
Set Arr90[Ctr91] to Arr1[Ctr1]
Increment Ctr91
}

Default
{
Set Arr92[Ctr92] to Arr1[Ctr1]
Increment Ctr92
}
}
Increment Ctr1
}
If (Ctr90 > 1) then /*First IF Statement*/
{
Print_Arr90 (Arr90, Ctr90)

}
If (Ctr91 > 1) then /*Second IF Statement*/
{
Print_Arr91 (Arr91,Ctr91)
}
If (Ctr92 > 1) then /*Third IF Statement*/
{
Print_Arr92 (Arr92,Ctr92)
}
}
Print_Arr90 ()
{
Print “Students with Student Numbers beginning with `90`:”
Print “ “
Set l to 1
While (l < Ctr90)
{
Print Arr90.StudName[l]
Increment l
}
Print “ “
}

Print_Arr91 ()
{
Print “Students with Student Numbers beginning with `91`:”
Print “ “
Set l to 1
While (l < Ctr91)
{
Print Arr91.StudName[l]
Increment l
}
Print “ “
}

Print_Arr92 ()
{
Print “Students with Student Numbers beginning with `92`:”
Print “ “
Set l to 1
While (l < Ctr92)
{
Print Arr92.StudName[l]
Increment l
}
Print “ “
}

Figure 2-10 Algorithm For Splitting A One-Dimensional Array


Let us now assume that Arr1 contains the following six elements.

Student Number Student Name


Arr1[1] 90-00001 Hewey Duck
Arr1[2] 92-08888 Mickey Mouse
Arr1[3] 90-02323 Dewey Duck
Arr1[4] 91-00463 Donald Duck
Arr1[5] 90-87361 Lewey Duck
Arr1[6] 91-04520 Daisy Duck

Figure 2-11 One-Dimensional Array With Six Elements

The pseudocode Split_Arr() would then generate the following results:

Ctr1 = 1
Ctr90 = 1
Ctr91 = 1
Ctr92 = 1

While Loop: 1st Iteration


/*Case 90*/
Arr90[1] = “90-00001Hewey Duck”
Ctr90 = 2
Ctr1 = 2
While Loop: 2nd Iteration
/*Default*/
Arr92[1] = “92-0888Mickey Mouse”
Ctr92 = 2
Ctr1 = 3
While Loop: 3rd Iteration
/*Case 90*/
Arr90[2] = “90-02323Dewey Duck”
Ctr90 = 3
Ctr1 = 4
While Loop: 4th Iteration
/*Case 91*/
Arr91[1] = “91-00463Donald Duck”
Ctr91 = 2
Ctr1 = 5
While Loop: 5th Iteration
/*Case 90*/
Arr90[3] = “90-87361Lewey Duck”
Ctr90 = 4
Ctr1 = 6
While Loop: 6th Iteration
/*Case 91*/
Arr92[2] = “91-04520Daisy Duck”
Ctr91 = 3
Ctr1 = 7

/*First IF Statement*/
/*Print_Arr90()*/
Print “Students with Student Numbers beginning with `90`:”
Print “ “
l=1
Print “ “

/*Second IF Statement*/
/*Print_Arr91()*/
Print “Students with Student Numbers beginning with `91`:”
Print “ “
l=1
While Loop: 1st Iteration
Print “Donald Duck”
l=2
While Loop: 2nd Iteration
Print “Daisy Duck”
l=3
Print “ “

/*Third IF Statement*/
/*Print_Arr92()*/
Print “Students with Student Numbers beginning with `92`:”
Print “ “
l=1
While Loop: 1st Iteration
Print “Mickey Mouse”
l=2
Print “ “
Summary of Data Printed

Students with Student Numbers beginning


with `90`:

Hewey Duck
Dewey Duck
Lewey Duck

Students with Student Numbers beginning


with `91`:

Summary of Contents of Arr90, Arr91, and Arr92:

Student Number Student Name


Arr90[1] 90-00001 Hewey Duck
Arr90[2] 90-02323 Dewey Duck
Arr90[3] 90-87361 Lewey Duck

Student Number Student Name


Arr91[1] 91-00463 Donald Duck
Arr91[2] 91-04520 Daisy Duck

Student Number Student Name


Arr92[1] 92-08888 Mickey Mouse

TWO-DIMENSIONAL ARRAYS

A two-dimensional array is an array with two subscripts. The first subscript is called row and the
second subscript is referred to as the column. Because of this property, two-dimensional arrays are
often called tables or matrices. To reference an element of two-dimensional array, both the row
number and column number of the desired element must be specified. For example, the statement
Arr1[2,3] accesses the element in the second row, third column of the two-dimensional array Arr1.
The figure below shows an example of a two-dimensional array called Arr1 containing 3 rows and 4
columns.

Column 1 Column 2 Column 3 Column 4


Row 1 1 2 3 4
Row 2 2 4 6 8
Row 3 3 6 9 12
Where: Arr1[1,1] = 1 Arr1[2,1] = 2 Arr1[3,1] = 3
Arr1[1,2] = 2 Arr1[2,2] = 4 Arr1[3,2] = 6
Arr1[1,3] = 3 Arr1[2,3] = 6 Arr1[3,3] = 9
Arr1[1,4] = 4 Arr1[2,4] = 8 Arr1[3,4] = 12

Figure 2-12 Two-Dimensional Array With 3 Rows and 4 Column

The following is a list of the basic that can be performed on two-dimensional arrays. In the list, m
represents the number of rows in the array and n the number of columns.

1. Retrieving the [i,j]th element in the array, 1 ≤ i ≤ m and 1 ≤ j ≤ n.


2. Reading all the elements in the array row-wise (or column-wise).
3. Storing a new value into the [i,j]th position, 1 ≤ i ≤ m and 1 ≤ j ≤ n.
4. “Deleting” the element at position [i,j], where 1 ≤ i ≤ m and 1 ≤ j ≤ n.
5. Copying an array

Merging and splitting may also be performed on two-dimensional arrays. However, these are no
longer basic operations because they require a close study of application for which they will be used.
Additionally, there is little difference between the pseudocodes of one-dimensional and two-
dimensional arrays for the first three operations, therefore, we will no longer elaborate on them.
With regard to copying an array, a number of changes will have to be done on the original code so
we will elaborate on this operation. We will also look into some applications of two-dimensional
arrays.

Copying a Two-Dimensional Arrays

To illustrate the procedure for copying a two-dimensional array into another array, we will use the
two arrays: Arr1nand Arr2. Arr1 will be our source array having m. rows and n columns. Arr2 will
represent the target array. Since we are copying all elements in Arr1, Arr2 also has the size [m, n].

The algorithm Copy_2Dim accepts as input the source array, Arr1, along with its dimensions: m, and
n. additionally, it uses two variables, I and J, as subscripts.
Copy_2Dim (Arr1,m,n)
{
Set I to 1
While (I ≤ m)
{
Set J to 1
While (J ≤ n)
{
Set Arr2[I,J] to Arr1[I,J]
Increment J
}
Increment I
}
}
Figure 2-13 Algorithm For Copying A Two-Dimensional Array By Row

To simulate the pseudocode Copy_2Dim (), we will use the two-dimensional array, Arr1, which in
figure 2-12.

I=1
Outer While Loop: 1st Iteration
J=1
Inner
Inner While
While Loop:
Loop: 31st Iteration
rd

Arr2[1,3]
Arr2[1,1] =
= 31
JJ =
= 42
InnerWhile
Inner WhileLoop:
Loop:2nd
4thIteration
Iteration
Arr2[1,4] = 4
Arr2[1,2] = 2
JJ =
= 53
I=2
Outer While Loop: 2nd Iteration
J=1
Inner While Loop: 1st Iteration
Arr2[2,1] = 2
J=2
Inner While Loop: 2nd Iteration
Arr2[2,2] = 4
J=3
Inner While Loop: 3rd Iteration
Arr2[2,3] = 6
J=4
Inner While Loop: 4th Iteration
Arr2[2,4] = 8
J=5
I=3
Outer While Loop: 2nd Iteration
J=1
Inner While Loop: 1st Iteration
Arr2[3,1] = 3
J=2
Inner While Loop: 2nd Iteration
Arr2[3,2] = 6
J=3

Inner While Loop: 3rd Iteration


Arr2[3,3] = 9
J=4
Inner While Loop: 4th Iteration
Arr2[3,4] = 12
J=5
I=4
ResultofExecution

Arr2[1,1] = 1 Arr2[2,1] = 2 Arr2[3,1] = 3


Arr2[1,2] = 2 Arr2[2,2] = 4 Arr2[3,2] = 6
Arr2[1,3] = 3 Arr2[2,3] = 6 Arr2[3,3] = 9
Arr2[1,4] = 4 Arr2[2,4] = 8 Arr2[3,4] = 10

Copying an Two-Dimensional Array by Column

In copying arrays, the number of loops equals the dimensionality of the array. In our example, Arr1
is a two-dimensional array, consequently procedure Copy_2Dim contains two While loops. If our
array had three dimensions then the pseudocode would have had three While loops. Additionally,
our algorithm copied Arr1 into Arr2 by row, meaning Arr1[1,1] was copied first then Arr1[1,2]
followed by Arr1[1,3], and so on. It is also possible to copy a two-dimensional array by column. The
following pseudocode shows how this is done. If you will notice the algorithm is basically similar to
Copy_2Dim.
Copy_2Dim_Col (Arr1,m,n)
{
Set J to 1
While (J ≤ n)
{
Set I to 1
While (I ≤ m)
{
Set Arr2[I,J] to Arr1[I,J]
Increment I
}
Increment J
}
}

Figure 2-14 Algorithm For Copying A Two-Dimensional Array By Column

By processing the arrays rows in the inner loop, our procedure effectively copies Arr1 into Arr2 by
column.
Matrices

The following sample applications of two-dimensional arrays make use of matrices. A matrix is a
mathematical object which arises in many physical problems. As computer scientists, we are
interested in studying ways to represent matrices so that the operations to be performed on them
can be carried out efficiently. A general matrix consists of m rows and n columns. The following
tables are examples of matrices.

Column Colum Column


1 n2 3
Row 1 -27 3 4
Row 2 6 82 -0.3
Row 3 109 -64 4
Row 4 0.12 8 9
Row 5 3.4 36 27

Column Column Column Column Column Column


1 2 3 4 5 6
Row 1 15 0 0 22 0 -15
Row 2 0 11 3 0 0 0
Row 3 0 0 0 -6 0 0
Row 4 0 0 0 0 0 0
Row 5 91 0 0 0 0 0
Row 6 0 0 28 0 0 0
Colum Column Column Sum
Figure 2-15 Matrices
n1 2 3
Row 1 6 1 8 15
Row 2 7 5 3 15
Row 3 2 9 4 15
Sum 15 15 15

The first matrix has five rows and three columns, the second, six rows and six columns. In general,
we write m x n (read m by n) to designate a matrix with m rows and n columns. Such a matrix has a
total of mn elements. Whenever m is equal to n, we call the matrix a square.

Magic Square

A magic square is 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

Figure 2-16 Magic Square For N=3

When n is odd, H. Coxeter has given a simple rule for generating a magic square:

“Start with 1 in the middle of the top row; then go up and left assigning numbers in
increasing order to empty squares; if you fall off the square imagine the same square as
tilting the plane and continue; if a square is occupied move down instead and continue.”

Using this rule, let us now study the pseudocode for creating an n x n magic square where n is odd
and n is greater than 1.
Magic (n)
{
Check_n()
Initialize_Square()

Set I to 1
Set J to (((n-1) / 2) + 1) /* Set J to the middle of the first row */
Set Square[I,J] to 1 /* Set middle of first row to 1 */
Set Next_Value to 2

While (Next_Value ≤ n2) /* Main While Loop */


{
/* Determine next row */
If (I = 1) then /* First If */
{
Set Temp_I to n
}
Else /* First Else */
{
Set Temp_I to (I – 1)
}
/* Determine next column */
If (J = 1) then /* Second If */
{
Set Temp_J to n
}
Else /* Second Else */
{
Set Temp_J to (J – 1)
}

If (Square[Temp_I, Temp_J] is not empty) then /* Third If */


{
If (I = n) then /* Fourth If */
{
Set I to 1
}
Else /* Fourth Else */
Check_n()
/* Verifies if n is odd number greater than 1 */
{
While (n is even) OR (n = 1)
{
Print “Error: The number of rows/columns in the square must be odd and
Greater than 1.”
}
}

Iniatialize_Square()
/* Sets all elements of Square to 0 */
{
Set I to 1
While (I ≤ n)
{
Set J to 1
While (J ≤ n)
{
Set Square[I,J] to 0
Increment J
}
Increment I
}
}

Figure 2-17 Magic Square Algorithm


To simulate the pseudocode magic, let us assume that n = 3.

/* Check_n() */
/* Initialize_Square() */
I=1
Outer While Lopp: 1st Iteration
J=1
Inner While Loop: 1st Iteration
Square[1,1] = 0
J=2
Inner While Loop: 2ndIteration
Square[1,2] = 0
J=3
Inner While Loop: 3rdIteration
Square[1,3] = 0
J=4
I=2
Outer While Lopp: 2ndIteration
J=1
Inner While Loop: 1st Iteration
Square[2,1] = 0
J=2
Inner While Loop: 2ndIteration
Square[2,2] = 0
J=3
Inner While Loop: 3rdIteration
Square[2,3] = 0
J=4
I=3
Outer While Lopp: 3rdIteration
J=1
Inner While Loop: 1st Iteration
Square[3,1] = 0
J=2
Inner While Loop: 2ndIteration
Square[3,2] = 0
J=3
Inner While Loop: 3rdIteration
Square[3,3] = 0
J=4
I=4

Contents of Square after Iniatilize_Square()

Column Colum Column


1 n2 3
Row 1 0 0 0
Row 2 0 0 0
Row 3 0 0 0

I=1
J=2
Square[1,2] = 1
Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 0 1 0
Row 2 0 0 0
Row 3 0 0 0

Next_Value = 2
Main While Loop: 1st Iteration
/* First If */
Temp_I = 3
/* Second Else */
Temp_J = 1
/* Third Else */
Next_Value
I=3 =3
J= 1
Contents of Square after Statements
Square[3,1] = 2

Column Colum Column


1 n2 3
Row 1 0 1 0
Row 2 0 0 0
Row 3 2 0 0

Main While Loop: 2nd Iteration


/* First Else */
Temp_I = 2
/* Second If */
Temp_J = 3
/* Third Else */
I=2
J= 3
Square[2,3] = 3
Next_Value = 4

Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 0 1 0
Row 2 0 0 3
Row 3 2 0 0
Main While Loop: 3rd Iteration
/* First Else */
Temp_I = 1
/* Second Else */
Temp_J = 2
/* Third If */
/* Fourth Else */
I=3
Square[3,3] = 4
Next_Value = 5

Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 0 1 0
Row 2 0 0 3
Row 3 2 0 4
Main While Loop: 4th Iteration
/* First Else */
Temp_I = 2
/* Second Else */
Temp_J = 2
/* Third Else */
I=2
J=2
Square[2,2] = 5
Next_Value = 6

Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 0 1 0
Row 2 0 5 3
Row 3 2 0 4

Main While Loop: 5th Iteration


/* First Else */
Temp_I = 1
/* Second Else */
Temp_J = 1
/* Third Else */
I=1
J=1
Square[1,1] = 6
Next_Value = 7
Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 6 1 0
Row 2 0 5 3
Row 3 2 0 4

Main While Loop: 6th Iteration


/* First Else */
Temp_I = 3
/* Second Else */
Temp_J = 3
/* Third If */
/* Fourth Else */
I=2
Square[2,1] = 7
Next_Value = 8

Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 6 1 0
Row 2 7 5 3
Row 3 2 0 4

Main While Loop: 7th Iteration


/* First Else */
Temp_I = 1
/* Second Else */
Temp_J = 3
/* Third If */
I=2
J=3
Square[1,3] = 8
Next_Value = 9

Contents of Square after Statements

Column Colum Column


1 n2 3
Row 1 6 1 8
Row 2 7 5 3
Row 3 2 0 4
Main While Loop: 8th Iteration
/* First Else */
Temp_I = 3
/* Second Else */
Temp_J = 2
/* Third If */
I=3
J=2
Square[3,2] = 9
Next_Value = 10

Final Contents of Square

Column Colum Column


1 n2 3
Row 1 6 1 8
Row 2 7 5 3
Row 3 2 9 4

Sparse Matrices

Let us assume that we have a 6 x 6 matrix called Arr1 which contains the following elements:

Colum Column Colum Column Colum Column


n1 2 n3 4 n5 6
Row 1 18 0 13 0 0 0
Row 2 0 4 0 0 8 0
Row 3 0 0 9 0 0 0
Row 4 6 0 0 16 0 7
Row 5 0 32 0 10 0 0
Row 6 0 0 12 0 25 0
Figure 2-18 Sparse Matrix

If we look at our matrix, we see that it has many zero (0) entries. Such a matrix is called sparse.
There is no precise definition of when a matrix is sparse and when it is not but it is a concept which
can be intuitively recognized. In the matrix Arr1, only 12 out of 36 elements are nonzero and that is
sparse! If we consider that each element of an array occupies a portion of computer memory, we
can deduce that a lot of valuable computer space is wasted by sparse matrices. One may think that
the space occupied by 36 elements is negligible. But what happens if Arr1 had 1000 rows and 1000
columns (1000 x 1000) and at the same time it was sparse! Having to store all one million elements
in the computer when only a small subset is needed is senseless and would greatly affect the
efficiency of the program processing them.
A sparse matrix would require us to consider an alternate form of representation that would store
only the nonzero elements. This comes about because in practice many of the matrices we want to
deal with are large life our 1000 x 1000 example, but at the same time they are sparse. In figure 2-
18, 12 out of 36 elements of Arr1 are nonzero. We can therefore represent Arr1 as the following
array which we will call ArrNew.

Column Colum Column


1 n2 3
Row 1 6 6 12
Row 2 1 1 18
Row 3 1 3 13
Row 4 2 2 4
Row 5 2 5 8
Row 6 3 3 9
Row 7 4 1 6
Row 8 4 4 16
Row 9 4 6 7
Row 10 5 2 32
Row 11 5 4 10
Row 12 6 3 12
Row 13 6 5 25
Figure 9-19 Alternative Representation OF A Sparse Matrix

The elements [1,1] and [1,2] of ArrNew contain the number of rows and columns in the sparse
matrix Arr1. The location [1,3] contains the number of nonzero elements in Arr1. Hence in ArrNew:

ArrNew[1,1] = 6 Since Arr1 has 6 rows


ArrNew[1,2] = 6 Since Arr1 has 6 columns
ArrNew[1,3] = 12 Since Arr1 has 12 nonzero elements

Each succeeding row in ArrNew provides information about a nonzero element in Arr1. Column 1
contains the number of the row where the nonzero element is located;and column 2 contains the
number of the column where the nonzero element is located. Lastly, column 3 contains the value of
the nonzero element itself. For example, in ArrNew, row 5 contains the information

Column Colum Column


1 n2 3
Row 5 2 5 8
Figure 2-20 Row 5 of Array ArrNew

which effectively describes: Arr1[2,5]

In assigning values to ArrNew, the nonzero elements of Arr1 are referenced first by row and then by
column. This means that the row in ArrNew describing the nonzero element at Arr1[3,3] is filled
before the row describing the nonzero element at Arr1[4,1]. Additionally, the row in ArrNew
describing the nonzero element at Arr1[5,2] is created before the row describing the nonzero
element at Arr1[5,4]. Essentially, a table representing a sparse matrix will always have 3 columns
and k + 1 rows, where k is equal to the number of nonzero elements in the matrix.
The following procedure creates the array ArrNew which will represent the nonzero elements in the
sparse matrix Arr1. The procedure requires the following information as input:

1. Arr1 The sparse matrix


2. m The number of rows in the sparse matrix
3. n The number of columns in the sparse matrix

In addition, the algorithm makes use of the following variebles:

1. Num Contains the number of nonzero elements in Arr1


2. I, Row, Col Used as subscripts for the two arrays Arr1 and ArrNew

Sparse (Arr1,m,n)
{
Set Num to 0
Count_Nonzero()
Definen ArrNew with size [(Num+1) x 3]
Set ArrNew[1,1] to m
SetSet ArrNew[1,2]
ArrNew[1,3] to n
to Num
Set Row to 1
Set I to 2
While (Row ≤ m)
{
Set Col to 1
While (Col ≤ n)
{
If (Arr1[Row,Col] is NOT Zero) then
{
Set ArrNew[I,1] to Row
Set ArrNew[I,2] to Col
Set ArrNew[I,3] to Arr1[Row,Col]
Increment I
}
Increment Col
}
Increment Row
}
}
Count_Nonzero()
/* Determines the number of nonzero elements in Arr1 */
{
Set Row to 1
While (Row ≤ m)
{
Set Col to 1
While (Col ≤ n)
{
If (Arr1[Row,Col] is NOT zero) then
{
Increment Num
}
Increment Col
}
Figure 2-21 Algorithm For Representing Sparse Matrices

Let us analyze the pseudocode Sparse using the matrix in the figure below which we will call Arr1

Column Column Column


1 2 3
Row 1 2 0 0
Row 2 0 3 0
Row 3 0 0 0
Row 4 0 8 0
Figure 2-22 4 x 3 Sparse Matrix
Num = 0
/* Count_Nonzero */
Row = 1
Outer While Loop: 1st Iteration
Col = 1
Inner While Loop: 1st Iteration
/* IF Statement: Arr[1,1] not Zero */
Num = 1
Col = 2
Inner While Loop: 2ndIteration
Col = 3
Inner While Loop: 3rdIteration
Col = 4

Row = 2
Outer While Loop: 2ndIteration
Col = 1
Inner While Loop: 1st Iteration
Col = 2
Inner While Loop: 2ndIteration
/* IF Statement: Arr[2,2] not Zero */
Num = 2
Col = 3
Inner While Loop: 3rdIteration
Col = 4
Row = 3
Outer While Loop: 3rdIteration
Col = 1
Inner While Loop: 1st Iteration
Col = 2
Inner While Loop: 2ndIteration
Col = 3
rd
Contents of ArrNew after Iteration

Column Colum Column


1 n2 3
Row 1 4 3 3
Row 2
Row 3
Row 4
Row = 1
I=2
Outer While Loop: 1st Iteration
Col = 1
Inner While Loop: 1st Iteration
/* IF Statement: Arr1[1,1] is NOT Zero */
ArrNew[2,1] = 1
ArrNew[2,2] = 1
ArrNew[2,3] = 2
I=3
Col = 2

Contents of ArrNew after Iteration

Column Colum Column


1 n2 3
Row 1 4 3 3
Row 2 1 1 2
Row 3
Row 4
Inner While Loop: 2nd Iteration
Col = 3
Inner While Loop: 3rd Iteration
Col = 4
Row = 2
Outer While Loop: 2nd Iteration
Col = 1
Inner While Loop: 1st Iteration
Col = 2
Inner While Loop: 2nd Iteration
/* IF Statement: Arr1[2,2] is NOT Zero */
ArrNew[3,1] = 2
ArrNew[3,2] = 2
ArrNew[3,3] = 3
I=4
Col = 3
Inner While Loop: 3rd Iteration
Col = 4

Contents of ArrNew after Iteration

Column Colum Column


1 n2 3
Row 1 4 3 3
Row 2 1 1 2
Row 3 2 2 3
Row 4

Row = 2
Outer While Loop: 3rd Iteration
Col = 1
Inner While Loop: 1st Iteration
Col = 2
Inner While Loop: 2nd Iteration
Col = 3
Inner While Loop: 3rd Iteration
Col = 4
Row = 4
Outer While Loop: 4th Iteration
Col = 1
Inner While Loop: 1st Iteration
Col = 2
Inner While Loop: 2nd Iteration
/* IF Statement: Arr1[2,2] is NOT Zero */
ArrNew[4,1] = 4
ArrNew[4,2] = 2
ArrNew[4,3] = 8
I=5
Col = 3
Final Contents of ArrNew

Column Colum Column


1 n2 3
Row 1 4 3 3
Row 2 1 1 2
Row 3 2 2 3
Row 4 4 2 8

Inner While Loop: 3rd Iteration


Col = 4
Row = 4

Transpose

One of the operations that may be performed on matrices is to computer for the transpose of a
matrix. The transpose is a “copy” of a particular matrix wherein the elements in the [i,j] position are
transferred to the [j,i] position. In other words, we are interchanging the rows and the columns. The
elements in the diagonal will always remain in the same position since i = j. If the original matrix
has a dimension of m x n, the resulting transpose of the matrix will have a dimension of n x m. To
illustrate this concept, let us consider the figure below which represents a matrix called Arr1.

Column Column Column Column Column


1 2 3 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
Figure 2-23 4x 5 matrix
Called Arr1

The following matrix, Arr2, is the transpose of Arr1.

Column Column Column Column


1 2 3 4
Row 1 1 6 11 16
Row 2 2 7 12 17
Row 3 3 8 13 18
Row 4 4 9 14 19
Row 5 5 10 15 20
Where: Arr2[1,1] = Arr1[1,1]
Arr2[1,2] = Arr1[2,1]
Arr2[1,3] = Arr1[3,1]
Arr2[1,4] = Arr1[4,1]
And so on.

Figure 2-24 Transpose of Arr1

The pseudocode in figure 2-25 computes for the transpose of matrix. It accepts as input the
following information:

1. Arr1 The source matrix


2. m The number of rows in the matrix
3. n The numbers of columns in the matrix

Additionally the algorithm makes use of two variables, I and J, as subscripts.

Transpose (Arr1,m,n)
{
Define Arr2 with size [n x m]
Set I to 1
While (I ≤ m)
{
Set J to 1
While (J ≤ n)
{
Set Arr2[J,I] to Arr1[I,J]
Increment J
}
Increment I
}
}

Figure 2-25 Transpose Algorithm

Let us now use the array Arr1 presented in figure 2-23 to simulate the algorithm.

Define Arr2 to be a 5 x 4 array


I=1
Outer While Loop: 1st Iteration
J=1
Inner While Loop: 1st Iteration
Array2[1,1] = 1
J=2
Inner While Loop: 2nd Iteration
Array2[2,1] = 2
J=2
Inner While Loop: 3rd Iteration
Array2[3,1] = 3
Contents of Arr2 after Iteration

Column Column Column Column


1 2 3 4
Row 1 1
Row 2 2
Row 3 3
Row 4 4
Row 5 5

Outer While Loop: 2nd Iteration


J=1
Inner While Loop: 1st Iteration
Array2[1,2] = 6
J=2
Inner While Loop: 2nd Iteration
Array2[2,2] = 7
J=3
Inner While Loop: 3rd Iteration
Array2[3,2] = 8
J=4
Inner While Loop: 4th Iteration
Array2[4,2] = 9
J=5
Inner While Loop: 5th Iteration
Array2[5,2] = 10
J=6
I=3

Contents of Arr2 after Iteration

Column Column Column Column


1 2 3 4
Row 1 1 6
Row 2 2 7
Row 3 3 8
Row 4 4 9
Row 5 5 10

Outer While Loop: 3rd Iteration


J=1
Inner While Loop: 1st Iteration
Array2[1,3] = 11
J=2
Inner While Loop: 2nd Iteration
Array2[2,3] = 12
J=3
Inner While Loop: 3rd Iteration
Array2[3,3] = 13
J=4
Inner While Loop: 4th Iteration
Array2[4,3] = 14
J=5
Inner While Loop: 5th Iteration
Array2[5,3] = 15
J=6
I=4

Contents of Arr2 after Iteration

Column Column Column Column


1 2 3 4
Row 1 1 6 11
Row 2 2 7 12
Row 3 3 8 13
Row 4 4 9 14
Row 5 5 10 15

Outer While Loop: 4th Iteration


J=1
Inner While Loop: 1st Iteration
Array2[1,4] = 16
J=2
Inner While Loop: 2nd Iteration
Array2[2,4] = 17
J=3
Inner While Loop: 3rd Iteration
Array2[3,4] = 18
J=4
Inner While Loop: 4th Iteration
Array2[4,4] = 19
J=5
Inner While Loop: 5th Iteration
Array2[5,4] = 20
J=6
I=5
Final Contents of Arr2

Column Column Column Column


1 2 3 4
Row 1 1 6 11 16
Row 2 2 7 12 17
Row 3 3 8 13 18
Row 4 4 9 14 19
Row 5 5 10 15 20

REPRESENTATION OF ARRAYS

Arrays are usually represented as contiguous memory locations that are sequentially allocated. This
means that for an array with n elements, where each element is one memory word (2 bytes) long, n
consecutive words are allocated in memory. Since the size of an array is always fixed the amount of
memory required by an array is also fixed. The following figure illustrates how an array Arr1 with
sizen may be provided storage in memory. For the purpose of this discussion, let us assume that
each element in Arr1 is k words long.

Memory
Start  Arr1[1]

1st Word Arr1[i] Arr1[i]


Figure 2-26 Storage Allocation For A One-Dimensional Array With n Elements

Start denotes the location of this first word of the element Arr1[i]. The formula

Start + k (i – 1)
May be applied to determine the starting address of the ith element of Arr1. Consider the following
figure representing the memory allocation for array Arr2 which has a size of n and whose elements
are each 3 words long. Remember that each memory address corresponds to the location of one
word. Memory Address

1000
1001 Arr2[1]  Start
1002
1003
1004 Arr2[2]
1005

c Arr2[n]  1st Word of Arr2[n]


c+1

Figure 2-27 Storage Allocation For Array Arr2 With n 3-Word Elements

Using the formula above, the starting address of element Arr2[2] may be computed as follows:

Start + k (i – 1) = 1001 + 3 (2-1)


= 1001 + 3
= 1004

Similarly, calculating for the starting address of element Arr2[6], where 6 ≤ n, will result to:

Start + k (i – 1) = 1001 + 3 (6-1)


= 1001 + 15
= 1016

A computer’s memory is typically a one-dimensional array. This means that two dimensional arrays
and multi-dimensional arrays must be stored in memory in one dimension. There are two ways to do
this:

1. Row Major The Elements of the array are stored by row


2. Column Major The elements of the array are stored by column

Row Major Representation

Let us consider the row major representation of two-dimensional and multi-dimensional arrays. The
following figure illustrates how a 2 x 3 array called Arr1 would be allocated storage in a row major
fashion. Let us assume that each element of Arr1 is one word long.
Memory Address

2000
2001
2002 Arr1[1,1]
2003 Arr1[1,2]
2004 Arr1[1,3]
2005 Arr1[2,1]
2006 Arr1[2,2]
2007 Arr1[2,3]
2008

Figure 2-28 Row Major Representation Of the 2 x 3 Array Arr1

As can be seen in the figure, the elements of Arr1 are stored by row. This means that all elements
of row 1, from left to right, are allocated sequentially before any element of row 2. When using the
row major representation, the address of any element in the array may be computed via following
formula.

Start + k ( [ (i – n) n] + [j – 1])

Where:

1. Start The starting address of the array


2. k The number of words occupied by each element
3. i The row number of the element desired
4. j The column number of the element desired
5. n The number of columns in the array

For example, let assume that a 3 x 2 array called Arr1 whose elements are each one word long are
stored row major fashion as shown in the figure below:

Memory Address
1000
1001 Arr1[1,1]  Start
1002 Arr1[1,2]
1003 Arr1[2,1]
1004 Arr1[2,2]
1005 Arr1[3,1]
1006 Arr1[3,2]
1007

Figure 2-29 Row Major Major Representation Of the 3 x 2
Array Arr1

Using the formula, let us compute for the address of the element Arr1[3,1]
Start + k ([ (i – 1) n] + [j – 1]) = 1001 + 1 ( [ (3 – 1) 2] + [1 – 1])
= 1001 + 1 ( [ (2) 2] + 0)
= 1001 + 1 (4)
= 1005

Let us do the same exercise assuming that each element of Arr1 is two (2) words long. The storage
allocation of the elements would appear something like the figure below.

Memory Address
1000
1001 Arr1[1,1]  Start
1002
1003 Arr1[1,2]
1004
1005 Arr1[2,1]
1006
1007 Arr1[2,2]
1008
1009 Arr1[3,1]
1010
1011 Arr1[3,2]

Figure 2-30 Row Major Representation Of The 3 x 2 Array Arr1 Where Each Element = 2 Words

Computing for the address of element Arr1[2,2] using the same formula would result to:

Start + k ( [ (i – 1) n] + [j – 1]) = 1001 + 2 ( [ (2 – 1) 2] + [2 – 1])


= 1001 + 2 ( [ (1) 2] + 1)
= 1001 + 2 (3)
= 1007

Column Major Representation

When a two-dimensional and multi-dimensional array is stored column major, all elements in column
1 are allocated storage before any element in column 2. The following figure shows how the
elements of a 2 x 3 array called Arr1 is stored column major.

Memory
2000 Address
2001 Arr1[1,1]  Start
2002 Arr1[2,1]
2003 Arr1[1,2]
2004 Arr1[2,2]
2005 Arr1[1,3]
2006 Arr1[2,3]

Figure 2-31 Column Major Representation Of the 2 x 3 Array Arr1

As with the row major representation, it is possible to compute for the address of any element in the
array. The formula to be applied when the array is stored as column major is as follows:

Start + k ( [ (j – 1) m] + [i – 1])

Where:

1. Start The starting address of the array


2. k The number of words occupied by each element
3. i The row number of the element desired
4. j The column number of the element desired
5. n The number of columns in the array

Let assume that there exists a 2 x 3 array called Arr1 where each element is three words long.
Figure 2-23 shows how Arr1 may be represented in memory as column major.

Memory Address

1000
1001 Arr1[1,1]  Start
1002
1003
1004 Arr1[2,1]
1005
1006
1007 Arr1[1,2]
1008
1009
1010 Arr1[2,2]
1011
1012
1013 Arr1[1,3]
1014
1015
1016 Arr1[2,3]

Figure 2-32 Column Major Representation Of The 2 x 3 Array Arr1 Where Each Element = 3 Words

Let us now try to compute for the address of element Arr1[2,3]

Start + k ( [ (j – 1) m] + [i – 1]) = 1001 + 3 ( [ (3 – 1) 2] + [2 – 1])


= 1001 + 3 ( [ (2) 2] + 1)
= 1001 + 3 (4 + 1)
= 1001 + 3 (5)
= 1001 + 15
= 1016

EXERCISES

1. Given a one-dimensional array with 50 elements, construct an algorithm that will locate the
smallest and largest elements in the array.

2. The median of set of integers is the value which divides the set exactly in half. Given a one-
dimensional array whose elements are sorted from lowest to highest, the median can be
determined by applying the following rules:
a) If the number of elements in the array is odd, the element in the middle of the array
is the median.
b) If the number of elements in the array is even, the median is computed as the
average of the two middle elements.

Construct an algorithm that will accept as input the array and the number of elements in
the array and output all the array’s elements along with median.

3. Given two arrays, A and B, each containing N elements, construct an algorithm that would
interchange all element of A and B

4. The array Class consists of the set of students in a class of 45. Each element in the array
contains three types of information: 1) the student Name, 2) the Gender of the student and,
3) the final Grade of the student. Each type of information may be accessed via the following
format:
Class Name[subscript] for the Student Name
Class Gender[subscript] for the Gender
(contains “M” for Male and “F” for Female)
Class Grafe[subscript] for the Final Grade

Write a procedure that would perform the following:

a) Create two (2) arrays named Male and Female. The array Male should contain a
sorted list of all male students in the class. Similarly, array Female will contain a
sorted list of all female students in the class. The arrays Male and Female should
contain only the student names.
b) Print the following information:
1. Average grade of all Male students
2. Average grade of all Female students
3. Average grade of the class

5. Given a one one-dimensional array with 100 4-word elements, compute for the address of
the following elements. Assume that the starting address of the array is 2300.
a) Arr[5]
b) Arr[72]
c) Arr[99]

6. Show the magic square for a 5 x 5 matrix

7. Given an n x n matrix, write an algorithm to determine if the matrix is an identity matrix. A


matrix is called an identity matrix if the elements on the main diagonal are 1s and all other
elements are 0s.

8. Another kind of sparse matrix that arises often in numerical analysis is the tridiagonal matrix.
In this square matrix, all elements other than those on the major diagonal and on the
diagonals immediately above and below this one are zero.
Col 1 Col 2 Col 3 Col 4 Col 5 Col 6 Col 7 Col 8 Col 9 Col 10
Row 1 X X
Row 2 x X X
Row 3 x X X
Row 4 x X X
Row 5 x X X
Row 6 x X X
Row 7 x X X
Row 8 x X X
Row 9 x X X
Row 10 x x
Figure 2-33 Tridiagonal Matrix A
If the elements in the band formed by these diagonals are represented rowwise in an array
B, then A[1,1] will be stored at B[1], A[1,2] at B[2] and so on. If array B will have total of n
elements, create an algorithm that would determine the value of A[i,j] in B.

9. Using a 3 x 3, construct an algorithm that would allow two users to play the game tic-tac-
toe. Use the symbols “x” and “o” to represent the two values of the array.

10. Given a 25 x 50 array stored in memory in column major and where each element is 5 words
long, compute for the address of the following elements. Starting address of the array is
1500.
a) Arr[5,20]
b) Arr[19,43]
c) Arr[25,50]
Compute for the address of these elements if the array were stored in a row major fashion.

You might also like