Computer Science 101 A Survey of Computer Science - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

Computer Science 101 A Survey of Computer Science

Description:

Informal language used to present algorithms. Compromise between verbose natural ... Has statements like most programming languages, but not quite as picky. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 65
Provided by: tomwh
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 101 A Survey of Computer Science


1
Computer Science 101A Survey of Computer Science
  • Pseudocode,Searching

2
Pseudocode
  • 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.

3
Three 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.

4
Language terminology
  • Syntax The formal rules for legal statements in
    the language.
  • Semantics The meaning of the statements - what
    happens when the statement is executed.

5
Sequential Operations
Step i
  • Computation
  • Input
  • Output

Step i1
Step i2
6
Variables 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.

7
Computation 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

8
Input/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

9
Conditional 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

10
Conditional 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

11
Iteration Do-while
  • Do Step I Step J
    while condStyleIndent the list of statements

12
Iteration While
  • SyntaxWhile cond do Step
    end-of-loop
  • StyleIndent the list of statements

13
Pseudocode Summary
  • Sequential
  • Set .
  • Get ...
  • Print ...
  • Conditional
  • If-then
  • If-then-else
  • Iteration
  • Do-while
  • While-do

14
Searching
  • 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

15
Or do we?
16
Or do we?
17
Or do we?
18
Or do we?
19
Sequential 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

20
Sequential Search-Names/Phone Nos
  • Given Names N1,N2,Nn Target NAME Phone
    Nos T1,T2,,Tn
  • Output Phone number of NAME

21
Names/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

22
Algorithm 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

23
Algorithm 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

24
Example
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
25
Example
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
26
Example
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
27
Example
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
28
Example
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
29
Example
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
30
Example
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
31
Example
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
32
Example
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
33
Example
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
34
Example
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
35
Example
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
36
Example
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
37
Example
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
38
Example
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
39
Example
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
40
Example
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
41
Example
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
42
Example
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
43
Example
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
44
Example
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
45
Example
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
46
Example
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
47
Example
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
48
Example
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
49
Example
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
50
Example
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
51
Example
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
52
Example
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
53
Example
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
54
Example
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
55
Example
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
56
Example
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
57
Example
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
58
Example
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
59
Example
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
60
Example
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
61
Example
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
62
News alert
63
News Alert!
64
It's a dirty job, but someone's gotta do it!
Write a Comment
User Comments (0)
About PowerShow.com