Download presentation
Presentation is loading. Please wait.
1
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables
2
Exercise Write an algorithm that asks the user for 10 temperature measurings, and prints out the temperatures entered and their average. For example: Enter 10 temperatures: 21, 32, 34, 56, 67, 89, 21, 45, 67, 54 The recorded temperatures are: 21, 32, 34, 56, 67, 89, 21, 45, 67, 54. The average is: …
3
Exercise Change your previous program so that it handles –20 temperatures – 50 temperatures –100 temperatures –1000 temperatures
4
List variables How to represent inputs of arbitrary size? Suppose that we need to read 100 numbers from the user, or 1000, or.. – we could give each variable a different name…tedious!! Use a list variable: –Variable: list a of size n –This means that a is a list of n elements: a 1, a 2, a 3,…,a n –To read the list from the user use a loop to read each element –To print the list use use a loop to print each element –We can treat each element in the list as a variable Set a 3 to 5 Set a 4 to a 3 +2 If (a 4 == a 3 ) then print “equal”
5
List variables We used to write –Variable: a, b, c, sum, avg, i etc If we want to use a list we’ll write –Variable: list a of size 100 –This tells the “computer” that a is a list variable which can hold 100 items –Now we can access any item in the list as a normal variable, by specifying its index Set a 2 to a 9 Set i = 10 Set a i to a 3 +2
6
List examples Reading a list of 100 elements from the user Variables: i, n, list a of size 100 Print “Enter 100 elements: ” n = 100 i = 1 while (i <= n) print “enter next element” get a i i = i+1 Print “Great, thanks.”
7
List examples Printing a list to the user Variables: i, n, list a of size 100 n = 100 i = 1 Print “The list is: “ while (i <= n) print a i i = i+1 Print “Done”
8
List examples What does the following code do? n = 10 i = 1 while (i <= n) a i = i i = i+1 What does the following code do? x = 0 i =1 while (i<= n) x = x + a i i = i+1 print x
9
Example: putting it together.. What does the following do? variables: I,n,x, list a of size 10 n = 10 print “Enter “ n “ numbers:” i = 1 while (i <= n) get a i i = i+1 x = 0 i =1 while (i<= n) x = x + a i i = i+1 print x
10
Searching Problem: Write an algorithm that reads from the user a list of 100 numbers and a target value, and searches to see if any element in the list is equal to the target value. If not, prints “target not found”. If yes, prints “target found”.
11
Searching, variations Modify your search algorithm so that: –It prints the location (i.e. index in the list) where it finds the target –It finds only the first occurence of target –It finds all occurences of target (and prints their locations) –It counts the number of occurences of target in the list –It counts how many elements in the list are larger than target
12
More exercises Write an algorithm that reads a list of 100 numbers from the user and –prints out the average of all numbers in the list. –prints out the largest element in the list –prints out the smallest element in the list
13
Iterating through a list In general, an algorithm that explores every single element in the list in order will look something like this: Set i = 1 while (i <= 100) Set i = i+1 jargon: this is called iterating through the list
14
Searching The following algorithm finds the first occurrence of target and prints out its position. Variables: i, target, list a of 100 elements i=1 while (i <= 100) –Print “enter number: “ i “:” –Get a i –i = i+1 Get target Set found = 0, i = 1 While ((i <= 100) and (found == 0)) –If a i == target set found = 1 –i = i+1 If (found ==1) print “Target found at position” i-1 Else print “ Target not found.”
15
Searching Assume a list of 100 elements –What is the fastest that our search algorithm can finish? This is called best-case time of the algorithm –What is the best-case input like? –What is the slowest that our search algorithm can finish? This is called worst-case time of the algorithm –What is the worst-case input like? Can we do better? –That is, faster? –In other words, can we find an algorithm that can decide whether the target is in the list without checking every single element?
16
Phone book search How do we search in a phone book? Check the middle value If smaller than target, go right Otherwise go left Problem: find a target in a sorted list –How can we exploit that the list is sorted? –Can we come up with an algorithm?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.