0% found this document useful (0 votes)
39 views3 pages

Chapter3 - Test Bank

The document contains 8 questions about algorithms on lists of integers. The questions cover topics like counting integers greater than a threshold, finding the second largest integer, locating the last even integer, calculating the average of the max and min, and using binary search. The answers then provide algorithms and descriptions to address each question, explaining how to approach problems involving analysis and manipulation of integer lists.

Uploaded by

busati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Chapter3 - Test Bank

The document contains 8 questions about algorithms on lists of integers. The questions cover topics like counting integers greater than a threshold, finding the second largest integer, locating the last even integer, calculating the average of the max and min, and using binary search. The answers then provide algorithms and descriptions to address each question, explaining how to approach problems involving analysis and manipulation of integer lists.

Uploaded by

busati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Questions for Chapter 3

1. Describe an algorithm that takes a list of n integers a1 , a2 , . . . , an and finds the number of integers each
greater than five in the list.
2. Describe an algorithm that takes a list of integers a1 , a2 , . . . , an (n ≥ 2 ) and finds the second-largest integer in
the sequence by going through the list and keeping track of the largest and second-largest integer encountered.
3. Describe an algorithm that takes a list of n integers ( n ≥ 1 ) and finds the location of the last even integer
in the list, and returns 0 if there are no even integers in the list.
4. Describe an algorithm that takes a list of n integers ( n ≥ 1 ) and finds the average of the largest and smallest
integers in the list.

5. Express a brute-force algorithm that finds the second largest element in a list a1 , a2 , . . . , an ( n ≥ 2 ) of
distinct integers by finding the largest element, placing it at the beginning of the sequence, then finding the
largest element of the remaining sequence.
6. Express a brute-force algorithm that finds the largest product of two numbers in a list a1 , a2 , . . . , an ( n ≥ 2 )
that is less than a threshold N .
7. Describe in words how the binary search works.
8. List all the steps the binary search algorithm uses to search for 27 in the following list: 5, 6, 8, 12, 15, 21, 25, 31 .
Chapter 3 Test Bank 495

Answers for Chapter 3


1. procedure greaterthanfive(a1 , . . . , an : integers)
answer := 0
for i := 1 to n
if ai > 5 then answer := answer + 1
return answer
2. procedure secondlargest(a1 , . . . , an : integers)
largest := a1
secondlargest := a2
if a2 > a1 then
secondlargest := a1
largest := a2
if n = 2 then
return secondlargest
for i := 3 to n
if ai > largest then
secondlargest := largest
largest := ai
if (ai > secondlargest and ai ≤ largest) then
secondlargest := ai
return secondlargest
3. procedure lasteven(a1 , . . . , an : integers)
location := 0
for i := 1 to n
if 2 | ai then location := i
return location
4. procedure avgmaxmin(a1 , . . . , an : integers)
max := a1
min := a1
for i := 2 to n
if ai > max then max := ai
if ai < min then min := ai
return (max + min)/2
5. procedure secondmax (a1 , a2 , . . . , an : integers)
for i := 2 to n
if a1 < ai then exchange a1 and ai
secondmax := a2
for j := 3 to n
if secondmax < aj then secondmax := aj
return secondmax {secondmax is the second largest element}
496 Test Bank Questions and Answers

6. procedure largestproduct(a1 , a2 , . . . , an , N : realnumbers)


largestproduct := −∞
for i := 2 to n
for j := 1 to i − 1
if ai · aj < N then
if ai · aj > largestproduct then largestproduct := ai · aj
return largestproduct {largestproduct is the largest product of two numbers in the list that
is less than N , or − ∞ if all products are greater than or equal to N }
7. To search for x in an ordered list a1 , . . . , an , find the “midpoint” of the list and choose the appropriate half
of the list. Continue until the list consists of one element. Either this element is x , or else x is not in the
list.
8. The consecutive choices of sublists of the original list are: 15, 21, 25, 31 ; 25, 31 ; and 25 . Since 27 6= 25 ,
the integer 25 is not in the list.

You might also like