Computer Programming: Shashidharan Department of Civil Engineering N S S College of Engineering Palakkad
Computer Programming: Shashidharan Department of Civil Engineering N S S College of Engineering Palakkad
to
COMPUTER PROGRAMMING
Shashidharan
Department of Civil Engineering
N S S College of Engineering Palakkad
What Is a Computer?
How is a computer defined?
Electronic machine operating
under the control of instructions
stored in its own memory
Accepts data
Manipulates data
Produces results
Stores results
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
Software
Instructions that
tell hardware how
to perform tasks
Examples of Hardware
Computer Monitor, Keyboard, Mouse, Base,
RAM, processor
Computer Printer
Video Game Console
CD-ROM, Floppy-Disk, Hard-Drive,
Modem
Network Hubs, switches, routers
Cables and cords
Examples of Software
Operating Systems:
Windows 3.1, 95, 98, CE, NT, 2000, XP,
UNIX, Linux,
Applications:
Internet Explorer, Netscape, ...
Email program
Executable files on a CD, DVD, Floppy-Disk or Hard
Drive
Personal Computing
In 1977, Steve Jobs and Steve Wozniak popularized
personal computing with the Apple (proprietary
equipment)
In 1981, IBM introduced the IBM Personal Computer
(PC), using off-the-shelf components
Todays top PCs are as powerful as the million dollar
machines of only ten years ago
10
Operating systems
Manage transitions between jobs
Increased throughput - amount of work computers process
Multiprogramming
Many jobs or tasks sharing the computer resources
Timesharing
Runs a small portion of one users job then moves on to
service the next user
Programs appear to be running simultaneously
11
Distributed computing
Organization has a Local Area Network (LAN)
Computers linked to it
1.5
Client/Server computing
File servers offer common store of programs that client
computers access
C and C++ popular for writing operating systems,
networking, and distributed client/server applications
Java used for Internet-based applications
Programming in Java can be more productive than C or C++
13
Output unit
Gets information (to screen, to printer, to control other devices)
Memory unit
Rapid access, low capacity, stores input information
14
Elements of a computer
Input device
Output device
Memory
Arithmetic and logic unit (ALU)
Central Processing Unit (CPU)
Secondary Storage (e.g. hard disk, floppy)
15
Computer Components
Processor (CPU) executes
instructions
(e.g., Pentium IV)
Monitor
Keyboard
Hard Drive
Floppy Drive
CD-ROM, DVD-ROM
Printers,
...
16
Keyboard
Monitor screen
Keyboard
Mouse
Bar code scanner
Light pen
Touch screen
Central
Processing
Unit
Hard Disk
Main
Memory
Floppy Disk
17
Main
Memory
18
fetch
execute
Carry out the
instruction
decode
Determine what the
instruction is
19
20
Hard disks
Floppy disks
ZIP disks
Writable CDs
Tapes
Central
Processing
Unit
Information is moved
between main memory
and secondary memory
as needed
Hard Disk
Main
Memory
Floppy Disk
21
Secondary
saves program and results
includes floppy & hard disk drives, CDs, tape, etc.
persists after power is off
22
Storage Capacity
Every memory device has a storage capacity,
indicating the number of bytes it can hold
Capacities are expressed in various units:
Unit
Symbol
Number of Bytes
kilobyte
KB
210 = 1024
megabyte
MB
gigabyte
GB
terabyte
TB
1 byte = 8 bits
23
A Computer Specification
Consider the following specification for a personal
computer:
24
A Computer Specification
Consider the following specification for a personal
computer:
25
26
27
28
Compiler
a program that translates code in a higher-level language to code in a lowerlevel language before run time, typically from the highest level (source
code) to the lowest level (machine code)
Compiling a program
Interpreter
a program that translates code in a higher-level language to code in a lowerlevel language at run time
Compilers and interpreters need to know the specific target hardware
Programming Languages
Machine language (we will call this object code)
Hardware-dependent, cumbersome manipulation of series of
numbers (1s and 0s)
Assembly language; also hardware dependent (has a oneto-one correspondense to maching language), e.g.
LOAD BALANCE
ADD TAX
STORE TOTAL
30
Computer Languages
Collection of symbols and rules that convey
information
Machine language is the native language of a computer,
it is a series of 0s and 1s (binary)
(e.g., 00010101)
31
Programming Languages
Low Level
Machine Code
Assembly Code
High Level
Many languages a few are
Visual Basic
C/C++
Java
Pascal
32
Machine Code
Directly coding in the 0s and 1s understood by the computer
E.g. for word hunt program
001010
001110
011110
000100
Very tedious
Assembly Code
Coding using mnemonics that map to binary code. Slightly easier to understand,
but still a 1 to 1 mapping between mnemonics and machine code
E.g.
33
Compiler
Translates the source code into machine code that the computer
can understand; code is machine specific
PC Compiler
PC Machine Code
Mac Compiler
C++ source
34
Virtual Machine
The Virtual Machine concept has recently become very popular
Used by Java and Microsoft .NET
35
2. Assembly languages
English-like abbreviations representing elementary computer operations
(translated via assemblers)
Example:
LOAD
BASEPAY
ADD
OVERPAY
STORE GROSSPAY
36
High-level languages
37
Information
First, lets focus on how information is stored
inside the computer
Then we will see a little of how the CPU operates
38
What is Information?
There are many different types of information; here are four types of
information the computer commonly manipulates:
Numeric
Character
Visual
Instructions
39
Digital Information
Computers store all information digitally:
numbers
text
graphics and images
audio
video
program instructions
40
What is Information?
All modern computers work with a system of numbers called binary
numbers.
Use only two symbols: 0 and 1.
Closed
circuit
Open
circuit
41
Representation of Numbers
The three-light system:
Has eight possible
combinations of
and off.
on
4 = 100
5 = 101
6 = 110
7 = 111
42
Representation of Numbers
Decimal numeration system:
Uses 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
The place values of each position are powers of ten.
A number such as 1357 will be expanded as:
104
103
102
101 100
10000 1000
100
10
1
(1 x 1000) + (3 x1100) + (53x 10) + (75 x 1) 7
=
= 1357 in the decimal system
43
Representation of Numbers
Binary numeration system:
Uses 2 symbols: 0, and 1. (Each is called a bit for binary digit)
The place values of each position are powers of two.
A binary number such as 10110two will be expanded as:
24
16
1
23
8
0
22
4
1
21
2
1
= (1 x 16) + (0 x 8) + (1 x 4) + (1 x 2) + (0 x 1)
= Only 22 in the decimal system!
20
1
0
44
Representation of Numbers
Decimal
Binary
45
46
Ctrl+@ - 0000000
Ctrl+A - 0000001
Ctrl+B - 0000010
Ctrl+C - 0000011
Ctrl+D - 0000100
Ctrl+E - 0000101
Ctrl+F - 0000110
Space - 0100000
Delete - 1111111
0 - 0110000
1 - 0110001
2 - 0110010
3 - 0110011
4 - 0110100
5 - 0110101
6 - 0110110
7 - 0110111
8 - 0111000
9 - 0111001
: - 0111010
A - 1000001
B - 1000010
C - 1000011
D - 1000100
E - 1000101
F - 1000110
G - 1000111
H - 1001000
I - 1001001
J - 1001010
K - 1001011
47
Representation of Images
Pictures:
A picture must be transformed into numeric form
before it can be stored or manipulated by the computer.
Each picture is subdivided into a grid of squares called
pixels (picture elements).
If the squares are small enough, we will see a reasonably good
image.
48
Representation of Images
010101010101010101010110101101001001000111110000
011010101010101010101001011010010110010100000110
100101010101010101010110110001010000101001010100
101101101011011010110101100110010110100010001001
011010010110100101101010001001100100101101010010
100101101100101011010101110110011001010010101100
011010010011010110010010001001100110101010010001
010101101100101100100101110110011001010100100101
010101010101010011011010001001100010100001010100
101010101010101100010010110010001101001110100001
010101010101010001000101000101101000010000001101
110110101010010100110100011010010011100101101000
101001010100100010100101100101101100001010000010
101011010001001001001001011110101011010100101100
101010000100010010010111110101111100101001001001
010100101001000100101010101110101011010010010000
101001000010011001101111101011101010101000100101
010010010100100011011000011110111011010110101000
000100000001001100100111111111110110111000000010
101000101010010011011000010101011101000010101000
000010000100101101010011111111111111011101000101
001000101001101010100100011101111110100010010000
010010010110001001001001111011110101101100100101
100100100000111010010010010111111111011001001000
49
Representation of Images
Thebaby'spicturewith
smallerpixelsmoredetail.
Thebaby'spicturewith4
levelsofgray.
50
Representation of Images
256levelsofgray
51
Representation of Images
Color Images with RGB
Uses three values per pixel
One number is used for
each of the amounts of
Red, Green and Blue on the
computer screen.
The amounts of Red, Green,
and Blue combine like light
or paint to create other
colors
Fullcolorimage
52
Algorithm
An algorithm is a step by step process used to carry out
some function:
e.g. recipes, driving directions, long-division
Algorithms are often expressed in pseudo-code.
WHILE class is in session
DO
listen to instructor.
IF material not understood THEN
Ask questions
END
ENDWHILE
54
Algorithms
55
56
57
C Language
Computer language developed as a portable
systems programming language.
Portable --- programs do not need to be rewritten to run
on other types of computers.
Systems programming --- Creation of operating
systems, such as Windows, UNIX, or Linux, or creation
of utilities such as file copying programs, printing
programs, database managers, etc..
Steps in Programming in C
1.
2.
3.
4.
5.
6.
7.
61
62
63
64
65
Escape sequences
In the printf statement the argument string contained the
symbols \n. The \ (backslash) symbol is called the escape
character. It tells the compiler the next character does not
have its usual meaning. The two characters result in a
newline character inside the string argument to printf. The
two characters are called an escape sequence.
Examples:
\n newline
\t tab
\r carriage return
\v vertical tab
\\ \ (backslash) character
\f formfeed.
66
Coding errors
Programs can have errors of many types.
Syntax errors occur when the language rules are
violated (Like grammatical errors in English).
Compilers usually catch syntax errors.
Syntax errors result in an error message. The compiler
does not produce output if the errors are severe.
A clean compile has no syntax errors.
67