CSC 255 Objects and Algorithms HW 1

advertisement
CSC 255
Objects and Algorithms
HW 1
Student Name
Section
Instructor
Due Date
Part
Maximum Points
1
25 points
2
25 points
3
25 points
4
25 points
Total
100 points
Your Score
Textbook Reading Assignment
Thoroughly read Chapter(s) 1 in your Applied Data Structures with C + + textbook.
Part 1 Glossary Terms
Define, in detail, each of these glossary terms from the realm of Web programming logic and design and
computer topics, in general. If applicable, use examples to support your definitions. Consult your notes
or course textbook(s) as references or the Internet by visiting Web sites such as:
http://www.askjeeves.com or
(a)
Data Abstraction
(b)
Big - O
(c)
Big - 
(d)
Indirect Recursion
(e)
Persistence ( of an object )
http://www.webopedia.com
Part 2 Exercises - Programming with Objects
For each of the following, enter T if the statement is TRUE otherwise enter F for FALSE.
(1)
(2)
(3)
(4)
(5)
(6)
(7)
_____ If the run time of an algorithm is given as O ( n ) , then the run time is
proportional to the size of the data.
_____ When computing Fibonacci numbers for large n , an iterative function is
typically faster than a recursive one.
_____ The multiplication algorithm for multiplying two n  n matrices must be at least
O ( n ).
_____ Inheritance tends to enable software engineers to build software applications
at a slower rate.
_____ Multiplying a 4 row by 2 column matrix with a 2 row by 2 column matrix
produces a product matrix having 8 separate matrix elements.
_____ Key comparison sorting algorithms may run faster than O ( n log n ) .
_____ Multiplying the individual elements of a 2 row by 3 column matrix with the
elements of a 3 row by 1 column matrix requires 18 separate multiplications.
(8)
_____ The function f ( n )  2 n is most likely O ( 2 n ) .
© Copyright 2012 by P.E.P.
Page 1 of 2
Objects and Algorithms
CSC 255
Student Name
(9)
(10)
HW 1
Section
_____ The computing time for a process given by the function T ( n )  n
2
2
 2 is
most likely O ( n ) .
_____ The order of magnitude of the binary search algorithm is given by O ( log 2 n ) .
This means that one will not require more than 15 comparisons to find a given
name in a list containing 20,000 names.
Part 3 Exercises - Programming with Objects
(1)
One of the simplest of the sorting algorithms is the bubble sort. During such a sort, the
larger elements " bubble up " to the top ( the high end ) of the array similar to the
bubbles in a carbonated beverage. In the best case, the complexity of a bubble sort is
O ( n 2 ) . This means that for large values of n , the number of loop iterations tends to
be proportional to n 2 . Therefore, if one large array is one - half the size of another, it
should take about _____________ times as long to sort. ( fill in the blank )
(2)
The computing time for a process is given by the function: T ( n )  n  5 log 2 n
What order of magnitude is the best big O notation, to describe this computing time?
3
Part 4 Exercises - Programming with Objects
( Sieve of Eratosthenes )
The method of Eratosthenes to find all prime numbers less than or equal to a given
integer n is given as follows:
Make a list of all integers less than or equal to n ( and greater than one ). Cross out
the multiples of all primes less than or equal to the square root of n . The numbers
that remain are the primes numbers.
The method of Eratosthenes to find all prime numbers less than or equal to a given
integer n is outlined in the following algorithm:
STEP 1
Write down the numbers from 1 to 100 ( or any desired range )
STEP 2
Start with two ( the first prime number )
STEP 3
Eliminate all its multiples
STEP 4
Advance to the next prime ( the next number on the list which you have
not yet eliminated )
STEP 5
Return to STEP 3 and repeat as many times as necessary
For example, to find all the primes less than or equal to 25 , first list the numbers from 2
to 25 , as shown below. Then apply the above algorithm.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
This algorithm can be written in pseudocode as follows:
Eratosthenes(n) {
a[1] = 0
for i = 2 to n do a[i] = 1
p = 2
while p 2 < n do {
j = 2p
while (j < n) do {
a[j] = 0
j = j + p
}
repeat p = p + 1 until a[p] = 1
}
return(a)
}
Write a complete program that uses the Sieve of Eratosthenes algorithm to list all prime
numbers from 1 to 1,000 . Submit the source code and screen snapshots of the program.
© Copyright 2012 by P.E.P.
Page 2 of 2
Download