Homework #4 - Homework #4

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Homework #4 Last Updated: 09/28/2009 Total points 52 Extra credit 6 Section 3.1 -- pp. 177 - 178 6 (4 pts) Describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. procedure negatives( a 1 , a 2 , a 3 , . .., a n : integers) count := 0 for i:=1 to n if a i < 0 then count:=count+1 8 (4 pts) Describe an algorithm that takes as input a list of n distinct integers and finds the location of the largest even integer in the list or returns 0 if there are no even integers in the list. Assumes and short-circuits evaluation when first operand is false, or short-circuits evaluation when first operand is true. procedure max( a 1 , a 2 , a 3 , . .., a n : integers) location := 0 for i:=1 to n if a i mod 2 = 0 and (location = 0 or a location < a i ) then location := i 12 (4 pts) Describe an algorithm that uses only assignment statements that replaces the triple (x,y,z) with (y,z,x). What is the minimum number of assignment statements needed? procedure triple( x, y, z ) t:=x Homework #4 http://homepages.ius.edu/rwisman/C251/html/homework/hw4Solutions.htm 1 of 10 10/13/2009 3:20 AM
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
x := y y := z z := t 4 is minimum. 14 (6 pts) Extra credit List all the steps used to search for 7 in the sequence {1,3,4,5,6,8,9,11} procedure linear search (x: integer , a 1 , a 2 , . .., a n : distinct integers) i := 1 while (i £ n and x ¹ a i ) i := i + 1 if i £ n then location := i else location := 0 1 2 3 4 5 6 7 8 1 3 4 5 6 8 9 11 Linear search: i=1, 2, 3, 4, 5, 6, 7, 8, 9; location:=0 List all the steps used to search for 7 in the sequence {1,3,4,5,6,8,9,11} procedure binary search (x: integer , a 1 < a 2 < . .. < a n : distinct integers) i := 1 j := n while i < j m := ë (i + j) / 2 û if x > a m then i := m + 1 else j := m Homework #4 http://homepages.ius.edu/rwisman/C251/html/homework/hw4Solutions.htm 2 of 10 10/13/2009 3:20 AM
Background image of page 2
if x = a
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/31/2009 for the course COMPUTER computer 1 taught by Professor Abedauthman during the Spring '08 term at Aarhus Universitet.

Page1 / 10

Homework #4 - Homework #4

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online