Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
santos-discrete_math_lecture_notes_2.pdf
Скачиваний:
23
Добавлен:
16.03.2016
Размер:
691.48 Кб
Скачать

Chapter 1

Pseudocode

In this chapter we study pseudocode, which will allow us to mimic computer language in writing algorithms.

1.1 Operators

1 Definition (Operator) An operator is a character, or string of characters, used to perform an action on some entities. These entities are called the operands.

2 Definition (Unary Operators) A unary operator is an operator acting on a single operand.

Common arithmetical unary operators are + (plus) which indicates a positive number, and (minus) which indicates a negative number.

3 Definition (Binary Operators) A binary operator is an operator acting on two operands.

Common arithmetical binary operators that we will use are + (plus) to indicate the sum of two numbers and (minus) to indicate a difference of two numbers. We will also use (asterisk) to denote multiplication and / (slash) to denote division.

There is a further arithmetical binary operator that we will use.

4 Definition (mod Operator) The operator mod is defined as follows: for a 0, b > 0,

a mod b

is the integral non-negative remainder when a is divided by b. Observe that this remainder is one of the b numbers 0, 1, 2, . . . , b 1.

In the case when at least one of a or b is negative, we will leave a mod b undefined.

5 Example We have

38 mod 15 = 8,

15 mod 38 = 15,

1961 mod 37 = 0,

and

1966 mod 37 = 5,

for example.

1

2

Chapter 1

6 Definition (Precedence of Operators) The priority or precedence of an operator is the order by which it is applied to its operands. Parentheses ( ) are usually used to coerce precedence among operators. When two or more operators of the same precedence are in an expression, we define the associativity to be the order which determines which of the operators will be executed first. Left-associative operators are executed from left to right and right-associative operators are executed from right to left.

Recall from algebra that multiplication and division have the same precedence, and their precedence is higher than addition and subtraction. The mod operator has the same precedence as multiplication and addition. The arithmetical binary operators are all left associative whilst the arithmetical unary operators are all right associative.

7 Example 15

3 4 = 3 but (15 3) 4

= 48.

8 Example

12

(5 mod 3) = 24 but (12 5) mod 3 = 0.

9 Example

12

mod 5 + 3 3 = 11 but 12

mod (5 + 3) 3 = 12 mod 8 3 = 4 3 = 12.

1.2 Algorithms

In pseudocode parlance an algorithm is a set of instructions that accomplishes a task in a finite am ount of time. If the algorithm produces a single output that we might need afterwards, we will use the word return to indicate this output.

10 Example (Area of a Trapezoid) Write an algorithm that gives the area of a trapezoid whose height is h and bases are a and b.

Solution: One possible solution is

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Algorithm 1.2.1: AREATRAPEZOID(a, b, h)

 

 

return (h

a + b

 

 

 

 

 

)

 

 

 

2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11 Example (Heron's Formula) Write an algorithm that will give the area of a triangle with sides a, b, and c.

Solution: A possible solution is

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Algorithm 1.2.2: AREAOFTRIANGLE(a, b, c)

 

 

 

 

 

 

È

 

 

 

 

 

 

 

 

 

return (.25 (a + b + c) (b + c a) (c + a b) (a + b c))

 

 

 

 

 

 

 

 

 

 

 

 

 

 

We have used Heron's formula

 

 

 

 

 

 

 

 

 

 

 

È

 

 

 

 

1

È

 

 

 

 

Area = s(s a)(s b)(s c) =

 

(a + b + c)(b + c a)(c + a b)(a + b c),

 

 

4

where

s = a + b + c

2

is the semi-perimeter of the triangle.

12 Definition The symbol is read “gets” and it is used to denote assignments of value.

2

Arrays

3

13 Example (Swapping variables) Write an algorithm that will interchange the values of two variables x and y, that is, the contents of x becomes that of y and viceversa.

Solution: We introduce a temporary variable t in order to store the contents of x in y without erasing the contents of y:

 

 

><8

 

 

 

 

Algorithm:

1.2.3: SWAP(x, y)

 

 

 

 

 

t x

comment: First store x in temporary place

 

 

 

x y

comment: x has a new value.

 

 

 

y t

comment: y now receives the original value of x.

 

 

 

 

 

 

 

 

 

 

If we approached the problem in the following manner

Algorithm8<>: 1.2.4: SWAPWRONG(x, y)

x 5 y 6

x y comment: x = 6 now.

y x comment: y takes the current value of x, i.e., 6.

we do not obtain a swap.

14 Example (Swapping variables 2) Write an algorithm that will interchange the values of two variables x and y, that is, the contents of x becomes that of y and viceversa, without introducing a third variable.

Solution: The idea is to use sums and differences to store the variables. Assume that initially x = a and y = b.

 

 

 

 

><8

 

 

 

 

 

Algorithm:

1.2.5: SWAP2(x, y)

 

 

 

 

 

 

x x + y

comment: x = a + b and y = b.

 

 

 

y x y

comment: y = a + b b = a and x = a + b.

 

 

x x y

comment: y = a and x = a + b a = b.

 

 

 

 

 

 

 

 

 

 

 

1.3 Arrays

 

˜

 

 

 

15 Definition An array is an aggregate of homogeneous types. The length of the array is the number of entries it has.

A 1-dimensional array is akin to a mathematical vector. Thus if X is 1-dimensional array of length n then

X = (X [0], X [1], . . . , X [n 1])

and all the n coordinates X [k] belong to the same set. We will follow the C-C++-Java convention of indexing the arrays from 0. We will always declare the length of the array at the beginning of a code fragment by means of a comment.

A 2-dimensional array is akin to a mathematical matrix. Thus if Y is a 2-dimensional array with 2 rows and 3 columns then

Y =

Y [0][0]

Y [0][1]

Y [0][2]

Y [1][0]

Y [1][1]

Y [1][2] .

3

4

8

Chapter 1

1.4 8If-then-else Statements

16 Definition The If-then-else control statement has the following syntax: if expression

<statementA 1

.

then :..

statementA I <statementB 1

.

else :..

statementB J

and evaluates as follows. If expression is true then all statementA 's are executed. Otherwise all statementB's are executed.

17 Example (Maximum of 2 Numbers) Write an algorithm that will determine the maximum of two numbers.

Solution: Here is a possible approach.

 

 

 

 

 

Algorithm 1.4.2: MAX(x, y)

 

 

 

 

 

 

if x y

 

 

 

then return (x)

 

 

 

else return (y)

 

 

 

 

18 Example (Maximum of 3 Numbers) Write an algorithm that will determine the maximum of three numbers.

Solution: Here is a possible approach using the preceding function.

 

 

 

 

Algorithm 1.4.3: MAX3(x, y, z)

 

 

 

 

 

 

if MAX(x, y) ≥ z

 

 

 

then return (MAX(x, y))

 

 

 

else return (z)

 

 

 

 

19 Example (Compound Test) Write an algorithm that prints “Hello” if one enters a number

between 4 and 6 (inclusive) and

“Goodbye” otherwise. You are not allowed to use any boolean o perators like and, or, etc.

 

 

Solution: Here is a possible answer.

 

 

 

 

 

 

Algorithm 1.4.4: HELLOGOODBYE(x)

 

 

 

 

 

 

if x >= 4

 

 

 

 

8

 

 

 

 

<if x <= 6

 

 

 

then : then output (Hello.)

 

 

 

 

else output (Goodbye.)

 

 

 

else output (Goodbye.)

 

 

 

 

4

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]