Title: Computer Science 101 A Survey of Computer Science
1Computer Science 101A Survey of Computer Science
2Pseudocode
- Informal language used to present algorithms.
- Compromise between verbose natural language and
precise programming language. - Has statements like most programming languages,
but not quite as picky.
3Three major control constructs of programming
- Sequential Simply do steps one after the other
in order they are listed. - Conditional Decide which statement to do next
based on some true/false test. - Iterative A set of statements is repeated over
and over until some condition is met.
4Language terminology
- Syntax The formal rules for legal statements in
the language. - Semantics The meaning of the statements - what
happens when the statement is executed.
5Sequential Operations
Step i
Step i1
Step i2
6Variables and expressions
- Variable A named storage location whose stored
value can be changed as the algorithm is
executed. - Arithmetic expression A legal expression
constructed of constants, variables, parentheses,
and arithmetic operations. Its value can be
computed using current values of the variables.
7Computation operation
- Syntax Set variable to expression
- Semantics
- Compute value of expression
- Store this as new value of the variable
- Example Set Pay to PayRate x Hours
8Input/Output
- Input
- Syntax Get variable, variable,
- Semantics User enters value(s) for variable(s)
- Output
- Syntax Print variable, variable,
- Semantics Value(s) of variable(s) displayed
9Conditional Operation if-then
- Syntax if condition then list of
statements - Exampleif Age 65 then Set Price to .85 x
Price Set SrCnt to SrCnt1 - StyleIndent the list of statements
10Conditional Operation if-then-else
- Syntax if condition then list1 of
operations else list2 of operations - Exampleif Hourly1 then Set Pay to Rate x
Hourselse Set Pay to Salary/12 - StyleIndent the lists of statements
11Iteration Do-while
- Do Step I Step J
while condStyleIndent the list of statements
12Iteration While
- SyntaxWhile cond do Step
end-of-loop - StyleIndent the list of statements
13Pseudocode Summary
- Sequential
- Set .
- Get ...
- Print ...
- Conditional
- If-then
- If-then-else
- Iteration
- Do-while
- While-do
14Searching
- Searching is one of the most common operations of
computing - Search for student record based on id
- Search for best item based on criteria
- Search for airline reservations
- Clearly we know all there is to know about
searching
15Or do we?
16Or do we?
17Or do we?
18Or do we?
19Sequential Search Algorithm
- Given List of values v1,v2,,vn
Target value, T - Output Information about T if in list
and message if T is not in list - Strategy
- Use variables
- i for current location in list
- Found to indicate whether T located or not
- Initialize i1, Foundfalse
- Pass through the list sequentially
20Sequential Search-Names/Phone Nos
- Given Names N1,N2,Nn Target NAME Phone
Nos T1,T2,,Tn - Output Phone number of NAME
21Names/Phone Nos Algorithm
- Set Found to false
- Set i to 1
- While not found and i last position Do
- if Ni NAME then
- Print Ti
- Set Found to true
- else
- Set i to i1 end-of-loop
- if Foundfalse then
- Print Sorry, not in directory
- Stop
22Algorithm to Find Largest
- Given List of numbers A1,A2,,An
- Output Largest value in list and its position
- Strategy
- Use variables
- i for current location in list
- Largest for largest value found so far
- LargeLoc for position of largest found so far
- Initialize LargestA1,LargeLoc1,i2
- Pass through the list sequentially
23Algorithm to Find Largest (cont)
- Set Largest to A1
- Set LargeLoc to 1
- Set i to 2
- While i ? n do
- if Ai gt Largest then
- Set Largest to Ai
- Set LargeLoc to i
- Set i to i1
- end-of-loop
- Print Largest, LargeLoc
- Stop
24Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
8
25Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 8
26Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 8
27Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 2 8
28Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 2 8
29Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 2 8
30Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 3 8
31Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 3 8
32Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 3 8
33Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
9 1 3 8
34Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 1 3 8
35Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 3 8
36Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 4 8
37Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 4 8
38Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 4 8
39Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 4 8
40Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 5 8
41Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 5 8
42Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 5 8
43Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 5 8
44Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 6 8
45Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 6 8
46Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 6 8
47Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
12 3 6 8
48Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 3 6 8
49Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 6 8
50Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 7 8
51Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 7 8
52Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 7 8
53Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 7 8
54Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 8 8
55Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 8 8
56Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 8 8
57Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 8 8
58Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 9 8
59Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 9 8
60Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 9 8
61Example
1 2 3 4 5 6 7 8 9 5 12 6 3 15
10 14
- 1. Set Largest to A1
- 2. Set LargeLoc to 1
- 3. Set i to 2
- 4. While i ? n do
- 5. if Ai gt Largest then
- 6. Set Largest to Ai
- 7. Set LargeLoc to i
- 8. Set i to i1
- 9. end-of-loop
- 10. Print Largest, LargeLoc
- 11. Stop
Largest LargeLoc i n
15 6 9 8
OUTPUT 15, 6
62News alert
63News Alert!
64It's a dirty job, but someone's gotta do it!