Title: CPS120:%20Introduction%20to%20Computer%20Science
1CPS120 Introduction to Computer Science
2Decimal Equivalents
- Assuming the bits are unsigned, the decimal value
represented by the bits of a byte can be
calculated as follows - Number the bits beginning on the right using
superscripts beginning with 0 and increasing as
you move left - Note 20, by definition is 1
- Use each superscript as an exponent of a power of
2 - Multiply the value of each bit by its
corresponding power of 2 - Add the products obtained
3Converting 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
4Converting 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
5Binary 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
6Converting 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
7Converting 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
8Converting Decimal to Hexadecimal
222 13 0
16 3567 16 222 16 13 32
16 0
36 62 13 32 48
47 14 32
15
D E F
21
9CPS120 Introduction to Computer Science
10Sample Program Flowchart
Start
Terminal Symbol
Initialize Variables
Preparation Symbol
or
Variables
Open Files
I/O Symbol
Read a Record
No
More items?
Process Record (Total Time)
Close Files
Stop
Decision Symbol
Yes
Process Record (Detail Time)
Process Symbol
Write Record
11Common Flowchart Symbols
12Rules 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
13Programs
- A program is a set of step-by-step instructions
that directs the computer to do the tasks you
want it to do and produce the results you want.
14What 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
15Sequence Control Structures
- Sequence control structures direct the order of
program instructions. - The fact that one instruction follows anotherin
sequenceestablishes the control and order of
operations.
16Calculate
- A program can instruct a computer to perform
mathematical operations.
Add 1 to Counter
17Store
- A program will often instruct a computer to store
intermediate results.
Place 1 in Counter
18Compare 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
19IF-THEN
Test condition p
20IF-THEN-ELSE
21Iterate
- A program loop is a form of iteration. A computer
can be instructed to repeat instructions under
certain conditions.
No
22Iteration 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
23Leading 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.
24DO WHILE Loop
25Trailing 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.
26DO UNTIL Loop
Loop statement a
27Pseudocode
- Pseudocode is an artificial and informal language
that helps programmers develop algorithms.
28Pseudocode
- This device is not visual but is considered a
first draft of the actual program. - Pseudocode is written in the programmers native
language and concentrates on the logic in a
programnot the syntax of a programming language.
29Writing Pseudocode
- You need to reach a balance between excessive and
insufficient detail.
30Rules for Pseudocode
- Make the pseudocode language-independent
- Indent lines for readability
- Make key words stick out by showing them
capitalized, in a different color or a different
font - Punctuation is optional
- End every IF with ENDIF
- Begin loop with LOOP and end with ENDLOOP
- Show MAINLINE first all others follow
- TERMINATE all routines with an END instruction
31A Computer Example
- Problem
- Create an address list that includes each
persons 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
32A Computer Example
33A Computer Example
34A Computer Example
35A Computer Example
36A Computer Example
37CPS120 Introduction to Computer Science
38Decision 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)
39I 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.
40Computers 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
41Computers 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
42Computers and Electricity
- Boolean algebra expressions in this algebraic
notation are an elegant and powerful way to
demonstrate the activity of electrical circuits
43Computers 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
44Truth 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.
1
0
1
1
1
1
1
1
1
45Gates
- Lets 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
46NOT
- 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
47NOT Gate
- A NOT gate accepts one input value and produces
one output value
Various representations of a NOT gate
48NOT 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
49AND
- 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
50AND 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
51OR
- 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
52OR 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
53XOR (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
54XOR 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
55XOR Gate
Various representations of an XOR gate
56NAND (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
57NAND 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
58Using 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