Comp 14: Introduction To Programming
Comp 14: Introduction To Programming
Introduction to Programming
Miguel A. Otaduy
– Any problems?
• Binary numbers
• What is an algorithm?
Reading Check-Up
1. hardware - computer components including
the CPU, main memory, I/O devices, and
secondary storage
2. CPU - the brain of the computer,
containing the CU, PC, IR, ALU, and ACC
3. program - computer instructions to solve a
problem
4. The digits 0 and 1 are called binary digits
or the shortened term bits
Hardware vs. Software
A computer is made up of hardware and software
Hardware Software
• CPU • operating systems
- ex: 1 GHz Pentium III - Windows XP
• input/output - Mac OS X
- keyboard
- monitor • applications
- network card - games
• main memory - Microsoft Word
- ex: 256 MB RAM - Internet Explorer
• secondary memory
- ex: 20 GB hard drive
Hardware Organization
CPU
memory
motherboard
hard drive
Central Processing Unit
CPU
• Control Unit (CU)
– "the brain" of the CPU
• Program Counter (PC)
– points to the next instruction to be executed
• Instruction Register (IR)
– holds the currently executing instruction
• Arithmetic Logic Unit (ALU)
– carries out all arithmetic and logical ops
• Accumulator (ACC)
– holds the results of the operations performed by the ALU
Main Memory
• Ordered sequence of cells
Secondary memory
devices provide
long-term storage
Information is moved
between main memory
and secondary memory
as needed
Input Devices
• Definition: devices that feed data and
computer programs into computers
• Examples:
– Keyboard
– Mouse
– Secondary Storage
Output Devices
• Definition: devices that the computer uses
to display results
• Examples:
– Printer
– Monitor
– Secondary Storage
Input/Output Devices
Hi, Heather.
• A sequence of 0s and 1s
– binary digit, or bit
– sequence of 8 bits is called a byte
1
1 bit ? 2 = 2 items
How many 2 bits ? 22 = 4 items
items can be 3
3 bits ? 2 = 8 items
represented by
4 bits ? 24 = 16 items
5 bits ? 25 = 32 items
Binary Numbers
• N bits to represent 2N values
• N bits represent values 0 to 2N-1
• Example: 5 bits
– 32 unique values (0-31)
– 00000 = 0
– 11111 = 31
24 23 22 21 20
16 + 8 + 4 + 2 + 1
Decimal to Binary
114 1110010
Place Digit Value
26 1 64
25 1 32
24 1 16
23 0 0
22 0 0
21 1 2
20 0 0
Questions
Binary Numbers
• What’s the maximum value a 6-bit number
can represent? 63
with
computer
Algorithm Design Example
Problem: Convert change in cents to number of half-dollars,
quarters, dimes, nickels, and pennies to be returned.
Example:
– given 646 cents
– number of half-dollars: divide 646 by 50
• quotient is 12 (number of half-dollars)
• remainder is 46 (change left over)
– number of quarters: divide 46 by 25
• quotient is 1 (number of quarters)
• remainder is 21 (change left over)
– number of dimes, nickels, pennies
– result: 12 half-dollars, 1 quarter, 2 dimes, 0 nickels, 1
penny
Resulting Algorithm
1. Get the change in cents
2. Find the number of half-dollars
3. Calculate the remaining change
4. Find the number of quarters
5. Calculate the remaining change
6. Find the number of dimes
7. Calculate the remaining change
8. Find the number of nickels
9. Calculate the remaining change
10. The remaining change is the number of pennies.
Exercise
• Execution of c=2*a+b in a computer
To do
• Read ch. 2
• Exercises 10 & 11 in ch. 1 (algorithm
design)
What’s Ahead...
• Java Basics