CPS120: Introduction to Computer Science

Download Report

Transcript CPS120: Introduction to Computer Science

CPS120: Introduction to
Computer Science
Session 5
Decimal Equivalents
•
Assuming the bits are unsigned, the decimal
value represented by the bits of a byte can be
calculated as follows:
1. Number the bits beginning on the right using
superscripts beginning with 0 and increasing as you
move left
•
Note: 20, by definition is 1
2. Use each superscript as an exponent of a power of 2
3. Multiply the value of each bit by its corresponding
power of 2
4. Add the products obtained
Converting Binary to Octal
• Groups of Three (from right)
• Convert each group
11010010
110 100 010
6
4 2
110100010 is 642 in base 8
17
Converting Octal to Decimal
What is the decimal equivalent of the octal
number 642?
6 x 8² = 6 x 64 = 384
+ 4 x 8¹ = 4 x 8 = 32
+ 2 x 8º = 2 x 1 = 2
= 418 in base 10
11
Binary to Hex
• Step 1: Form four-bit groups beginning from the
rightmost bit of the binary number
– If the last group (at the leftmost position) has less than
four bits, add extra zeros to the left of the group to
make it a four-bit group
• 110111101111 becomes
• 1101 1110 1111
• Step 2: Replace each four-bit group by its
hexadecimal equivalent
– DEF(16
Converting Hexadecimal to
Decimal
What is the decimal equivalent of the
hexadecimal number DEF?
D x 16² = 13 x 256 = 3328
+ E x 16¹ = 14 x 16 = 224
+ F x 16º = 15 x 1 = 15
= 3567 in base 10
Remember, base 16 is
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Converting Decimal to Other Bases
• Step 1: Divide the number by the base you are
converting to (r)
• Step 2: Successively divide the quotients by (r)
until a zero quotient is obtained
• Step 3: The decimal equivalent is obtained by
writing the remainders of the successive division
in the opposite order in which they were obtained
– Know as modulus arithmetic
• Step 4: Verify the result by multiplying it out
Converting Decimal to
Hexadecimal
222
16 3567
32
36
32
47
32
15
13
16 222
16
62
48
14
0
16 13
0
13
D E F
21
CPS120 Introduction to
Computer Science
Pseudocode
Sample Program Flowchart
Terminal Symbol
Preparation Symbol
Start
or
I/O Symbol
Initialize Variables
Variables =
Open
Files
Read a
Record
No
Decision Symbol
More
items?
Yes
Process Symbol
Process
Record
(Detail Time)
Write
Record
Process
Record
(Total Time)
Close
Files
Stop
Common Flowchart Symbols
Common Flowchart Symbols
Terminator. Shows the starting and ending points of the program. A terminator has
flow lines in only one direction, either in (a stop node) or out (a start node).
Data Input or Output. Allows the user to input data and results to be displayed.
Processing. Indicates an operation performed by the computer, such as a variable
assignment or mathematical operation. With a heading – an internal subroutine
Decision. The diamond indicates a decision structure. A diamond always has two
flow lines out. One flow lineout is labeled the “yes” branch and the other is labeled the
“no” branch.
Predefined Process. One statement denotes a group of previously defined statements.
Such as a function or a subroutine created externally
Connector. Connectors avoid crossing flow lines, making the flowchart easier to read.
Connectors indicate where flow lines are connected. Connectors come in pairs, one with
a flow line in and the other with a flow line out.
Off-page connector. Even fairly small programs can have flowcharts that extend several
pages. The off-page connector indicates the continuation of the flowchart on another
page. Just like connectors, off-page connectors come in pairs.
Flow line. Flow lines connect the flowchart symbols and show the sequence of operations
during the program execution.
Rules for Drawing Flowcharts
• Top to bottom and left to right
– Draw the flowchart the way you like to read
– Use arrowheads on flow lines whenever the
flow is not top to bottom, left to right
• Be neat ! Use graphics software
• Avoid intersecting lines
Programs
• A program is a set of stepby-step instructions that
directs the computer to do
the tasks you want it to do
and produce the results
you want.
What Can a Program Do?
• A program can only instruct a computer to:
–
–
–
–
–
–
–
Read Input
Sequence
Calculate
Store data
Compare and branch
Iterate or Loop
Write Output
Sequence Control Structures
• Sequence control structures direct the order
of program instructions.
• The fact that one instruction follows
another—in sequence—establishes the
control and order of operations.
Calculate
• A program can
instruct a computer
to perform
mathematical
operations.
Add 1 to
Counter
Store
• A program will often
instruct a computer to
store intermediate
results.
Place 1
in
Counter
Compare and Branch
• A program can instruct a computer to compare
two items and do something based on a match
or mismatch which, in turn, redirect the
sequence of programming instructions.
– There are two forms:
– IF-THEN
– IF-THEN-ELSE
IF-THEN
Entry
Test
condition p
Exit
false
true
True
statement a
IF-THEN-ELSE
Entry
Test
condition p
false
“false”
statement a
true
Exit
“true”
statement a
Iterate
• A program loop is a
form of iteration. A
computer can be
instructed to repeat
instructions under
certain conditions.
No
Iteration Control Structures
• Iteration control structures are looping
mechanisms.
• Loops repeat an activity until stopped. The
location of the stopping mechanism
determines how the loop will work:
• Leading decisions
• Trailing decisions
Leading Decisions
• If the stop is at the beginning of the
iteration, then the control is called a leading
decision.
• The command DO WHILE performs the
iteration and places the stop at the
beginning.
DO WHILE Loop
Entry
Exit
No
Test
condition p
Yes
Loop
statement a
Trailing Decisions
• If the stop is at the end of the iteration, the
control mechanism is called a trailing
decision.
• The command DO UNTIL performs the
iteration and puts the stop at the end of the
loop.
DO UNTIL Loop
Entry
Loop
statement a
Test
condition p
Exit
No
Yes
Pseudocode
• Pseudocode is an artificial and informal
language that helps programmers develop
algorithms.
Pseudocode
• This device is not visual but is considered a
“first draft” of the actual program.
• Pseudocode is written in the programmer’s
native language and concentrates on the logic
in a program—not the syntax of a
programming language.
Writing Pseudocode
• You need to reach a balance between
excessive and insufficient detail.
Rules for Pseudocode
1. Make the pseudocode language-independent
2. Indent lines for readability
3. Make key words stick out by showing them
capitalized, in a different color or a different font
4. Punctuation is optional
5. End every IF with ENDIF
6. Begin loop with LOOP and end with ENDLOOP
7. Show MAINLINE first; all others follow
8. TERMINATE all routines with an END
instruction
A Computer Example
• Problem
– Create an address list that includes each
person’s name, address, telephone number, and
e-mail address
– This list should then be printed in alphabetical
order
– The names to be included in the list are on
scraps of paper and business cards
A Computer Example
A Computer Example
A Computer Example
A Computer Example
A Computer Example
CPS120 Introduction to
Computer Science
Boolean Logic
Decision Making In Computers
• A circuit quite simply allows one out of two
choices to be made depending on its inputs
• When decisions are made in a computer program,
they are simply the result of a computation in
which the final result is either TRUE or FALSE
• The value zero (0) is considered to be FALSE.
Any positive or negative value is considered to be
TRUE (usually represented by 1)
I Know This Concept
• If you have ever taken a True or False test,
you have used Boolean logic.
– In the Boolean system an object can exist in
only one of two states, there is no third choice
• This is a central concept in programming.
Computers and Electricity
• A gate is a device that performs a basic
operation on electrical signals
• Gates are combined into circuits to perform
more complicated tasks
Computers and Electricity
• There are three different, but equally
powerful, notational methods
for describing the behavior of gates
and circuits
– Boolean expressions
– logic diagrams
– truth tables
Computers and Electricity
• Boolean algebra: expressions in this
algebraic notation are an elegant and
powerful way to demonstrate the activity of
electrical circuits
Computers and Electricity
• Logic diagram: a graphical representation
of a circuit
– Each type of gate is represented by a specific
graphical symbol
• Truth table: defines the function of a gate
by listing all possible input combinations
that the gate could encounter, and the
corresponding output
Truth Tables
• Use this truth table to determine the results of the
logical operators. In this table, 1 represents TRUE
and 0 represents FALSE.
• Note that the ! symbol (the logical NOT operator)
changes a TRUE to a FALSE.
AND
A
0
0
1
1
B
0
1
0
1
A && B
0
0
0
1
OR
A
0
0
1
1
B
0
1
0
1
A || B
0
1
1
1
NOT
A
0
1
!A
1
0
Gates
• Let’s examine the processing of the following
six types of gates
–
–
–
–
–
–
NOT
AND
OR
XOR
NAND
NOR
• Typically, logic diagrams are black and white, and the
gates are distinguished only by their shape
NOT
• Reverses the input. If TRUE is input, the
result id FALSE; if FALSE is input, the
result is TRUE.
– 1 would evaluate to FALSE
– 0 would evaluate to TRUE
NOT Gate
• A NOT gate accepts one input value
and produces one output value
Various representations of a NOT gate
NOT Gate
• By definition, if the input value for a NOT
gate is 0, the output value is 1, and if the
input value is 1, the output is 0
• A NOT gate is sometimes referred to as an
inverter because it inverts the input value
AND
• Two or more items must agree(be evaluated to the
same result) for the expression to be true.
–
–
–
–
–
–
1 AND 1 would evaluate to TRUE
0 AND 1 would evaluate to FALSE
1 AND 0 would evaluate to FALSE
0 AND 0 would evaluate to FALSE
1 AND 1 AND 1 would evaluate to TRUE
1 AND 1 AND 0 would evaluate to FALSE
AND Gate
• An AND gate accepts two input signals
• If the two input values for an AND gate are both 1,
the output is 1; otherwise, the output is 0
Various representations of an AND gate
OR
• One, both or more items must agree. If both
inputs are FALSE, the result it FALSE.
– 1 OR 1 would evaluate to TRUE
– 0 OR 1 would evaluate to TRUE
– 0 OR 0 would evaluate to FALSE
OR Gate
• If the two input values are both 0, the output
value is 0; otherwise, the output is 1
Figure 4.3 Various representations of a OR gate
XOR (eXclusive OR)
• Only one input may be TRUE, if both are TRUE
the entire result id FALSE.
– 1 XOR 1 would evaluate to FALSE
– 1 XOR 0 would evaluate to TRUE
– 0 XOR 1 would evaluate to TRUE
– 0 XOR 0 would evaluate to FALSE
XOR Gate
• XOR, or exclusive OR, gate
– An XOR gate produces 0 if its two inputs are the
same, and a 1 otherwise
– Note the difference between the XOR gate
and the OR gate; they differ only in one
input situation
– When both input signals are 1, the OR gate
produces a 1 and the XOR produces a 0
XOR Gate
Various representations of an XOR gate
NAND (Not AND)
• This basically negates AND:
– 1 NAND 1 would evaluate to FALSE
– 1 NAND 0 would evaluate to TRUE
– 0 NAND 0 would evaluate to TRUE
– 0 NAND 1 would evaluate to TRUE
NAND and NOR Gates
• The NAND and NOR gates are essentially the
opposite of the AND and OR gates, respectively
Various representations of a NAND
gate
Various representations of a NOR
gate
Using Logical Operators
• When complex decisions must be coded into an
algorithm, it may be necessary to "chain together"
a few relational expressions (that use relational
operators)
• This is done with logical operators (also called
Boolean operators.)
&&
||
!
is the logical AND operator
is the logical OR operator
is the logical NOT operator