41 views

Uploaded by Anthony Arbado

- Fast Methods for Solving Linear Diphantine Equations
- (Undergraduate Texts in Computer Science) David R. Brooks (auth.)-Problem Solving with Fortran 90_ For Scientists and Engineers-Springer-Verlag New York (1997).pdf
- 1_SAP BPC NW 10.0 Script Logic Implementation Guide V18
- lisp
- final project report
- codigos de Ejemplo
- Inline Function
- VB
- Csc115 Chapter 2a
- AutoCAD 2D 2 Module 24
- Function l to Create Posting Change.
- Chapter-01_1_Overview-of-ABAP-List-Viewer-ALV.ppt
- E-Learning Excel VBA Programming Lesson 2
- Configuring Message Control and Understanding How Message Control Works
- Using Invoker's Rights and Autonomous Transactions
- Enhancements(User Exits and Customer Exits)
- Chap 5Lesson01EmsysNewCProgrElements
- SAP_LSMW_CONV_00000015
- scimakelatex.13059.Jacinto.Bosch.Shelley.pdf
- Investigation of I/O Automata

You are on page 1of 86

An algorithm is a finite set of instruction that specify a sequence of operations to be carried out in order to solve a specific problem or class of problems.

A procedure that does not have the properties outlined below is not an algorithm and will, not in general, give the desired result when a program based on it is presented to a computer. Every algorithm must satisfy the following criteria:

There are zero or more quantities which are externally

1. INPUT

supplied.

At least one quantity is produced. Each instruction must be clear and unambiguous. If we trace out the instructions of an algorithm, then for all cases, the algorithm will terminate after a finite number of steps. 5. EFFECTIVENESS Every instruction must be sufficiently basic that it can, in principle, be carried out by a person using only pencil and paper. It is not enough that operation be definite as in #3, but it must also be feasible.

Finiteness

The execution of a programmed algorithm must be completed after a finite number of operations have been performed. Otherwise, we cannot claim that the execution produces a solution. the following figure shows an algorithm which embodies this property since every step is performed at least once.

1. 2. 3. 4. 5. 6. 7.

Input the numbers of integers to compare call it N. Input the first integer; call it NUM1. NUM1. Input the second integer; call it LARGE. Set up a counter representing the number of integers that have been read; call it COUNT. Set COUNT to 2. COUNT. Compare NUM1 with LARGE; if NUM1 is greater than LARGE, set LARGE; LARGE, LARGE to NUM1. NUM1. If COUNT equals N, display the value of LARGE and exit. Otherwise, increment COUNT by 1 and input the next integer to be compared. Return to Step 5.

DEFINITENESS

The representation of every step of an algorithm should have a unique interpretation. This condition simply means that every time an algorithm is presented with the same input data, the same results should be obtained.

Inputs are the data items presented to the algorithm. An algorithm has either no input or a predetermined number of them. Outputs are the data items presented to the outside world as the result of the execution of the program based on the algorithm.

EFFECTIVENESS

The instructions of an algorithm may only order a computer to performed task that it is capable of carrying out. A computer cannot performed an instruction if it has insufficient information or if the result of the execution would be inherently undefined.

REPRESENTING ALGORITHM

Algorithms are often presenting using a pseudo-language which is pseudobasically a combination of the constructs of a programming language together with informal English statements. Almost any common programming language could be used to represent algorithms.

PROCEDURES

Once the algorithm for doing one job is understood, it can be used as a step in another algorithm. An essential tool in programming, generalize the notion of an operator. Can be used to encapsulate parts of an algorithm by localizing in one section all the statements relevant to a certain aspect of a program.

Data type is a term used to refer to the kinds of data that a variable may hold in a given programming language. In FORTRAN, the data types are available are INTEGER, REAL, LOGICAL, COMPLEX, and DOUBLE PRECISION. In PL/I and C, there is a data type CHARACTER. The fundamental data type of SNOBOL is the character string and in LISP, it is the list (or S-expression). SWith every programming language, there is a set of built-in data builttypes. The term data structure, on the other hand, refers to a collection of variables, possibly of several different data types connected in various ways.

If a computer is merely a means to an end, then the means may be an algorithm but the end is the transformation of data. That is why a computer is often referred to as a data processing machine. Raw data is input to a computer and algorithms are used to transform them into refined data. So, instead of saying that computer science is a study of algorithms, alternatively, it is also a study of data. This includes: Machines that hold data Languages for describing data manipulation Foundations which describe what kinds of refined data can be produced from raw data Structures for representing data There is an intimate connection between the structuring of data and the synthesis of algorithms. In fact, a data structure an algorithm should be thought of as a unit, neither one making sense without the other.

1. 2. 3. 4.

PSEUDOCODES

Initially, algorithms are represented as flowcharts. The flowchart is a traditional graphical tool using standardized symbols. The pseudocode, on the other hand, is the textual presentation of an algorithms flowchart. Eventually the pseudocode of an algorithm becomes part of the programs documentation. Basically, a pseudocode is a kind of stylized English that could be translated more or less readily into a program. Using pseudocodes emphasizes the difference between algorithms and programs. Pseudocodes serve to arrive at the ready-to-code algorithm in a ready-toprocess of top-down program design, called stepwise refinement. toprefinement.

It was mentioned earlier that pseudocodes emerge from flowchart. Although the flowcharts of the algorithms discussed in this book are no longer presented, let us study how certain flowchart substructures may be translated into pseudocode statements. The following notation will be followed when presenting pseudocodes: The pseudocodes of the procedure begins with a procedure name followed by open and close parentheses, (). Words enclosed with open and close parentheses,(), represent parameters. An open and close curly bracket, {}, enclose the pseudocode statement within a procedure. In the diagrams below showing the pseudocode representation of some statements, words written italic represent user-defined entries such as uservariables. Words enclosed in the /* and */ pair are comments. A sequence of dot, ., means that the preceding statements may be repeated necessary.

1. 2. 3. 4.

5. 6.

IF Statement

The IF statement is a conditional statement. Figure 1-2(A) illustrates how 1the IF statement may appear in a flowchart. Figure 1-2(B) presents the 1translation of the flowchart representation into pseudocode format.

When using the IF statement, Statement 1 is executed when Condition is true. Processing then continues with Statement 2. 2. When Condition is false, Statement 1 is ignored and execution proceeds directly to Statement 2.

The IF-ELSE statement is a conditional statement that provides the IFcomputer with a choice between two options. Figure1-3(A) illustrates how Figure1the IF-ELSE statement my appear in a flowchart. Figure1-3(B) presents the IFFigure1translation of the flowchart representation into pseudocode format. Figure 1-3(A) Flowchart Representation Of The IF-ELSE Statement 1IF-

True

False

Condition

Statement 1 Statement 3

Statement 2

Figure 1-3(B) Pseudocode Representation Of The IF-ELSE Statement 1IFIF (Condition) then { Statement 1 } ELSE { Statement 2 } Statement 3

In the IF-ELSE statement, Statement 1 is executed when Condition is IFtrue. Processing then continues with Statement 3. when Condition is 3. false, Statement 2 is processed followed by Statement 3.

SWITCH Statement

The SWITCH statement is basically an expansion of the IF-ELSE IFstatement. It provides a mechanism for choosing an option among several possibilities. Figure1-4(A) illustrates how the SWITCH statement Figure1may appear in a flowchart. Figure 1-4(B) presents the translation of the 1flowchart representation into pseudocode format. Figure 1-4(A) Flowchart Representation Of The SWITCH Statement 1-

Var Value 1

True

False

Var Value 2

True

False

...

Statement n

Statement 1

Statement 2

Figure 1-4(B) Pseudocode Representation Of The SWITCH Statement 1SWITCH (Var) { CASE (Value1) { Statement 1 } CASE (Value 2) { Statement 2 } DEFAULT { Statement n } }

The logic behind the SWITCH statement may be describe as follows: If Var is Value 1 then Statement 1 is executed. If Var is not equal to Value 1 then Var is compared with Value 2. If Var equals Value 2, then Statement 2 is executed. If 2, Var is still not equal to Value 2, then Var is compared with the next Value in the 2, Value succeeding CASE statement until a match is found. When all the parameters of the CASE statements have been compared with no successful match, Statement n match, under DEFAULT is executed.

WHILE Statement

The WHILE statement allows the computer to repeatedly perform a series of statements based on a particular condition. Figure 1-5(A) illustrates ho 1the WHILE statement may appear in a flowchart. Figure 1-5(B) presents 1the translation of the flowchart representation into pseudocode format.

True

Condition

False

Statement 1

Statement 2

In the WHILE statement, Condition is tested before each iteration of the loop. Statement 1 is executed if and only if Condition is true; thus it s possible that Statement 1 is not executed at all. Once Condition is false, execution continues with Statement 2. 2.

Start

Fill kettle with water Turn on stove Put kettle on stove Water Is NOT boiling?

T F

Turn off stove Fill mug with boiled water Put 1 teaspoon of coffee in mug

Y

With sugar?

N

With milk?

Pseudocode Representation

Cup_of_Coffee() { Fill Kettle with Water Turn Stove On Put Kettle On Stove While (Water is NOT Boiling) { /* Do nothing. Repeat loop until water has boile.*/ } Turn Stove Off Fill Mug with Boiled Water Put One (1) Teaspoon of Coffee in Mug If (Coffee with Sugar) then { Put two (2) Teaspoon of Sugar in Mug } IF (Coffee with Milk) then { Pour one (1) Tablespoon of Milk in Mug } Stir contents of Mug }

RECURSION

Any given set of instruction which perform a logical operations, perhaps a very complex and long operations, can be group together as procedure. The procedure name and its parameters are viewed as a new instruction which can be used by other programs. Given the input-output specifications of a procedure, we dont inputeven have to know how the task is accomplished, only that it is available. The view of the procedure implies that it is invoked, executed and that it returns control to the appropriate place in the calling procedure. What this fails to stress is the fact that procedures may call themselves. Such procedures are called recursive procedures and the process by which a procedure calls itself is called recursion. recursion. There are two (2) types of recursion: Direct Recursion Recursion wherein procedures directly invoked themselves. Indirect Recursion Recursion wherein a chain invocation of procedures is permitted resulting in a closed circle (ex. Procedure A calls procedure B, which calls procedure C, which calls procedure A). Recursion is viewed as a somewhat mystical technique which is only useful for some very special class of problems (such as computing factorials). Essentially, any program that can be written using (1) the assignment statement, (2) the if-else ifstatement, (3) the while statement can also be written using (1) the assignment statement, (2) the if-else statement, and (3) recursion. if-

Reversing a String

Let us consider the procedure of reversing a character string S which contains n characters. S = X1Xn. Let us assume that: (a) S is a variable containing the string to be reserve. (b) SUBSTRING(S,i,j) is a function which returns the string XiXj where: i is an integer representing the position in S of the first character which will be returned j is an integer representing the total length of the to be returned For example: if S = Sample SUBSTRING(S,3,4) = mple (a) S II T represents the concatenation of two string (b) N is an integer variable initially set to the number of character in the string S.

Algorithm For Reversing A String Reverse (S) { Set N to the Length of string S If (N=1) then { RETURN (S) } Else { Decrement N RETURN(Reverse(SUBSTRING(S,2,N)) II SUBSTRING (S,1,1) } } Let us examine more closely how the Reverse procedure works. Let us assume that: S = Algorithms

Since the word Algorithms contains 10 characters, N will initially equal to 10. by tracing through the lines of the pseudocode the following results are recursively obtained. Each pass represents one level of recursion

Pass 1: N = 10 /* ELSE Statement: N is NOT Equal to 1*/ N=9 RETURN((Reverse(Igorithms)) RETURN((Reverse(Igorithms)) II A) Pass 2: N=9 /* ELSE Statement: N is NOT Equal to 1 */ N=8 RETURN((Reverse(gorithms)) RETURN((Reverse(gorithms)) II I) Pass 3: N=8 /* ELSE Statement: N is NOT Equal to 1 */ N=7 RETURN ((Reverse(orithms)) II g) ((Reverse(orithms))

Pass 4: N=7 /* ELSE Statement: N is NOT Equal to 1 */ N=6 RETURN((Reverse(rithms)) RETURN((Reverse(rithms)) II o) Pass 5: N=6 /* ELSE Statement: N is NOT Equal to 1 */ N=5 RETURN((Reverse(ithms)) RETURN((Reverse(ithms)) II r) Pass 6: N=5 /* ELSE Statement: N is NOT Equal to 1 */ N=4 RETURN((Reverse(thms)) RETURN((Reverse(thms)) II i) Pass 7: N=4 /* ELSE Statement: N is NOT Equal to 1 */ N=3 RETURN((Reverse(hms)) RETURN((Reverse(hms)) II t)

Pass 8: N=3 /* ELSE Statement: N is NOT Equal to 1 */ N=2 RETURN((Reverse(ms)) II *h*) Pass 9: N=2 /* ELSE Statement: N is NOT Equal to 1 */ N=1 RETURN((Reverse(s)) II *m*) Pass 10: /* IF Statement: N Equals 1 */ RETURN (*S*) To summarize the process by which the procedure Reverse generates the inverse of the string S, let us review the series of RETURN statements which are performed.

Reverse (igorithms II A Reverse (gorithms) II I II A Reverse (orithms) II g II I II A Reverse (rithms) II o II g II l II A Reverse (ithms) II r II o II g II l II A Reverse (thms) II i II r II o II g II l II A Reverse (hms) II t II i II r II o II g II l II A Reverse (ms) II h II t II i II r II o II g II l II A Reverse (s) II m II h II t II I II r II o II g II l II A s II m II h II t II i II r II o II l II A

Factorial

To compute for the factorial (N!) of a positive integer N, it is convenient to (N use the following recursive definition: Return 1 if N = 0 Return N(N-1) N(Nif N > 0

This means that the following factorial of 4 would be: 4! = 4 x 3! = 4 x 3 x 2! = 4 x 3 x 2 x 1! = 4 x 3 x 2 x 1 x 0! =4x3x2x1x1 = 24 The definition of the factorial of a number, N, may be logically expressed via the following pseudocode. Factorial (N) { If (N = 0) { RETURN (1) } Else { RETURN (N * (Factorial (N 1))) } }

Pass 1: /* ELSE Statement: N > 0 */ RETURN(4 * Factorial(3)) Pass 2: /* ELSE Statement: N > 0 */ RETURN(3 * Factorial(2)0 Pass 3: /* ELSE Statement: N > 0 */ RETURN(2 * Factorial(1)) Pass 4: /* ELSE Statement: N > 0 */ RETURN(1 * Factorial(0)) Pass 5: /* IF Statement: N = 0 */ RETURN (1)

Once again, let us summarize the process by which we arrived at the solution to 4! 4 * Factorial (3) 4 * 3 * Factorial (2) 4 * 3 * 2 * Factorial (1) 4 * 3 * 2 * 1 * Factorial (0) 4*3*2*1*1 24

1. 2. 3. 4. 5. 6.

Permutation

Given a set of N >= 1 elements, the problem is to print all possible permutations of this set. For example, if the set is {A, B, C}, then the set of permutation is {(A, B, C), (A, C, B), (B, A, C), (B, C, A), (C, A, B), (C, B, A)}. It is easy to see that the given N elements there are N! different permutations. A simple algorithm can be achieved by looking at the case of three elements {A, B, C}. The answer is obtained by printing:

1. 2. 3.

A followed by all permutations of (B, C) B followed by all permutations of (A, C) C followed by all permutations of (B, A) The expression followed by all permutations is the clue to recursion. It implies that we can solve the problem for a set with N elements if we had an algorithm which worked on N-1 elements. The considerations led to the following pseudocode for the permutation procedure. The permutations make use of the assumptions that: Procedure Permutation accepts as input three parameters (String1,K,N) (String1,K where: . String1 The character string whose permutation is to be produced. . K A number from 1 to N. For the first pass of procedure Permutation, K = 1. . N The numbers of characters in String1. String1. Example: If String1 = A,B,C then N = 3 A procedure Interchange (String1,k,i) exists whose function is to String1,k exchange the kth character with the ith character of String1.

1.

1.

The procedure makes use of the variable named String2 to temporarily hold the original value of String1 when the procedure is called. Permutation (String1, K, N) { If (K = N) then { Print (String1) RETURN } Set String2 to String1 Set l to K While (l <=N) } Interchange(String1, K, l) Permutation(String1, K+1, N) Set String1 to String2 Increment } }

Let us analyze the pseudocode above by generating all possible permutations of the string A, B, C. The parameters used during each recursive call are indicated together with the pass number.

Pass 1: Permutation (A, B, C, 1, 3) String2 = A, B, C I=1 While Loop: 1st Iteration String1 = A, B, C /* A result of interchange of positions (1, 1)*/ Permutation(A, Permutation(A, B, C, 2, 3) /* Proceed to Pass 2*/ /*Return from Pass 2*/ String1 =A, B, C I=2

While Loop: 2nd Iteration String1 = B, A, C /* A result for interchange of positions (1, 2)*/ Permutation(B, Permutation(B, A, C, 2, 3) /* Proceed to Pass 5*/ 5*/ /* Return from Pass 5*/ 5*/ String1 =A, B, C I=3 While Loop: 3rd Iteration String1 = C, B, A /* A result of interchange of positions (1, 2)*/ Permutation(C, Permutation(C, B, A, 2, 3) /* Proceed to Pass 8*/ 8*/ /* Return from Pass 8*/ String1 =A, B , C I=4 Pass 2: Permutation(A, B, C, 2, 3)

While Loop: 1st Iteration String1 = A, B, C Permutation(A, Permutation(A, B, C, 3, 3) /* Proceed to Pass 3*/ 3*/ /* Return from Pass 3*/ 3*/ String1 =A, B, C I=3 While Loop: 2nd Iteration String1 = A, C, B Permutation(A, Permutation(A, B, C, 3, 3) /* Proceed to Pass 4*/ 4*/ /* Return from Pass 4*/ String1 =A, B, C I=4 Pass 3: Permutation(A, B, C,3, 3) Print A, B, C Return

Pass 4: Permutation(A, C, B, 3, 3) Print A, C, B RETURN Pass 5: Permutation(B, A, C, 2, 3) String2 = B, A, C I=2 While Loop: 1st Iteration String1 = B, A, C Permutation(B, Permutation(B, A, C, 3, 3) /* Proceed to Pass 6*/ /* Return from Pass 6*/ String1 =B, A, C I=3 While Loop: 2nd Iteration String1 = B, C, A Permutation(B, Permutation(B, C, A, 3, 3) /* Proceed to Pass 7*/

/* Return from Pass 7*/ String1 =B, A, C I=4 While Loop: 3rd Iteration String1 = C, B, A Permutation(C, Permutation(C, B, A, 2, 3) /* Proceed to Pass 8*/ /* Return from Pass 8*/ String1 = A, B, C I=4 Pass 6: Permutation(B, A, C, 3, 3) Print B, A, C RETURN Pass 7: Permutation(B, C, A, 3, 3) Print B, C, A RETURN

Pass 8: Permutation(C, B, A, 2, 3) String2 = C, B, A I=2 While Loop: 1st Iteration String1 = C, B , A Permutation(C, Permutation(C, B, A, 3, 3) /* Proceed to Pass 9*/ /* Return from Pass 9*/ String1 = C, B, A I=3 While Loop: 2nd Iteration String1 = C, A, B Permutation(C, Permutation(C, B, A, 3, 3) /* Proceed to Pass 10*/ /* Return from Pass 10*/ String1 = C, A, B I=4

Summary of Output: A, B, C A, C, B B, A, C B, C, A C, B, A C, A, B

Base Value

In the procedure Factorial, the calculations is complete once we have Factorial, processed the case of 0!. FACTORIAL(0) Is what we call the base value. The base value is what we point wherein no recursive reference to the procedure is made. It is at this point that previous recursive calls are backbackup.

Depth Recursion

The depth of recursion refers to the number of times the procedure is called recursively in the process of evaluating a given argument. This means that the depth of recursion is always equal to the number of Passes minus one (Pass 1 i9s not a recursive call). For FACTORIAL(4), the depth of recursion is 4.

Arrays

One of simplest and most common type of data is the list. By definition, a list list. is an ordered set consisting of a variable number of elements to which addition and deletions may be made, if applicable. A list which displays the relationship of physical adjacency is called a linear list. 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 list 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) Floors 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 a computer memory, it may be stored in a table similar to the one in the figure below.

Last Name First Name Anderson Bautista Casado Canlas Triccia Marjorie Bianca Lorna

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) an) were a1, to an are elements of some set S. a1,

There are a variety of operations that may be performed on a linear lists. These include: Finding the length of the list , n. Reading the list from left-to-right (or right-to-left) left-to(or right-to-left) Retrieving the ith element in the list, 1 I n. n. Storing a new value into an ith position, 1 I n. n. Inserting a new element at position i, where 1 i n, causing elements n, numbered i,i +1, ,n to become numbered i+1, i+2, , n+1. +1, ,n i+1, i+2, n+1. Deleting the element at position i, where 1 i n, causing elements numbered i+1, i+2, ,n to become numbered i, i+1,,n-1. i+2, ,n i+1,,nSorting the elements in a list into ascending or descending order. Copying a list into another list. Merging a two or more lists to form a new list. Splitting a list into several sublists.

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

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 an array). Individual data items is an array are called elements. elements. Array elements are ordered according to subscript (an integer from 1 to n); for these reason they are sometimes called subscripted variables. variables. An array element is indicated by the array name followed by its subscripts, which appear a pair of square brackets.

Dimensionality of an Array

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

ONEONE-DIMENSIONAL ARRAYS

We have defined one-dimensional arrays as an array with only one onescript. Therefore, if Array1 is a one-dimensional array with 10 oneelements, these elements may be referenced as: Array1 [subscript] where 1 <= subscript1 <=10 All operations which may be performed on linear lists may also be done using arrays. Retrieving the ith Element of a One-Dimensional Array One-

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 as assume that array Arr1 has 10 elements. The following statement retrieves the 6th element of Arr1: Arr1: Arr1[6] where: Arr1 = array name 6 = subscript

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 input: Arr1 N The name of an array whose elements are to be read The number of elements in the array

1. 2.

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

We will now try to stimulate the algorithm Read_Arr by assuming that the array Arr1 contains the following 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] Iteration Ctr = 9 Summary of Output Dasher Dancer Prancer Comet Cupid Donner Blitzen Vixen

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 elements 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 important to note the subscript used to specify the elements 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 OneThe elements of an array cannot be physically deleted. This is because the sie of an array, once defined, always remains the same. Instead, array elements may be set to a specific value, for ex., blank, to signify deletion. The following statement deletes the 8th element in an array called Arr1 which has a size of 10 elements: Arr1[8] = where 8 = position of element to be deleted

Using an array called Arr1 with the size of N, the following pseudocode illustrates the procedure for sorting an array in an ascending order. The algorithm makes use of three other variables: Ctr1 Ctr2 Temp A subscript to an element of the array A subscript to another element of the array A variable used to temporarily hold the value of an element

Sort_Arr (Arr1, N) { Set Ctr1 to 1 While (Ctr1 <= N-1) N{ 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 } }

Let us assume that Arr1 is an array of size 4 containing the following elements: Arr[1] Arr[2] Arr[3] Arr[4] Raphaelo Michaelangelo Leonardo Donatello

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 Temp = Michaelangelo Temp = Leonardo Temp = Donatello

Contents of Arr1 after Inner Loop Arr[1] Arr[2] Arr[3] Arr[4] Michaelangelo Raphaelo Leonardo Donatello

Inner While Loop: 2nd Iteration Temp = Michaelangelo Arr1[1] = Leonardo Arr1[3] = Michaelangelo Ctr2 = 4

Contents of Arr1 after Inner Loop Arr[1] Arr[2] Arr[3] Arr[4] Leonardo Raphaelo Michaelangelo 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: 1st Iteration Temp = Raphaelo Arr1[2] = Michaelangelo Arr1[3] = Raphaelo Ctr2 = 4

Content of Arr1 after Inner Loop Donatello Arr[1] Michaelangelo Arr[2] Arr[3] Raphaelo Arr[4] Leonardo

Inner While Loop: 2nd Iteration Temp = Michaelangelo Arr1[1] = Leonardo Arr1[4] = Michaelangelo Ctr2 = 5

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

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

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 Arr2. 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 Arr2, 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 } } Algorithm for Copying A One-Dimensional Array One-

To simulate the pseudocode Copy_Arr (), we will use the array where N = 8. Ctr = 1 While Loop: 1st Iteration Arr2[1] = Dasher Ctr = 2 While Loop: 2nd Iteration Arr2[2] = Dancer Ctr = 3 While Loop: 3rd Iteration Arr2[3] = Prancer Ctr = 4 While Loop: 4th Iteration Arr2[4] = Comet Ctr = 5

While Loop: 5th Iteration Arr2[5] = Cupid Ctr = 6 While Loop: 6th Iteration Arr2[6] = Donner Ctr = 7 While Loop: 7th Iteration Arr2[7] = Blitzen Ctr = 8 While Loop: 8th Iteration Arr2[8] = Vixen Ctr = 9

Summary of Output: Arr2[1] Arr2[2] Arr2[3] Arr2[4] Arr2[5] Arr2[6] Arr2[7] Arr2[8]

Dasher Dancer Prancer Comet Cupid Donner Blitzen Vixen

Merging Two One-Dimensional Arrays OneGiven three arrays: Arr1, Arr2,and Arr3, where Arr1 and Arr2 are sorted Arr1, Arr2,and Arr3, 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 input the arrays Arr1 and Arr2 along with the following information: N1 N2 The size of array Arr1 The size of array Arr2

1. 2.

The algorithm also uses three variables, Ctr1, Ctr2, and Ctr3, as Ctr2, Ctr3, subscripts to the three arrays.

Merge_Arr (Arr1, Arr2, N1, N2) { Set Ctr1 to 1 Set Ctr2 to 1 Set Ctr3 to 1 While ((Ctr1 <= N1) OR (Ctr2 <= N2)) { If (Ctr1 > N1) then { Copy_from_Arr2() } Else { If (Ctr2 > N2) then { Copy_from_Arr1() } Else { If (Arr1[Ctr1] < Arr2{Ctr2] ) then {

/* First Condition */

/* Second Condition */

/* Third Condition */

Copy_from_Arr1() } Else } Copy_from_Arr2() } } } Increment Ctr3 } } Copy_from_Arr1 () { Set Arr3[Ctr3] to Arr1[Ctr1] Increment Ctr1 } Copy_from_Arr2 () { Set Arr3[Ctr3] to Arr2{Ctr2] Increment Ctr2 } /* Fourth Condition */

1.

Arr1 is a one-dimensional array of size 4 containing the following elements: oneArr1[1] Arr1[2] Arr1[3] Arr1[4] Dopey Grumpy Happy Sleepy

2.

Arr2 is a one-dimensional array of size 3 containing the following elements: oneArr2[1] Arr2[2] Arr2[3] Bashful Doc Sneezy

3.

Arr3 is an array of size 7 containing blanks. Ctr1 = 1 Ctr2 = 1 Ctr3 = 1 While Loop: 1st Iteration /*Fourth Condition*/ /*Copy_from_Arr2*/ Arr3[1] = Bashful Ctr2 = 2 Ctr3 = 2 While Loop: 3rd Iteration /*Third Condition*/ /*Copy_from_Arr1*/ Arr3[3] = Dopey Ctr1 = 2 Ctr3 = 4 While Loop: 2nd Iteration /*Fourth Condition*/ /*Copy_from_Arr2*/ Arr3[2] = Doc Ctr2 = 3 Ctr3 = 3 While Loop: 4th Iteration /*Third Condition*/ /*Copy_from_Arr1*/ Arr3[4] = Grumpy Ctr1 = 3 Ctr3 = 5

While Loop: 5th Iteration /*Third Condition*/ /*Copy_from_Arr1*/ Arr3[5] = Happy Ctr1 = 4 Ctr3 = 6

While Loop: 6th Iteration /*Third Condition*/ /*Copy_from_Arr1*/ Arr3[6] = Sleepy Ctr1 = 5 Ctr3 = 7

While Loop: 7th Iteration /*First Condition*/ /*Copy_from_Arr2*/ Arr3[7] = Sneezy Ctr2 = 4 Ctr3 = 8

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

1. 2.

The pseudocode for splitting 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, which each element is a record containing the following fields: Student Name Student Number Let us further suppose that the field Student Number has the format: YYYY-nnnnn where: 90<= YY<= 92 The whole record of any element i may simply be reference 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 Arr1.Studnum[i] To retrieve the Student Number field of element i The pseudocode Split_Arr() will divide Arr1 into the following three arrays:

1. Arr90 An array which will contain all records whose Student Number field has YY = 90 2. Arr91 An array which will contain all records whose Student Number field has YY = 91 3. Arr92 An array which will contain all records whose Student Number field has YY = 92

After dividing Arr1 into Arr90, Arr91 and Arr92, Split_Arr() will print the names of the students under each new array along with appropriate headings

Split_Arr (Arr1, N1) { Set Ctr1 to 1 Set Ctr90 to 1 Set Ctr91 to 1 Set Ctr92 to 1 While (Ctr1 <= N1) { Switch (YY of Arr1.Studnum[Ctr1]) { Case (90) { Set Arr90[Ctr90] to Arr1[Ctr1]) Increment Ctr90 } Case (91) { Set Arr91[Ctr91] to Arr1[Ctr1] Increment Ctr91

} } Increment Ctr1 } If (Ctr90 > 1) then /*First IF Statement*/ { Print_Arr90 (Arr90, Ctr90) } If (Ctr91 > 1) then /*Second Statement*/ { Print_Arr91 (Arr91, Ctr91) } If (Ctr92 > 1) then /*Third 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 Students Numbers beginning with 92: Print Set l to 1 While (l < Ctr92) { Print Arr92.StudName[l] Increment l } Print }

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

Student Number Arr1[1] Arr1[2] Arr1[3] Arr1[4] Arr1[5] Arr1[6] 9090-00001 9292-08888 9090-02323 9191-00463 9090-87361 9191-04520 Student Name Hewey Duck Mickey Mouse Dewey Duck Donald Duck Lewey Duck Daisy Duck

Ctr = 1 Ctr90 = 1 Ctr91 = 1 Ctr92 = 1 While Loop: 1st Iteration /*Case 90*/ Arr90[1] = 90-00001Hewey Duck 90Ctr90 = 2 Ctr1 = 2 While Loop: 2nd Iteration /*Default*/ Arr92[1] = 92-08888Mickey Mouse 92Ctr92 = 2 Ctr1 = 3

While Loop: 3rd Iteration /*Case 90*/ Arr90[2] = 90-02323Dewey Duck 90Ctr91 = 3 Ctr1 = 4 While Loop: 4th Iteration /*Case 91*/ Arr91[1] = 90-00463Donald Duck 90Ctr91 = 2 Ctr1 = 5 While Loop: 5th Iteration /*Case 90*/ Arr90[3] = 90-87631Lewey Duck 90Ctr90 = 4 Ctr1 = 6 While Loop: 6th Iteration /*Case 91*/ Arr91[2] = 91-04520Daisy Duck 91Ctr91 = 3 Ctr1 = 7

/*First IF Statement*/ /*Print_Arr90()*/ Print Students with Student Numbers beginning with 90: Print l=1 While Loop: 1st Iteration Print Hewey Duck l=2 While Loop: 2nd Iteration Print Dewey Duck l=3 While Loop: 3rd Iteration Print Lewey Duck l=4 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: Donald Duck Daisy Duck Students with Student Numbers beginning with 92: Mickey Mouse

Student Number Student Name Hewey Duck Dewey Duck Lewey Duck

Student Number

Arr91[1] Arr91[2]

9191-00463 9191-04520

Student Number

Arr92[1]

9292-08888

TwoTwo-Dimensional Arrays

A two-dimensional array is an array with two subscripts. The first subscript twois called the row and the second subscript is referred to as a column. column. Because of this property, two-dimensional arrays are often called tables or twomatrices. To reference an element of a two-dimensional array, both the row twonumber 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. twothe figure below shows an example of a two-dimensional array called Arr1 twocontaining 3 rows and 4 columns. Column 1 Row 1 Row 2 Row 3 Where: 1 2 3 Column 2 2 4 6 Arr1[2,1] = 2 Arr1[2,2] = 4 Arr1[2,3] = 6 Arr1[2,4] = 8 Column 3 3 6 9 Column 4 4 8 12 Arr1[3,1] = 3 Arr1[3,2] = 6 Arr1[3,3] = 9 Arr1[3,4] = 12

The following is a list of the basic operations that can be performed on twotwo-dimensional arrays. In the list, m represents the number of rows in the array and n the number of columns. Retrieving the [i,j]th element in the array, 1 i m and 1 j n [i,j]th 1 Reading all the elements in the array row-wise (or column-wise) rowcolumnStoring a new value into the [i,j]th position, 1 i m and 1 j n [i,j]th Deleting the element at position [i,j], where 1 i m and 1 j n [i,j], Copying an array Merging and splitting may also be performed on two-dimensional arrays. twoHowever, these are no longer basic operations because they require a close study of the application for which they will be used. Additionally, there is little difference between the pseudocodes of one-dimensional oneand two-dimensional arrays for which the first three operations, therefore, twowe will no longer elaborate in them. With regard to copying of 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 twotwo-dimensional arrays.

1. 2. 3. 4. 5.

To illustrate the procedure for copying a two-dimensional array into another array, we twowill use the two arrays: Arr1 and Arr2. Arr1 will be our source array having m rows and Arr2. 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 l } }

To simulate the pseudocode Copy_2Dim (), we will use the two-dimensional twoarray, Arr1. l=1 Outer While Loop: 1st Iteration J=1 Inner While Loop: 1st Iteration Arr2[1,1] = 1 J=2 Inner while Loop: 2nd Iteration Arr2[1,2] = 2 J=3 Inner While Loop: 3rd Iteration Arr2[1,3] = 3 J=4 Inner While Loop: 4th Iteration Arr2[1,4] = 4 J=5 l=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 l=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

Result of Execution

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

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 twocontains two While loops. If our array had three dimensions then pseudocode would have had three While loops. Additionally, our algorithm copied first then Arr1[1,2] followed by Arr1[1,3], and so on. It is possible to copy a two-dimensional array by twocolumn. The following pseudocode shows how this is done. If you will notice, the algorithm is basically similar to Copy_2Dim. Copy_2Dim { 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 } }

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 twomatrices. A matrix is a mathematical object which arises in many physical problems.

- Fast Methods for Solving Linear Diphantine EquationsUploaded byhumejias
- (Undergraduate Texts in Computer Science) David R. Brooks (auth.)-Problem Solving with Fortran 90_ For Scientists and Engineers-Springer-Verlag New York (1997).pdfUploaded byMaulanaSyarifAl-KafieSatrya
- 1_SAP BPC NW 10.0 Script Logic Implementation Guide V18Uploaded byAngel Ammulu
- lispUploaded bypostscript
- final project reportUploaded byapi-311032696
- codigos de EjemploUploaded byIcehellFrias
- Inline FunctionUploaded byRaghavendra Rajendra Basvan
- VBUploaded byrithikaasakthisasi
- Csc115 Chapter 2aUploaded bynajib9994899
- AutoCAD 2D 2 Module 24Uploaded byalvin balisbis
- Function l to Create Posting Change.Uploaded byaleksegil
- Chapter-01_1_Overview-of-ABAP-List-Viewer-ALV.pptUploaded bynivas
- E-Learning Excel VBA Programming Lesson 2Uploaded byperepavijay
- Configuring Message Control and Understanding How Message Control WorksUploaded byNickHansen
- Using Invoker's Rights and Autonomous TransactionsUploaded byCatalina Achim
- Enhancements(User Exits and Customer Exits)Uploaded bylancelot630
- Chap 5Lesson01EmsysNewCProgrElementsUploaded bystupidscribd99
- SAP_LSMW_CONV_00000015Uploaded bytabish24
- scimakelatex.13059.Jacinto.Bosch.Shelley.pdfUploaded byJohn Garmonsway
- Investigation of I/O AutomataUploaded byHarry Bo
- A New Measure of Software Complexity Based on Cognitive WeightsUploaded byමරසෙන් පරදමු
- Robotics 1Uploaded byJohnatan Zeta
- Pricing Configuration is Same for Regular Sales OrderUploaded bysomu_suresh5362
- SV Assignment QuestionsUploaded byNarasimha Swamy
- Assignment 5Uploaded byKenneth Wong
- ARRAYSUploaded bySikander025
- LSMW Tool_V1 0 - 25_05_2012 training by Juan Caño.pptUploaded byDaniel
- Maze RoutingUploaded byneerajbakshi
- COCO Performance AssesmentUploaded bymabdulkadhar
- It210 Course SyllabusUploaded byGrag Bauer

- Spring Amqp Net ReferenceUploaded byhenry_fis
- RM_2-0.pdfUploaded bypahia3118
- 3HAC043508-001Uploaded bybozickovic
- CS201-2008_solved 1 (3)Uploaded byMuhammad Irfan
- C Programming and Data Structure_notesUploaded bySupragy Bhatnagar
- EmbeddedgurusUploaded bysatish
- Burke Et Al-NIL Notes Release 0259-1983Uploaded byAaron Nelson
- User PropertiesUploaded byKavya Cheppalli
- Java TutorialUploaded byMumbai Academics
- 07215108Uploaded byleandrolms
- AcPaper 2Uploaded byKedar Shelke
- j Introtojava1 PDFUploaded byRaúl Oramas Bustillos
- C# PrintUploaded byFenil Desai
- ug_fifoUploaded bySravanthi Ramini
- FIFO USER GUIDE.pdfUploaded bySugar Blues
- 11Uploaded byAli Ahmed
- Lab NotesUploaded byMohd Yaseen
- SAP Coding Standards ERPAMSUploaded byNaresh Vepuri
- 02 Minerals Library Basic Objects 5p1s4aUploaded byman_y2k
- Java Notes 2Uploaded bygurusodhii
- Combined Queries in Web IntelligenceUploaded bykpallu
- Language Processors Rr320504Uploaded byNizam Institute of Engineering and Technology Library
- JavascriptUploaded bySabari Nathan
- Smallworld-Pathway-Training-2014.pdfUploaded byLinder Reyna
- XLSFormUploaded byRifqi Oktavianto
- Complete C & Data Structure NotesUploaded byLatchireddy Manojkumar
- Blackcat usbUploaded byMichael Russell
- ArraysUploaded byJake Chou
- ATGProgGuideUploaded byGaurav Pawar
- lm23.pdfUploaded byGrace Vasquez Calle