Anda di halaman 1dari 67

INTRODUCTION

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

Device capable of performing computations and


enacting logical decisions
Todays computers perform these tasks millions
and even billions of times faster than people can
Supercomputers can perform hundreds of billions,
even trillions of instructions per second
Computers process data through sets of
instructions called computer programs
Programs guide the computer through actions as
specified by people called computer programmers;
For this course, that will be YOU.

What are hardware and software?


Hardware
Electronic and
mechanical equipment

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

Evolution of the computer


Early computers used batch processing one job
(program) at a time
Very inefficient, sometimes it took days to get your
output
Operating systems (OSs) were first developed to
handle switching between jobs
Also very inefficient (lots of time spent switching)
Next came multiprogramming OSs several
programs took turns sharing the computers
resources
Better, but not by much

Evolution of the computer contd


Timesharing developed in the sixties
Users could access the computer through dumb
terminals
Programs appear to run simultaneously and response
to input produced almost immediately

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

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
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

Personal, Distributed and Client/Server


Computing
Personal computing
Popularized by Apple Computer in 1977
IBM followed suit in 1981 with the IBM Personal Computer

Computers economical enough for personal use


Stand-alone units

Distributed computing
Organization has a Local Area Network (LAN)
Computers linked to it

Computing distributed over the LAN, rather than at one


central location
12

1.5

Personal, Distributed and Client/Server


Computing

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

1.3 Computer Organization


Six logical units in every computer
Input unit
Gets information from input devices (keyboard, mouse)

Output unit
Gets information (to screen, to printer, to control other devices)

Memory unit
Rapid access, low capacity, stores input information

Arithmetic and logic unit (ALU)


Performs arithmetic calculations and logic decisions

Central processing unit (CPU)


Supervises and coordinates the other sections of the computer

Secondary storage unit


Cheap, long-term, high-capacity storage, stores inactive
programs and data

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)

Memory (RAM, D-RAM,


) temporary storage
Input/Output Devices
(Peripherals)

Monitor
Keyboard
Hard Drive
Floppy Drive
CD-ROM, DVD-ROM
Printers,

...

16

Input / Output Devices


Monitor

Keyboard

Monitor screen
Keyboard
Mouse
Bar code scanner
Light pen
Touch screen

Central
Processing
Unit

I/O devices allow user


interaction

Hard Disk
Main
Memory
Floppy Disk

17

CPU and Main Memory


Central
Processing
Unit

Primary storage area


for programs and data
that are in active use

Chip that executes


program commands
Intel Pentium 4
AMD Athlon
PowerPC G4
Sun Sparc Processor

Main
Memory

18

The Central Processing Unit


A CPU is also called a microprocessor
It continuously follows the fetch-decode-execute cycle:
Retrieve an instruction from main memory

fetch
execute
Carry out the
instruction

decode
Determine what the
instruction is

19

The Central Processing Unit


The speed of a CPU is controlled by the system clock
The system clock generates an electronic pulse at regular
intervals
The pulses coordinate the activities of the CPU
The speed is measured in hertz (Hz) = 1 / second
Megahertz (MHz) = 1 million Hz
Gigahertz (GHz) = 1 billion Hz

20

Secondary Memory Devices


Secondary memory
devices provide
long-term storage

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

Two Kinds of Memory


Main
working area
stores program and data (while program is executing)
erased when power goes off

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

220 (over 1 million)

gigabyte

GB

230 (over 1 billion)

terabyte

TB

240 (over 1 trillion)

1 byte = 8 bits

23

A Computer Specification
Consider the following specification for a personal
computer:

1.2 GHz Pentium 4 Processor


512 MB RAM
40 GB Hard Disk
24x speed CD ROM Drive
17 Multimedia Video Display with 1280 x 1024 resolution
56 KB Modem

What does it all mean?

24

A Computer Specification
Consider the following specification for a personal
computer:

speed of CPU clock


1.2 GHz Pentium 4 Processor
main memory capacity
512 MB RAM
a form of secondary memory (ca
40 GB Hard Disk
24x speed CD ROM Drive a form of secondary mem (sp
17 Multimedia Video Display with 1280 x 1024 resolution
monitor, output
56 KB Modem
modem, input & output (speed)

25

How do we tell the computer what to do?


A program is a set of instructions for a machine to
accomplish a specific task or solve a specific
problem.
Program is an algorithm written in a language a computer
can execute.

Computers do not understand the intention of a


program. (Computers do what you tell them, not
what you meant to tell them.)
Programs must be precise
Programs must be detailed
Programs must be debugged.

26

How do we tell the computer what to do?


Computer Program - a series of instructions that tells the
computer what to do

27

28

Getting from Source


to Machine Code

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

**The compiler and interpreter are themselves programs.


29

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

High-level languages (compiled or interpreted)


Pascal, Lisp, Ada, Java, Basic, C, C++, etc.

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)

Assembly language is a language in which computer


operations are represented by mnemonic codes (rather
than by binary numbers)
(e.g., ADD A)

High-level language is any language used to write a


program that can be executed on any CPU; it combines
algebraic expressions and English symbols
Pascal, C, C++, Java, BASIC, FORTRAN, COBOL

31

Programming Languages
Low Level
Machine Code
Assembly Code

High Level
Many languages a few are

Visual Basic
C/C++
Java
Pascal

Emerging Model : Virtual Machine

32

Low Level Programming

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.

ADD Register1, Register2


MOV Register 3, Mem Address 100
SUB Register1, 5
Etc.

Easier than machine code, but still tedious

33

High Level Languages


Written in a language much closer to English, so easier to
write
Source Code : Program written in the high level language
E.g. Print Hello there

Compiler
Translates the source code into machine code that the computer
can understand; code is machine specific

PC Compiler

PC Machine Code

Mac Compiler

Mac Machine Code

C++ source

34

Virtual Machine
The Virtual Machine concept has recently become very popular
Used by Java and Microsoft .NET

A compiler translates source code into machine independent byte


code that can be executed by a virtual machine.
This machine doesnt actually exist! It is simply a specification of how a
machine would operate if it did exist in terms of what machine code it
understands.
However, the byte code is fairly generic to most computers, making it
fairly easy to translate this byte code to actual native machine code.

35

1.6 Machine Languages, Assembly Languages


and High-Level Languages
Types of programming languages
1. Machine languages
Strings of numbers giving machine specific instructions
Example:
+1300042774
+1400593419
+1200274027

2. Assembly languages
English-like abbreviations representing elementary computer operations
(translated via assemblers)
Example:
LOAD
BASEPAY
ADD
OVERPAY
STORE GROSSPAY

36

1.6 Machine Languages, Assembly Languages


and High-Level Languages
Types of programming languages
3.

High-level languages

Similar to everyday English and use mathematical notations


(translated via compilers)
Example:
grossPay = basePay + overTimePay

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

First, the information must be transformed (converted) into an


acceptable representation that the computer will accept.
That format is ultimately a binary number

39

Digital Information
Computers store all information digitally:

numbers
text
graphics and images
audio
video
program instructions

In some way, all information is digitized - broken down


into pieces and represented as numbers

40

What is Information?
All modern computers work with a system of numbers called binary
numbers.
Use only two symbols: 0 and 1.

Binary circuits: Electronic devices are cheapest and function most


reliably if they assume only two states.

Closed
circuit

Open
circuit

41

Representation of Numbers
The three-light system:
Has eight possible
combinations of
and off.

on

Could be used to indicate the


numbers 0, 1, 2, 3, 4, 5, 6, 7.
0 = 000
1 = 001
2 = 010
3 = 011

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

Each number has a unique


representation.
Counting:

Each number has a unique


representation.
Counting:

When you run out of


digits, make it a zero and
increment the next place
value to the left.
99 becomes 100

When you run out of


digits, make it a zero and
increment the next place
value to the left.
11two becomes 100two

45

Representation of Symbols and Text

To store any kind of information in the computers memory, it must first


be transformed into a binary numeric form.

Symbols and Text


Includes characters, punctuation, symbols representing numbers.
Each symbol can be assigned a numeric value.
Two standardized sets of codes for symbols:
ASCII: American Standard Code for Information Interchange. Can represent
255 different characters/symbols.
UNICODE: More modern code that can represent 65536 characters/symbols
(useful for other languages such as Arabic, Chinese).

46

Representation of Symbols and Text


ApartiallistingoftheASCIIcharacterset

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

In a picture with only


black and white pixels:
1 represents black.
0 represents white.

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

Photographic quality images


have a gray-scale.
Several shades between black
and white are used.
4 level gray-scale means 4
shades are used.

Each pixel needs 2 bits:


00 - represents white
01 - represents light gray
10 - represents dark gray
11 - represents black

256 level gray scale means


8 bits per pixel are needed for
256 shades of gray

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

Steps in Creation of a Program


1.
2.
3.
4.

Define the problem.


Analyze the problem.
Design an algorithm to solve the problem.
Express the an algorithm in a computer language and
store in one or more files.
Text Editors are used for file creation, languages such as
C, C++, BASIC, assembler, JAVA, etc., are used to
express the algorithm.
5. Compile the files to create a program. (Most languages)
6. Run/execute the program.
7. Test and verify the completed program.
53

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

There are no real standards for expressing algorithms in pseudo-code.


Most algorithms consist of
(1) Get the data
(2) Perform computations
(3) Display results

54

Algorithms

Algorithm - a set of instructions (steps) for solving a problem.


must be precise
must be complete
May be in a number of different formats
natural language (such as English)
a specific programming language
a diagram, such as a flow chart
pseudocode - a mix of natural and programming language

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..

C was originally developed by Dennis Ritchie of


Bell Labs. Its definition is now standardized
58

Steps in Programming in C
1.
2.
3.
4.
5.

State the problem


Analyze the problem
Design an algorithm to solve the problem; express the
algorithm in pseudo-code.
Code the algorithm in C language; input the C code into a
file
Compile the file(s)
1.
2.
3.

6.
7.

pre-processing (step increases power and portability)


compilation (translates C language into machine language)
linking (combines compiler output with libraries of other
algorithms)

Execute the program


Test and verify the completed program
59

The First Program


/* The traditional first program in honor of
Dennis Ritchie who invented C at Bell Labs
in 1972 */
#include <stdio.h>
int main(void)
{
printf(Hello, world!\n);
return 0;
}
60

Dissection of the first program


/* The traditional first program in honor of
Dennis Ritchie who invented C at Bell
Labs in 1972 */
These lines are considered to be a comment. A
comment is ignored by the compiler and serves to
document the program for a human reader. In C,
comments begin with the pair of symbols /* and
end with the pair */.
/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs
in 1972 */
#include <stdio.h>
int main(void)
{
printf(Hello, world!\n);
return 0;
}

61

Dissection of first program


#include <stdio.h>
Example of a preprocessor directive. The
preprocessor will find a file with the name stdio.h
and the contents of that file will be included as
part of the program. Preprocessor directives
begin with the # symbol as the first non-blank
character on the line.
In our program, the directive defines the printf()
function. /* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs
in 1972 */
#include <stdio.h>
int main(void)
{
printf(Hello, world!\n);
return 0;
}

62

Dissection of first program


int main(void)
Every program has a function called main.
The void keyword indicates that the function main does not
take parameters.
The keyword int indicates the function will return an integer
value. In this case the value is returned to the operating
system.
When a program is called the operating system passes
control to the function called main.
Statements between the { symbol and the corresponding } are
part of the function main.
/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs
in 1972 */
#include <stdio.h>
int main(void)
{
printf(Hello, world!\n);
return 0;
}

63

Dissection of first program


printf(Hello, world!\n);
The printf statement is an example of invoking a function.
The printf function prints information on the screen.
Given the argument Hello, world!\n, it prints the following
output on the screen.
Hello World!
The \n in the string Hello, world!\n, tells printf to skip to a
next line. A single newline character is output.
Arguments are passed to functions within parentheses, if there
are no arguments the keyword void is used (as was the case
here)
Notice that statements end with a ; symbol
/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs
in 1972 */
#include <stdio.h>
int main(void)
{
printf(Hello, world!\n);
return 0;
}

64

Dissection of first program (seventh line)


return 0;
return causes the function containing it (the function
main in this example) to pass control back to the
function that called it (in this case an operating
system function). The value 0 is returned to the
calling function. In our example, the main
function returns an integer value, it does this by
providing an integer in a return statement.
/* The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs
in 1972 */
#include <stdio.h>
int main(void)
{
printf(Hello, world!\n);
return 0;
}

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

Anda mungkin juga menyukai