Title: Chapter 1: An Introduction to Computer Science
1Chapter 1 An Introduction to Computer Science
- Invitation to Computer Science,
- Java Version, Third Edition
2Objectives
- In this chapter, you will learn about
- The definition of computer science
- Algorithms
3Introduction
- Common misconceptions about computer science
- Computer science is the study of computers
- Computer science is the study of how to write
computer programs - Computer science is the study of the uses and
applications of computers and software
4The Definition of Computer Science
- Gibbs and Tucker definition of computer science
- Computer science is the study of algorithms that
is used to solve problems. - The problem solving process involves 4 steps
- Creating correct and efficient algorithms
- Building computers to execute algorithms
- Designing programming languages to translate
algorithms - Applications
5Quiz
- 1. (T/F) Computer science is the study of
allergy. - 2. (T/F) Building cars is one of problems solving
steps used by computer scientists. - 3. (T/F) Designing programming languages is the
third step of problems solving steps used by
computer scientists.
6The Definition of Computer Science (continued)
- Algorithm
- An ordered step by step instructions that is
guaranteed to solve a specific problem
7The Definition of Computer Science (continued)
- An algorithm is a list that looks like
- STEP 1 Do something.
- STEP 2 Do something.
- STEP 3 Do something.
- . .
- . .
- . .
- STEP N Stop. You are finished.
8Example of Algorithm How to make sticky rice?
- 1. Measure Your Sticky Rice.
- 2. Add Water and Let Soak.
- 3. Add Salt and Bring to a Boil.
- 4. Cook the Rice.
- 5. Use a Fork to Check the Rice.
- 6. Allow Rice to Steam-Cook for 20 min..
- 7. Enjoy Your Sticky Rice!
9Criteria for Correct Algorithm
- Criteria
- 1. unambiguous instructions
- 2. computable instructions
- 3. producing results
- 4. stopping in a finite amount of time
- Example
- 1. Add X to some numbers -gtviolate criteria
1 - 2. Set X to (5 / 0) -gtviolate
criteria 2 and 3 - 3. Repeat Step 4 Until X gt 10 -gtviolate criteria
4 - 4. Set X 1
- 5. Stop
10Algorithm Operation 1 Sequential Operation
- Sequential operations
- Carry out a single well-defined task when that
task is finished, the algorithm moves on to the
next operation - Examples
- 1. Set X to 1
- 2. Set Y to 2
- 3. Set Z to X Y
- 4. Print Z
- 5. Stop
11Quiz
- What is the output of Step 4 of the following
algorithm? - 1. Set Y to 4
- 2. Set W to 2
- 3. Set P to (Y W) / 2
- 4. Print P
- 5. Stop
12Quiz
- What is the output of Step 4 of the following
algorithm? - 1. Set Y to 4
- 2. Set W to Y 2
- 3. Set Y to (Y W) / 2
- 4. Print Y
- 5. Stop
13Algorithm Operation 1 Conditional Operation
- Conditional operations
- Ask a question and then select the next operation
to be executed on the basis of the answer to that
question - Examples
- 1. Set X to 1
- 2. If X is even
- then Set Y to 1
- else Set Y to 2
- 3. Stop
14Quiz
- What is the output of Step 3 of the following
algorithm? - 1. Set X to 4
- 2. If X gt 3
- then Set Y to A
- else Set Y to B
- 3. Print Y
- 4. Stop
15The Definition of Computer Science (continued)
- Iterative operations
- Go back and repeat the execution of a previous
block of instructions - Examples
- 1. Set X to 1
- 2. Repeat Step3 Until X is 3
- 3. Set X to X 1
- 4. Stop
16Quiz
- What is the output of Step 3 of the following
algorithm? - 1. Set X to 1
- 2. Set Y to 2
- 3. Repeat Step4-5 Until X is 3
- 4. Set X to X 1
- 5. Set Y to Y 2
- 6. Stop
17- What is the output of Step 6 of the following
algorithm?_____ - How many times have step 4 been executed? ____
- 1. Set X to 1
- 2. Set Y to 2
- 3. Repeat Step4-5 Until X is Y gt 7
- 4. Set X to X 3
- 5. Set Y to Y 3
- 6. Print Y
- 7. Stop
18Quiz
- What is the output of Step 6 of the following
algorithm?_____ - How many times have step 4 been executed? ____
- 1. Set X to 1
- 2. Set Y to 2
- 3. Repeat Step4-5 Until X is Y gt 7
- 4. Set X to X 3
- 5. Set Y to Y 3
- 6. Print X
- 7. Stop
19What is boolean?
- a boolean is either TRUE or FALSE
- What is the output of the following algorithm?___
- 1. Set B to FALSE
- 2. If B is TRUE then
- Set A to OK
- else
- Set A to YES
- 3. Print A
- 4. Stop
20Quiz
- What is the output of step 4 of the following
algorithm?___ - Has the statement Set A to TRUE been executed?
_____ - 1. Set B to TRUE
- 2. If B is TRUE then
- Set A to FALSE
- Else
- Set A to TRUE
- 3. If A is FALSE then
- Set W to X
- Else
- Set W to Y
- 4. Print W
- 5. Stop
21What is the output on step 6? ________ How many
times of step 4 been executed? _______ 1. Set
INDEX to 0 2. Set DONE to false 3. Repeat steps
4-5 until DONE is true 4. Set INDEX to INDEX
1 5. If INDEX gt 2 then Set DONE to
true 6. Print INDEX 7. Stop
22Quiz
- What is the output on step 6? ________
- How many times of step 4 been executed? _______
- 1. Set INDEX to 1
- 2. Set DONE to false
- 3. Repeat steps 4-5 until DONE is true
- 4. Set INDEX to INDEX 2
- 5. If INDEX gt 5 then
- Set DONE to true
- 6. Print INDEX
- 7. Stop
23Repeat/Until
- Repeat A Until B
- meaning if B is not true then do A.
24While /Do
- While A Do B
- meaning if A is true then do B
25- What is the output of step 6?_____
- How many times have step5 been executed? ____
- 1. Set INDEX to 1
- 2. Set SUM to 1
- 3. While INDEX lt 5 do steps 4 - 5
- 4. Set SUM to SUM INDEX
- 5. Set INDEX to INDEX 2
- 6. Print SUM
- 7. Stop
26Quiz
- What is the output of step 6?_____
- How many times have step5 been executed? ____
- 1. Set INDEX to 2
- 2. Set SUM to 3
- 3. While INDEX lt 8 do steps 4 - 5
- 4. Set SUM to SUM INDEX
- 5. Set INDEX to INDEX 3
- 6. Print SUM
- 7. Stop
27- What is the output of step 8?_____
- How many times have step5 been executed? ____
- 1. Set INDEX to 1
- 2. Set SUM to 1
- 3. Set DONE to false
- 4. While DONE is false do steps 5-7
- 5. Set SUM to SUM INDEX
- 6. Set INDEX to INDEX 3
- 7. If INDEX gt 7 THEN
- Set DONE to true
- 8. Print SUM
- 9. Stop
28Quiz
- What is the output of step 8?_____
- How many times have step5 been executed? ____
- 1. Set INDEX to 1
- 2. Set SUM to 1
- 3. Set DONE to false
- 4. While DONE is false do steps 5-7
- 5. Set SUM to SUM INDEX
- 6. Set INDEX to INDEX 3
- 7. If INDEX gt 7 THEN
- Set DONE to true
- 8. Print SUM
- 9. Stop
29Quiz - Challenge
- What is the output on step 6? ________
- How many times of step 4 been executed? _______
- 1. Set INDEX to 0
- 2. Set DONE to true
- 3. Repeat steps 4-5 until DONE is true
- 4. Set INDEX to INDEX 1
- 5. If INDEX gt 2 then
- Set DONE to true
- 6. Print INDEX
- 7. Stop
30Quiz
- What is the output of step 8?_____
- How many times have step5 been executed? ____
- 1. Set INDEX to 1
- 2. Set SUM to 1
- 3. Set DONE to true
- 4. While DONE is false do steps 5-7
- 5. Set SUM to SUM INDEX
- 6. Set INDEX to INDEX 3
- 7. If INDEX gt 7 THEN
- Set DONE to true
- 8. Print SUM
- 9. Stop
31Chapter 1. Exercises Samples
- Exercise 5.
- Under what conditions would the following
function not be effectively computable? - F(x) sqrt(a)/b
32Chapter 1. Exercises Samples
- Exercise 7. What is the output of the following
algorithm if X is 5 and Y is 2? - 1. Get X
- 2. Get Y
- 3. Divide X by Y and call the reminder R
- 4. If R is not 0 then reset X to X-1 and go back
to step 3. - 5. Print X
- 6. Stop
33Exercise 9 Samples
- Write an algorithm to compute the area of a
rectangle. - Area Length Width
- Get Length
- Get Width
- Set Area Length Width
- Print Area
- Stop
34Quiz
- Write an algorithm to compute the area of a
triangle. - Rule Area Height Width / 2
35Exercise 9 Sample.
- Write an algorithm to determine the financial
status. - Rule If the savings gt 10000 then output Good
otherwise Bad. - Get Savings
- If Savings gt 10000
- Set Status Good
- Else
- Set Status Bad
- Print Status
- Stop
36Quiz
- Write an algorithm to determine the weather
status. - Rule If the temperature lt 32F then output Cold
otherwise Warm.
37Quiz
- Write an algorithm to compute the following
- mysterious equation. Be sure to print the error
message when C is 0. - rule A B / C
38Example. If/else
- Write an algorithm to compute and display the
value sqrt(x) if the value of x is not negative.
If x is negative, then display the message
Unable to perform the sqrt() operation. - 1. Get X
- 2. If X lt 0
- Print Unable to perform the sqrt()
- Else
- Print sqrt(x)
- 3. Stop
39Quiz
- Write an algorithm to compute and display the
value a/b if the value of b is not 0. If b is 0,
then display the message Unable to perform the
division.
40Loop Exercise 1. The worst solution.
- Write an algorithm to compute
- Sum 234?
- 1. Set Sum to 0
- 2. Set Num to 2
- 3. Set Sum Sum Num
- 4. Set Num to Num 1
- 5. Set Sum Sum Num
- 6. Set Num to Num 1
- 7. Set Sum Sum Num
- 8. Print Sum
- 9. Stop
41Loop Exercise 2. The best solution
- Write an algorithm to compute
- Sum 234?
- 1. Set Sum to 0
- 2. Set Count to 1
- 3. Set Num to 2
- 4. While Count lt3 Do Steps 5-7
- 5. Sum Sum Num
- 6. Num Num 1
- 7. Count Count 1
- 8. Print Sum
- 9. Stop
42Loop Exercise 2. The best solution
- Write an algorithm to compute
- Sum 234?
1. Set Sum to 0 2. Set Count to 1 3. Set Num to
2 4. Repeat Sept 5-7 Until Count gt 3 Do 5.
Sum Sum Num 6. Num Num 1 7.
Count Count 1 8. Print Sum 9. Stop
43Quiz. Loop.
- Use While/Do to write an algorithm to compute
- sum 1 2 3 4 5 6 7 8 9 10
44Quiz. Loop.
- Use Repeat/Until to write an algorithm to compute
- sum 1 2 3 4 5 6 7 8 9 10
45- Use While/Do to write an algorithm to compute
- sum 2 4 6 8 10
46The Definition of Computer Science (continued)
- Iterative operations
- Tell us to go back and repeat the execution of a
previous block of instructions - Examples
- Repeat the previous two operations until the
mixture has thickened - While there are still more checks to be
processed, do the following five steps - Repeat steps 1, 2, and 3 until the value of y is
equal to 11
47The Definition of Computer Science (continued)
- If we can specify an algorithm to solve a
problem, we can automate its solution - Computing agent
- The machine, robot, person, or thing carrying out
the steps of the algorithm - Does not need to understand the concepts or ideas
underlying the solution
48The Formal Definition of an Algorithm
- Algorithm
- A well-ordered collection of unambiguous and
effectively computable operations that, when
executed, produces a result and halts in a finite
amount of time - Unambiguous operation
- An operation that can be understood and carried
out directly by the computing agent without
needing to be further simplified or explained - Ambiguous example, Set X to some number
49The Formal Definition of an Algorithm (continued)
- A primitive operation (or a primitive) of the
computing agent - Operation that is unambiguous for computing agent
- Primitive operations of different individuals (or
machines) vary - An algorithm must be composed entirely of
primitives - Effectively computable
- Computational process exists that allows
computing agent to complete that operation
successfully - Ineffectively computable example, Generate a
list of prime numbers
50The Formal Definition of an Algorithm (continued)
- The result of the algorithm must be produced
after the execution of a finite number of
operations - Infinite loop
- The algorithm has no provisions to terminate
- A common error in the designing of algorithms
- Infinite loop example,
- Step 1. Wet the car
- Step 2. Repeat Step 3
- Step 3. Wash the car
51The Importance of Algorithmic Problem Solving
- Algorithmic solutions can be
- Encoded into some appropriate language
- Given to a computing agent to execute
- The computing agent
- Would mechanically follow these instructions and
successfully complete the task specified - Would not have to understand
- Creative processes that went into discovery of
solution - Principles and concepts that underlie the problem
52The Early Period Up to 1940
- 3,000 years ago Mathematics, logic, and
numerical computation - Important contributions made by the Greeks,
Egyptians, Babylonians, Indians, Chinese, and
Persians - 1614 Logarithms
- Invented by John Napier to simplify difficult
mathematical computations - Around 1622 First slide rule created
53The Early Period Up to 1940 (continued)
- 1672 The Pascaline
- Designed and built by Blaise Pascal
- One of the first mechanical calculators
- Could do addition and subtraction
- 1674 Leibnitzs Wheel
- Constructed by Gottfried Leibnitz
- Mechanical calculator
- Could do addition, subtraction, multiplication,
and division
54- Figure 1.4
- The Pascaline One of the Earliest Mechanical
Calculators
55The Early Period Up to 1940 (continued)
- 1801 The Jacquard loom
- Developed by Joseph Jacquard
- Automated loom
- Used punched cards to create desired pattern
- 1823 The Difference Engine
- Developed by Charles Babbage
- Did addition, subtraction, multiplication, and
division to 6 significant digits - Solved polynomial equations and other complex
mathematical problems
56The Early Period Up to 1940 (continued)
- 1823 The Difference Engine
- Developed by Charles Babbage
- Capabilities
- Addition, subtraction, multiplication, and
division to 6 significant digits - Solve polynomial equations and other complex
mathematical problems
57- Figure 1.5
- Drawing of the Jacquard Loom
58The Early Period Up to 1940 (continued)
- 1830s The Analytic Engine
- Designed by Charles Babbage
- More powerful and general-purpose computational
machine - Components were functionally similar to the four
major components of todays computers - Mill (modern terminology arithmetic/logic unit)
- Store (modern terminology memory)
- Operator (modern terminology processor)
- Output (modern terminology input/output)
59The Early Period Up to 1940 (continued)
- 1890 U.S. census carried out with programmable
card processing machines - Built by Herman Hollerith
- These machines could automatically read, tally,
and sort data entered on punched cards
60The Birth of Computers 19401950
- Development of electronic, general-purpose
computers - Did not begin until after 1940
- Was fueled in large part by needs of World War II
- Early computers
- Mark I
- ENIAC
- ABC system
- Colossus
- Z1
61- Figure 1.6
- Photograph of the ENIAC Computer
62The Birth of Computers 19401950
- Stored program computer model
- Proposed by John Von Neumann in 1946
- Stored binary algorithm in the computers memory
along with the data - Is known as the Von Neumann architecture
- Modern computers remain, fundamentally, Von
Neumann machines - First stored program computers
- EDVAC
- EDSAC
63The Modern Era 1950 to the Present
- First generation of computing (1950-1959)
- Used vacuum tubes to store data and programs
- Each computer was multiple rooms in size
- Computers were not very reliable
64The Modern Era 1950 to the Present (continued)
- Second generation of computing (1959-1965)
- Replaced vacuum tubes by transistors and magnetic
cores - Dramatic reduction in size
- Computer could fit into a single room
- Increase in reliability of computers
- Reduced costs of computers
- High-level programming languages
- The programmer occupation was born
65The Modern Era 1950 to the Present (continued)
- Third generation of computing (1965-1975)
- Used integrated circuits rather than individual
electronic components - Further reduction in size and cost of computers
- Computers became desk-sized
- First minicomputer developed
- Software industry formed
66The Modern Era 1950 to the Present (continued)
- Fourth generation of computing (1975-1985)
- Reduced to the size of a typewriter
- First microcomputer developed
- Desktop and personal computers common
- Appearance of
- Computer networks
- Electronic mail
- User-friendly systems (Graphical user interfaces)
- Embedded systems
67- Figure 1.7
- The Altair 8800, the Worlds First Microcomputer
68The Modern Era 1950 to the Present (continued)
- Fifth generation of computing (1985-?)
- Recent developments
- Massively parallel processors
- Handheld devices and other types of personal
digital assistants (PDAs) - High-resolution graphics
- Powerful multimedia user interfaces incorporating
sound, voice recognition, touch, photography,
video, and television
69The Modern Era 1950 to the Present (continued)
- Recent developments (continued)
- Integrated global telecommunications
incorporating data, television, telephone, FAX,
the Internet, and the World Wide Web - Wireless data communications
- Massive storage devices
- Ubiquitous computing
70- Figure 1.8
- Some of the Major Advancements in Computing
71- Figure 1.8
- Some of the Major Advancements in Computing
72Organization of the Text
- This book is divided into six separate sections
called levels - Each level addresses one aspect of the definition
of computer science - Computer science/Algorithms
73Organization of the Text
- Level 1 The Algorithmic Foundations of Computer
Science - Chapters 1, 2, 3
- Level 2 The Hardware World
- Chapters 4, 5
- Level 3 The Virtual Machine
- Chapters 6, 7
74Organization of the Text
- Level 4 The Software World
- Chapters 8, 9, 10, 11
- Level 5 Applications
- Chapters 12, 13, 14
- Level 6 Social Issues
- Chapter 15
75- Figure 1.9
- Organization of the Text into a Six-Layer
Hierarchy
76Summary
- Computer science is the study of algorithms
- An algorithm is a well-ordered collection of
unambiguous and effectively computable operations
that, when executed, produces a result and halts
in a finite amount of time - If we can specify an algorithm to solve a
problem, then we can automate its solution - Computers developed from mechanical calculating
devices to modern electronic marvels of
miniaturization