2
CSCI101 - Assignment 2 Fall 2014 Ch. 5: Algorithms 1) Assuming that each of the following functions receives n as a parameter (n is a positive integers), write a recursive as well as an iterative pseudo code for them: A=2 +3 6 =0 Exp(x) =1+ 1! + 2 2! +…+ ! +… A procedure that takes as an argument N, and checks if it is prime. 2) Write a pseudo code of a procedure that takes as an argument a list x and returns: the number of elements in x the greatest element of x the last element of x true or false, depending on x being ordered or not another list with the elements of x without repetition the mean of the values in x 3) Write nested loops in a pseudo code for printing the following patterns: .... 1 ...2 ..3 .4 .... 1 ...22 ..333 .4444 .... 1 ...2. ..3.. .4...

Assignment_2.pdf

Embed Size (px)

Citation preview

Page 1: Assignment_2.pdf

CSCI101 - Assignment 2

Fall 2014

Ch. 5: Algorithms

1) Assuming that each of the following functions receives n as a parameter (n

is a positive integers), write a recursive as well as an iterative pseudo code

for them:

A=∑2𝑖+3𝑖

6𝑖𝑛𝑖=0

Exp(x) =1+𝑥

1!+𝑥2

2!+…+

𝑥𝑛

𝑛!+…

A procedure that takes as an argument N, and checks if it is prime.

2) Write a pseudo code of a procedure that takes as an argument a list x and

returns:

the number of elements in x

the greatest element of x

the last element of x

true or false, depending on x being ordered or not

another list with the elements of x without repetition

the mean of the values in x

3) Write nested loops in a pseudo code for printing the following patterns:

....1

...2

..3

.4

....1

...22

..333

.4444

....1

...2.

..3..

.4...

Page 2: Assignment_2.pdf

5 55555 5....

4) Consider the list: 1 3 7 9 11 20 25. How many times will the loop structure in the binary search algorithm execute if we are looking for: 4, 10, 25?

Ch.6: Programming Languages:

Problems from Chapter Review Problems

4,22,26,30

Ch.8: Data Abstractions

Problems from Chapter Review Problems

16, 29, 35

Second Question

Suppose the letter A is placed in an empty queue, followed by the letters B and C,

in that order. Then suppose that a letter is removed from the queue and the

letters D and E are inserted. List the letters that would be in the queue in the

order they would appear from head to tail. If a letter is now removed from the queue, which letter will it be?