Computer
Programming
Code: C Languange
Week 1
Introduction to Computer
What is a Computer?
Computer
Device capable of performing computations and making logical
decisions
Computers process data under the control of sets of instructions
called computer programs
Hardware
Various devices comprising a computer
Keyboard, screen, mouse, disks, memory, CD-ROM, and
processing units
Software
Programs that run on a computer
3
eureka!
Algorithm:
Algorithm A set of
instructions describing how to
do a task (or process).
Programming: C
Topics
Hardware components
Computer Networks
Transistor
Collector
Base
Emitter
semi-conductor
Binary digit or bit:
0 off
1 on
6
Transistor
Collector
Base
Emitter
off : 0
7
Transistor
Collector
Base
Emitter
on : 1
8
Transistor
Collector
Base
Emitter
Modern-day chips (about 3 x 3 mm in size) can contain
up to millions of transistors.
9
Gates
Gate: a group of transistors
Types:
AND Gate
OR Gate
NOT Gate
10
Example: AND Gate
A
B
A AND B
A AND B
0
0
1
0
1
0
0
0
0
1
A
B
A AND B
11
Gates and Boolean Algebra
AND Gate
0
0
1
1
0
1
0
1
OR Gate
A AND B A OR B
0
0
0
1
0
1
1
1
12
Gates and Boolean Algebra
NOT Gate
NOT A
0
1
1
0
13
Gates and Boolean Algebra
A sequence of bits at a time:
A =
B =
1 1 0 0 1 1 0 1
0 1 1 0 0 1 1 0
A AND B =
0 1 0 0 0 1 0 0
Most PCs do 32 bits at a time (32-bit machines),
others as many as 128 bits at a time
14
0 or 1
Gates are the basic building blocks of computers.
15
Hardware Components of a
Typical Computer
Input/Output
Devices
Central
Processing
Unit
Memory
"Buses" allow components to pass data to each other.
16
17
Computer Organization
Six logical units in every computer:
1. Input unit
Obtains information from input devices (keyboard, mouse)
2. Output unit
Outputs information (to screen, to printer, to control other devices)
3. Memory unit
Rapid access, low capacity, stores input information
4. Arithmetic and logic unit (ALU)
Performs arithmetic calculations and logic decisions
5. Central processing unit (CPU)
Supervises and coordinates the other sections of the computer
6. Secondary storage unit
Cheap, long-term, high-capacity storage
Stores inactive programs
18
Hardware Trends
Every year or two the following approximately
double:
Amount of memory in which to execute programs
Amount of secondary storage (such as disk storage)
Used to hold programs and data over the longer term
Processor speeds
The speeds at which computers execute their programs
19
Moores Law
90 nm
596 mm2
1.7 billions
Montecito
10 m
13.5mm2
2,25
0
42million
s
Exponential growth
Transistor count will be doubled every 18 months
Gordon Moore, Intel co-founder
20
Hardware Components of a
Typical Computer
Input/Output
Devices
Central
Processing
Unit
Memory
Central Processing Unit (CPU) - performs the
basic operations.
consists of two parts:
Arithmetic / Logic Unit (ALU) - data manipulation
Control Unit - coordinate machines activities
21
Hardware Components of a
Typical Computer
Input/Output
Devices
Central
Processing
Unit
Memory
Main Memory - holds programs and data
stores bits in fixed-sized chunks: word (8, 16, 32, or
64 bits)
each word is stored in a cell, which has a unique address
the cells can be accessed in any order (thus, randomaccess memory or RAM
22
"Bits, Bytes, Kilo-, Mega-, Giga-, "
A bit: 0 or 1
A word: chunk of bits (8, 16, 32 or 64 bits)
a byte = 8 bits
a kilobyte = 210 bytes = 1024 bytes
a megabyte = 1024 KB = 220 bytes
= 1,048,576 bytes
a gigabyte = 1024 MB = 230 bytes
a terabyte = 1024 GB = 240 bytes
23
Hardware Components of a
Typical Computer
Input/Output
Devices
Central
Processing
Unit
Memory
I/O Devices (Peripherals) - to communicate with
the outside world.
Examples:
Input: keyboard, mouse, microphone, scanner, sensors
(camera, infra-red), punch-cards, "mark sense" cards.
Output: video, printer, audio speakers, etc.
24
Other Input/Output Devices
The computer stores information for longer
periods of time as files in secondary (mass)
storage devices.
Examples: Hard drive, Floppy disk, Tape, Cassette,
CD-ROM (Compact Disk Read-Only Memory),
Flopticals, Cartridges, etc.
Communications device
Examples: Modems, Ethernet cards
25
Features of Computers
Speed
CPU speed
CPU clock speed: in cycles per second ("hertz")
Examples: 700MHz Pentium III, 500MHz G4
but different CPU designs do different amounts of
work in one clock cycle
flops (floating-point operations per second)
mips (million instructions per second)
26
Features of Computers
Speed (continued)
System clock/bus speed
communication between CPU, memory and I/O devices
depends on main board (a.k.a. "motherboard") design
Examples:
50MHz, 60MHz, 66MHz, 100Mhz, 133MHz, and rising
Intel 1.50GHz Pentium-4 works on a 400MHz bus speed
In common PC's:
system clock speed can be set in the main board
CPU clock speed is the bus speed times a "multiplier"
Example: A 150MHz CPU may be running at 2.5 x 60MHz, 3
x 50MHz or 2 x 75Mhz ("overclocked" system bus)
27
Features of Computers
Speed (continued)
RAM access speed
about 60ns (1 nanosecond = a billionth of a second), and
getting faster
may be rated with respect to "bus speed" (eg. PC-100)
Cache memory
faster than main memory (about 20ns access speed), but
more expensive
contains only data which the CPU is likely to use next
28
Features of Computers
Speed (continued)
I/O device speed
Mass storage access
Examples:
3.5in 1.4MB floppy disk: about 500kb/sec at 300 rpm
(revolutions per minute)
3.5in hard disk: average seek time about 8.5 microsecond,
and 7,200 rpm
Communications
Examples: modems at about 56 kilobits per second, and network
cards at 10 or 100 megabits per second.
Interface
Examples: ISA, PCI, IDE, SCSI, ATA, USB, etc....
29
Features of Computers
Reliability
Maintenance issues
Operating conditions
Example: heat, ventilation, "over-clocking, power supply
Error detection
Example: parity check, CRC
Note: Most system failures are due to software
(i.e. programming) flaws rather than hardware
30
Modes of communication
Parallel communication:
all the bits are transferred at the same time
each bit on a separate line
Serial communication:
one bit at a time.
31
Modems (MOdulator-DEModulator)
Remote
Computer
Digital
Data
Modem
Audio signal
phone lines
Home
Computer
Digital
Data
Modem
32
Computer Networks
Types
Local Area Networks (LAN)
Computers in an organization.
Example: the PCs in the lab.
Long Haul Networks
Separated by hundreds or thousands of miles.
Physical wires, telephone lines, satellites, etc.
Example: Internet backbone
33
Computer Networks
Method of communication
Each computer has an address
Example: IP address
A sender computer transmits data through the
network in packets, with each packet tagged
with the destination and return address
When data is too big to fit in one packet, the sender computer
can split the data into several packets, labeled in sequence
34
Computer Networks
Method of communication (continued)
The packets are sent via links from computer to
computer (routing)
Each intermediate computer receives and
retransmits the message (hops)
The packets do not necessarily arrive in the order in which
they were sent.
The recipient computer puts the packets in
correct sequence and retrieves the data
35
Summary
Transistors, gates, chips, hardware.
Computer = CPU + Memory + I/O Devices
Networks, modems
Jargons and acronyms
36
Week 2
Introduction to Programming
37
Software
Software are programs written to perform
specific tasks.
Two types of programs
System programs - programs that take control of
the computer, such as the operating system (e.g.
Windows, OS/2, Linux, etc.)
Application programs - programs that perform a
specific task. (e.g. Word processors, spreadsheets,
and games are examples of application programs)
38
Evolution of Operating Systems
Batch processing
Do only one job or task at a time
Operating systems
Manage transitions between jobs
Increased throughput
Amount of work computers process
Multiprogramming
Computer resources are shared by many jobs or tasks
Timesharing
Computer runs a small portion of one users job then
moves on to service the next user
39
The Language of a Computer
Two types of electrical signal - analog and digital. Since
inside the computer digital signals are processed,
the language of a computer is a sequence of 0s and 1s.
The language of a computer is called the machine
language.
The digit 0 or 1 is called a binary digit or in short form a
bit.
A sequence of 0s and 1s is also referred as a binary code.
40
The Language of a Computer (cont.)
Bit: A bit is a binary digit 0 or 1.
Byte: A sequence of 8 bits.
Coding Scheme
ASCII (American Standard Code for Information
Interchange).
128 characters
A is encoded as 1000001 (66th character)
3 is encoded as 0110011.
EBCDIC (used by IBM)-256 characters
Unicode - 65536 characters. Two bytes are needed to store a
character.
41
The Evolution of Programming Languages
Early computers were programmed in machine language.
Example
Suppose we want to represent the equation
work = force distance
to calculate the work in machine language.
If 100100 stands for load, 100110 stands for multiplication and
100010 stands for store, then the following sequence of instructions
might be needed to calculate the weekly wages.
100100 0000 010001
100110 0000 010010
42
100010 0000 010011
1940s Numerically based Problems +
Handcoded Machine Language Programs
Numeri
Proble
cal
ms
43
The Evolution of Programming Languages
(cont.)
Assembly languages : an instruction in assembly language
is an easy-to-remember form called a mnemonic.
Using the assembly language instructions, the equation to calculate the weekly
wages can be written as follows:
LOAD
force
MULT
distance
STOR
work
Assembler: An assembler is a program that translates a program written in 44
assembly language into an equivalent program in machine language.
The Evolution of Programming Languages
(cont.)
High level languages: Basic, FORTRAN,
COBOL, Pascal, C++, C
In order to calculate the work, the equation
work = force distance
in Pascal, can be written as follows:
work := force * distance;
Compiler: A compiler is a program that
translates a program written in a high level
language to an equivalent machine language.
45
46
PROGRAMMING
LANGUAGES
BASIC
Stands for Beginners All-purpose Symbolic Instruction Code. Basic was
developed in 1964 and is still in use today. Many variations of it exist and its
on most desktop and notebook computers. Its a procedural, interpreted
language.
Visual
Basic
Developed by Microsoft, VB uses a graphical interface to develop eventdriven Windows programs. Visual Basic for Applications (VBA) is a subset of
VB that allows programmers to develop macros in the Microsoft Office
environment.
COBOL
Stands for Common Business Oriented Language, COBOL is a widely used
procedural language in business applications. Its primarily used on
mainframe computers.
C is a compiled procedural program used to develop operating systems and
application software. It is a powerful and efficient program, but its difficult
to use.
C++
An object-oriented version of C. Its used for man of the same applications as
C as well as database and Internet programs. You dont need to know C to
learn C++.
FORTRA
N
Stands for Formula Translator. FORTRAN is the oldest high-level language
still in use. Scientists and engineers use it to handle complex mathematical
and scientific calculations.
Pascal
Developed in the late 1960s to help students learn structured programming
concepts. Its named after the 17th-century French mathematician, Blaise
Pascal. Turbo Pascal is an object-oriented version.
Java
A high-level, object-oriented programming language based on C++. Java is
optimized for Web and Internet appliance applications.
47
Processing a High-Level Language
Program
The following steps are necessary to execute a
program written in a high level language, say, Pascal:
1. Use an editor to create a program (that is type) in Pascal. This
program is called the source program.
Source program: A program written in a high-level language.
2. Check that the program obeys the rules of the programming
language and translate the program into an equivalent machine
language. All this is accomplished by the compiler. The
equivalent machine language program is called an object
program.
Object program: The machine language version of the high-level
language program.
48
Processing a High-Level Language
Program
3. The programs that you write in a high-level language are
developed using a software development kit (SDK), which
contains many programs that are useful in creating your
program. This prewritten code resides in a place called the
library.
Linker: A program that combines the object program with
other programs provided by the SDK and used in the program
to create the executable code.
4. The next step is to load the executable program into the main
memory for execution and a program called loader
accomplishes this.
Loader: A program that loads an executable program into
main memory.
5. The final step is to execute the program.
49
Processing a High-Level Language
Program
50
Programming with the Program
Analysis-Coding-Execution Cycle
Programming is a process of problem solving.
Problem solving techniques
Analyze the problem
Outline the problem requirements
Design steps, called an algorithm, to solve the problem
Algorithm: A step-by-step problem-solving process
in which a solution is arrived at in a finite amount
of time.
51
Programming with the Program
Analysis-Coding-Execution Cycle
Problem solving process
1. (a) Analyze the problem.
(b) Outline the problem and its solution requirements.
(c) Design steps (algorithm) to solve the problem.
2. (a) Implement the algorithm in a programming language,
such as Pascal.
(b) Verify that the algorithm works.
3. Maintenance: Maintenance requires using and modifying the
program if the problem domain changes.
52
Problem Analysis-Coding-Execution Cycle
53
Programming with the Program
Analysis-Coding-Execution Cycle
Analysis of the problem is the first and the most important
step. This phase requires us to:
1. Thoroughly understand what the problem is about.
2. Understand the problem requirements. Some of the requirements could be:
a. Does the program require interaction with the user?
b. Does the program manipulate data? If the program manipulates data,
the programmer must know what the data are and how the data are
represented, that is, look at sample data.
c. Is there any output of the program? If yes, the programmer should
know how the results should be generated.
3. If the problem is complex, divide the problem into sub-problems. Analyze
each sub-problem as above.
54
Programming with the Program
Analysis-Coding-Execution Cycle
Dividing a problem into smaller subproblems is called
structured design.
The structured design approach is also known as top-down
design, stepwise refinement, and modular programming.
In structured design, the problem is divided into smaller
problems.
Each subproblem is then analyzed, and a solution is obtained to
solve the subproblem.
The solutions of all subproblems are then combined to solve the
overall problem.
This process of implementing a structured design is called
structured programming.
55
Programming with the Program
Analysis-Coding-Execution Cycle
The next step is to design an algorithm to solve the
problem.
If the problem was broken into subproblems, design
algorithms for each subproblem.
Once the necessary steps have been designed, check the
correctness of the algorithm.
Sometimes algorithms correctness can be tested using
sample data.
At times some mathematical analysis might be required to
test the correctness of the algorithm.
Once the algorithm is designed and correctness is
verified, the next step is to write the equivalent code
into the high level language.
Then using an editor enter the program into the56
Programming with the Program
Analysis-Coding-Execution
The
next step is to ensure that the program Cycle
follows the
constructs of the language.
Run the code through the compiler.
If the compiler generates error, we must go back, look at the
code, remove the errors, and run the code again through the
compiler.
If there are no syntax errors, the compiler generates the
equivalent machine code, the linker links the machine code
with the systems resources, and the loader can then place the
program into the main memory so that it can be executed.
The final step is to execute the program.
The compiler only guarantees that the program follows
the rules of the language. It does not guarantee 57that the
program will run correctly.
Problem Analysis-CodingExecution Cycle
58