CSL101: Introduction to Computers and Programming: Lecture 0

advertisement
CSL101: Introduction to Computers and
Programming: Lecture 1
Instructors: Saroj Kaushik and Vinay Robeiro
Prof Saroj Kaushik (Groups 1 to 3)
Room No : 416, Bharati School building (CSE)
Phone Number:
1292 (internal)
Email
:
saroj@cse.iitd.ernet.in
Web Link :
http://www.cse.iitd.ac.in/~saroj
Dr Vinay Rabeiro (Groups 6 to 10)
Room No : 421, Bharati School building (CSE)
Phone Number:
1297 (internal)
Email
:
vinay.ribeiro@gmail.com
Web Link :
http://www.cse.iitd.ac.in/~vinay
1st semester 2009-2010: Time Table
CSL101 Introduction to Computers and Programming
Section 1
Section 2
Lecture: 8.00-9.20 AM
Practical: 3.00-4.50 PM (CSC)
Lect Venue : II LT1
Lecture: 2.00-3.20 PM Practical:
11.00-12.50 PM (CSC)
Lect Venue : II LT1
Cycle 1 : G1, 6, 11 - Tuesday
Cycle 2 : G2, 7, 12 - Wednesday
Cycle 3 : G3, 8, 13 - Thursday
Cycle 4 : G4, 9, 14 - Friday
Cycle 5 : G5, 10, 15- Monday
Cycle 1 : G1, 6, 11 -Wednesday
Cycle 2 : G2, 7, 12 - Thursday
Cycle 3 : G3, 8, 13 - Friday
Cycle 4 : G4, 9, 14 - Monday
Cycle 5 : G5, 10, 15- Tuesday
Attendance
•
•
•
•
Compulsory
A test/quiz may be held on any day
Classes will begin on time
Cheating on exams and assignments
– We follow 0-tolerance policy
Course contents
• “Problem solving using computers” as opposed to “Programming”
• Concept of an algorithm
• Developing algorithms:
–
–
–
–
–
–
Specification
Stepwise refinement
Data organization
Efficiency
Termination and correctness
Testing and de-bugging
• Programming in a high level language: C
– Syntax and semantics
• Problem solving in science and engineering
• Introduction to computer architecture, operating systems, compilers
and application software
Course structure
•
•
•
•
Minor 1 and 2, each has 15-20% weight
Major, 30-35% weight
Quizzes and home assignments, 5%
Programming exercises, 20-25% weight
– 2 hr/week lab for each group
– Location is Computer Services Centre
– Lab exercises, rules, instruction on how / when / whom
to submit will be posted on website
http://www.cse.iitd.ac.in/~saroj
• Note these weights are indicative, and may change
as semester progresses
Submission of lab reports
• For each exercise there will be a deadline for
submission of report/code. Be sure that you
complete the exercise well before the deadline and
submit the report in time.
• We will not tolerate excuses (power fail, server
down, printer fault, lab closed, etc.).
• We will give you:
– a grace period of 1 day, provided you are willing to
accept a penalty of 10% of the marks assigned, or
– a grace period of 1 week (7 calendar days), provided you
are willing to accept a penalty of 50% of the marks
assigned to the exercise.
Text books
• R. G. Dromey “How to solve it by computer”,
Prentice Hall, Indian edition, 1999.
• Brian W. Kernighan, Dennis Ritchie, C
Programming Language (2nd Edition).
Electronic Computers
• Responsible for a revolution in almost all
spheres of areas such as
–
–
–
–
Science
Engineering
Business
Management
Types of Computers
• Classified into three categories
– Digital :
– Analog :
Process digital information
– Hybrid :
Combination of both
Data is represented by physical
quantities
(pressure, speed, volume etc)
• Generally by computer we mean digital
computer
Digital Computer
• Capable of performing
–
–
–
–
arithmetic operations on numbers
logical operations on bits (binary digit)
decision test
retrieval, manipulation and storage of large
amount of data at a very high speed
Computer System
• A computer system is made up of both
– hardware and
– software
• Software is another term for computer program.
• Software controls the computer and makes it do
useful work.
• Without software a computer is useless, just as a
car without someone to drive it.
• To get a computer to perform a specific task it
must be given a sequence of unambiguous
instructions called a program written in some
programming language.
• Hardware components of a computer system are the electronic
and mechanical parts.
• It is the physical medium.
• The major hardware components are:
–
–
–
–
Processor
Main memory & Secondary memory
Input devices (keyboard)
Output devices (printer)
• The processor, main memory, and secondary memory devices
are inside the system unit seen as metal box.
• The keyboard and monitor (the TV-like screen) are
input/output devices.
• Hardware components also include
– mouse, disk drive, soundcard and speakers
– CD ROM drive (Read Only Memory), network card
– floppy disc drive (for backing up data)
Types of Computers
• By 1980, computers were classified into three main types.
• Main Frame:
– Traditionally large computers, often containing thousands of IC’s
and costing millions of dollars
– High speed, large storage & highly accurate
– can be shared by group of users
• Mini Computers:
– Smaller in size, slow in speed, low cost and can be shared by group
of users
• Micro Computers (Personal computer - PC):
– New class of general purpose machines intended for single user.
– Small in size and inexpensive
The following schematic diagram gives the
layout of a digital computer.
• CPU (Central Processor Unit) does the
`work‘ of fetching, storing and manipulating
data that are stored in the computers memory.
• Main memory is used to store program and
data during execution of a program.
• Disc drive (hard or floppy) used to store files
(program + data) `permanently‘.
• Keyboard allows user to input information.
• VDU (Visual Display Unit) visually outputs
the data.
• Printer allows a hard copy to be made.
The current architecture of PC (personal Computer) is
as follows (major hardware components )
The arrows show the direction of data flow.
• The bus is a group of wires on the main circuit board of the
computer through which electrical signal passes.
• It is a pathway for data flowing between components.
• Buses are of three types:
– Address bus:
• used to transmit address of memory location
– Data bus:
• Used to transmit data from/to memory
– Control bus:
• Supervises reading/writing of data
• Most devices are connected to the bus through a controller.
• Controller coordinates the activities of the device and the bus.
Central Processing Unit (CPU)
• The processor as its name suggests is the unit that does
the work of the computer system i.e.
– it executes computer programs.
• Instructions in the program tell the processor
– when and what to read from a keyboard;
– what to display on a screen;
– what to store and retrieve from a disk drive and so on.
• It can do arithmetic, compare numbers and perform
input/output. (read information and display or store it).
• It has no magical powers. It is instructive to bear in mind
that all computer programs are constructed from
sequences of instructions based on such primitive
operations.
• The processor itself is made up of a number of components
such as the
– arithmetic logic unit (ALU) and
– control unit (CU).
• The ALU carries out arithmetic operations (e.g. addition and
subtraction) and logical operations (e.g. and, or, not)
• The CU controls the execution of instructions.
• Traditionally, the processor is referred to as the central
processing unit or CPU.
• With the advent of microprocessors, the term MPU or
microprocessor unit is also used.
• A microprocessor is simply a processor contained on a single
silicon chip.
• Processors come in all different `shapes and sizes'
– There are many different types of architectures which are suited to a
variety of different tasks
Main Memory
• Main memory is where programs and data are kept during
execution that the processor is actively working with.
• when the processor is actively using them.
• Main memory is also called Random Access Memory
(RAM) or primary memory.
• "Random" means that the memory cells can be accessed in
any order randomly.
• Main memory is:
– very closely connected to the processor.
– the contents are quickly and easily changed.
– interacts with the processor millions of times per second.
Micro Computer Memory
• It has two types of memory.
• Random Access Memory (RAM)
–
–
–
–
Volatile
Random access to any memory location
Read & write access
Program + data reside in this memory
• Read only Memory (ROM)
– Non volatile
– Only read access
– Permanent programs reside
Cache Memory
• A special and very high speed memory used
to increase the speed of processing.
• It lies between CPU and main memory.
• It is expensive and usually small in size.
• Stores only segments of programs currently
being executed by CPU.
• It is also sometimes called buffer.
Secondary memory
• Connected to main memory through the bus
and a controller.
• The contents are easily changed, but this is
very slow compared to main memory.
• It is used for long-term storage of programs
and data.
• Common secondary storage devices are the
hard disk and floppy disks and CDs.
• The hard disk
– has enormous storage capacity compared to main
memory (300 times the amount of storage in main
memory).
– storage capacity of 40/80 Gigabytes and Terabytes.
– usually contained in the systems unit of a
computer.
Quick comparison of two types of storages:
Primary memory
Secondary memory
1.
2.
3.
4.
1.
2.
3.
4.
Fast
Expensive
Low capacity
Connects directly to
the processor
Slow
Cheap
Large capacity
Not connected
directly to the
processor
Some Basic Terminology
• Bit is short for Binary Digit.
– Bit has only two values represented as: 1 or 0,
(on or off ; true or false),
•
•
•
•
•
Byte consists of 8 Bits.
1 KB (1 KiloByte) contains 1024 Bytes.
1 MB (1 MagaByte) consists of 1024 KB.
1 GB (1 GigaByte) has 1024 MB.
1 Terabyte consists of 1024 GB.
Table of units of measurement
Name
Equivalent
Number of Bytes
power of
2
byte
8 bits
1
20
Kilobyte
(KB)
1024 bytes
1024
210
Megabyte
(MB)
Gigabyte
(GB)
1024 KB
1,048,576
220
1024 MB
1,073,741,824
230
Terabyte
1024 GB
1,099,511,627,776
240
Organization of Main Memory
• Main memory consists of a very long list of bytes.
• In most modern computers, each byte has an address.
• Each row is called memory location (single byte and has an
address).
• The addresses are the integers to the left of the boxes: 0, 1,
2, 3, 4, ... and so on.
• The addresses for most computer memory start at 0 and go
up in sequence until each byte has an address.
• The address of a byte is not part of its contents.
• The bits at a memory location are called the contents of that
location.
• Each location contains a pattern of eight bits, each bit is
either 0 or 1.
• By the term that a computer has "128 MB of RAM" they are
talking about the size of memory.
• Groups of memory locations can be treated as a
whole to allow more information to be stored.
• All machines have a wordsize: a fundamental
unit of storage,
– For example, 8-bits, 16-bits, etc.
• The size of a word (in Bytes) differs between
machines.
• A Pentium based machine generally has word
size of 32-bits.
•
•
•
•
The CPU can read to and write from a
specified memory location.
Most processors can write (and read) more
than a single byte at a time.
While writing a byte into a given location,
the previous bit pattern is destroyed and the
new contents (bit pattern) are saved for
future use.
While reading a byte from a given location
–
–
The processor gets the bit pattern stored at that
location.
The contents of that location are NOT changed.
• CPU also has a small number (usually less than 100) of
storage locations to store information that are currently
being processed.
• These locations are called registers and depending on the
processor, a register may typically store 8, 16, 32 or 64 bits.
• Processors with a register size of
– n-bits are called n-bit processors,
– processors with 8-bit registers are called 8-bit processors,
– similarly there are 16-bit, 32-bit and 64-bit processors.
• CPU has several important registers
–
–
–
–
–
Memory address register (MAR)
Memory data register (MDR)
Program counter (PC)
Current instruction register (CIR)
Accumulator
• The greater the number of bits the more powerful the
processor is, since it will be able to process a larger unit
of information in a single operation.
• For example, a 32-bit processor will be able to add two
32-bit numbers in a single operation whereas an 8-bit
processor will only be able to add two 8-bit numbers in
a single operation.
• An n-bit processor will usually be capable of
transferring n-bits to or from memory in a single
operation.
• This number of bits is also referred to as the memory
word size. So, while a byte refers to an 8-bit quantity, a
word can mean 8, 16, 32, 64 or some other number of
bits.
• On some machines a word is taken to mean a 16-bit
quantity and the term long word is used to refer to a
32-bit quantity.
Software
• Software is a program and data that a computer uses.
• Software is kept on some hardware device such as a hard disk
or floppy disk for further use.
• Programs are lists of instructions for the processor.
• Data can be any information that a program needs. It could be:
– character data, numerical data, image data, audio data, and
countless other types.
• The distinction between programs and data within the
computer memory is not as clear-cut as you might think,
however.
• However both programs and data are saved in computer
memory in the same way.
• The electronics of computer memory (both main memory and
secondary memory) make no distinction between programs
and data.
• The programs and data can be saved using the same electronic
methods.
• This is one of the most important ideas in computer science.
• Computer systems can use their memory for whatever needs
arise.
• A computer that is dedicated to running a program that
controls another device is an embedded system.
• An embedded system is usually embedded inside the device it
controls.
• Usually they run just one program that is permanently kept in a
special kind of main memory called ROM (for Read Only
Memory).
• More processor chips are sold per year for embedded systems
than for all other purposes
Types of Programs
• There are two categories of programs.
– Application
programs
(usually
called
just
"applications") are programs that people use to get their
work done.
– Systems programs keep all the hardware and software
running together smoothly. It helps in developing
application programs.
• The difference between "application program" and
"system program" is fuzzy.
• Often it is more a matter of marketing than of
logic.
Application Programs
Systems Programs
•
•
•
•
•
•
•
•
•
•
•
•
Word processors
Game programs
Spreadsheets
Data base packages
Graphics programs
Web browsers
Operating system.
Networking system.
Database system.
Compilers
Web site server.
Data backup.
Operating Systems (OS)
• The operating system is a complex collection of
many programs that governs the control of various
resources such as:
–
–
–
–
–
Processor
Main Memory
Secondary storage
I/O devices
Files
• The operating system is software; the same
hardware can be used with many different
operating systems (although only one at a time.)
Modules of OS
• The operating system is always present when
the computer is running.
• It coordinates the operation of all the
hardware and software components of the
computer system.
• The operating system is responsible for
starting application programs running and
finding the resources that they need.
• The operating system manages the details of
the resources an application requires while it
is running.
• There are various modules of OS
–
–
–
–
Processor management
Memory management
Device management
Information management
• These modules resolve conflicts, optimize
performance and acts as an interface between the
user’s program and computer hardware.
• Modern operating systems usually come with a user
interface that enables users to easily interact with
application programs by using windows, buttons,
menus, icons, the mouse, and the keyboard.
• Examples of operating systems are DOS, Unix,
Windows 98, Windows NT, Linux, Solaris, etc.
• There are different OS for different kinds of
machines.
–
–
–
–
–
Single user machine
Batch processing
Multiprogramming
Time sharing
Real time machines
Problem solving using computers
• Problem definition
• Method (how to solve it)
–
–
–
–
•
•
•
•
Algorithm
Data structure
Verification for correctness
Analysis for efficiency
Coding in given “programming language”
Understanding of computer “architecture”
“Compilation”, “testing”, “de-bugging”
Documentation
Problem definition
• Most difficult
• Requires interaction between “programmer” and user
• Specs include:
– Input data
• Type, accuracy, units, range, format, location, sequence
– Special symbols to signal end of data
– Output data (results)
• type, accuracy, units, range, format, location, “headings”
– How is output related to input
– Any special constraint
• Example: “find the phone no. of a person”
• Problems get revised often
Algorithm
• It is a finite set of instructions which, if
followed accomplish a particular task.
• It is basically used to describe a problem
solving method suitable for implementation
as a computer program.
• Algorithm is independent of the machine and
language used for implementation.
Characteristics of an Algorithm
• Input
• Zero or more quantities are supplied externally
• Output
• At least one quantity is produced
• Definiteness
• Each instruction is clear & unambiguous
• Finiteness
• It terminates after finite steps
• Effectiveness
• Each instruction is simple to be carried out
manually.
Algorithms(1)
• An “unambiguous specification of a method”
• Is characterized by:
– Ordered sequence of well-defined, effective operations that, when
executed, will produce a result after “terminating” within a finite
no of steps
Algorithms (2)
• Well-defined and effective
• No ambiguity, a method must exist
• Good Examples:
Add 1 to x
compute largest prime no. < 100
compute square root of x to 4 decimal places
• Bad examples:
divide 10 by x
compute largest prime
compute square root of x
Algorithms (3)
• Always terminate, and be sure about it
• Produce correct results
– this may require some hard thinking
– testing helps, but is not adequate
Algorithm verses Program
• Basic differences are:
– Program is written in programming language
whereas algorithm is in English like pseudo
language.
– Program may be non terminating (OS) whereas
algorithm should terminate in finite steps.
• Study of algorithm can be
classified in four distinct areas
namely how to
– devise
– express
– validate
– analyze algorithms
• Devising good algorithm:
– Requires study of various design techniques
– Top down, bottom up and
object oriented
approaches
• Expressing an algorithm:
– Good algorithms are expressed using principle of
structured programming
• Validation of algorithm:
– It should be validated for correctness of all possible
legal inputs.
• (correct, incorrect, exceptions)
– Note that algorithm need not yet be expressed as a
computer program.
• Analysis of an algorithm:
– Study of behavior pattern or performance
profile.
– It can be calculated in terms of computing
time and space requirement in the
machine.
• Time Complexity: Running time of the
program as a function of the size of input.
• Space Complexity: Amount of computer
memory required during the program
execution.
Top Down Design Model
• In top-down model, an overview of the
system is formulated, without going into
detail for any part of it.
• Each part of the system is then refined in
more details.
• Each new part may then be refined again,
defining it in yet more details until the entire
specification is detailed enough to validate
the model.
Top Down Concept in Problem Solving
• This design model can also be applied while
developing algorithm.
• It basically refers to successive refinement
of the problem (task) into sub problems
(subtasks).
• Refinement is applied until we reach to the
stage where the subtasks can be directly
carried out.
Top Down Design
Main Task
subtask1
subtask2
subtask3
Bottom-up Design
• In bottom-up design individual parts of the
system are specified in details.
• The parts are then linked together to form
larger components, which are in turn linked
until a complete system is formed.
• Object-oriented languages such as C++ or
JAVA use bottom-up approach where each
object is identified first.
Bottom up Design
Structured Programming (SP)
• It is a technique using which one can write
algorithms (programs) in top down fashion.
• This technique should be used with every
level of refinement.
• In SP, one entry and one exit principle is
adopted in all the constructs.
• Basically there are three structures in SP
Three types of control flow in SP
Sequential (sequence)
entry
T1
T2
exit
Selection (test)
• If cond then task1
Y
Cond
N
task1
• If cond then task1 else task2
task2
N
Cond
Y
task1
Repetition
• While
While (cond) do
endwhile
• Do while /Repeat
Do
while (cond)
Ordered Sequences in Algorithms
• Ordered sequence
T1
– T1, T2, T3
T2
T3
T1
– T1, repeat 5 times T2, T3
• same as T1, T2, T2, T2, T2, T2, T3
T2
5 times
T3
Ordered Sequences in Algorithms (2)
– T1, T2, “if C = true, go to second T2”, T3
• resulting sequence is
T1, T2, T2,...until C is true,T3
T1
T2
Y
C
T3
Ordered Sequences in Algorithms (3)
• What is the difference between:
S1
S2
Y
S1
C
C
S2
S3
S3
Y
Algorithms: an example
Computation of income tax
• Given a tax table as below, compute the tax, T, on an income, X.
INCOME
0 <= INC <=100000
100001 <= INC <=200000
200001 <= INC <=300000
300001 <= INC
• Here is an algorithm:
Step 1: Input INC;
Step 2: Compute tax, T;
Step 3: Output T
TAX
0
0 + 0.10*(INC-100000)
10000 + 0.20*(INC-200000)
30000 + 0.30*(INC-300000)
Algorithms: (refined)
Step 1: Input INC;
Step 2a: if INC > 300000
then T  30000 + 0.30*(INC-300000);
Step 2b: if INC > 200000 and INC  300000
then T  10000 + 0.20*(INC-200000);
Step 2c: if INC > 100000 and INC  200000
then T  0 + 0.10*(INC-100000);
Step 2d: if INC  100000 then T  0;
Step 3: Output T
Algorithms: another example
• Table look-up: Consider:
– special key K,
– length of list, 5 or N, more generally
– unsorted L = [(x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5)], where xi is
key and yi is corresponding output value
• Problem is to search whether key K is in the list and output
relevant pair if it exist
• Here is an algorithm:
Step 1: Input all data, K, and list L;
Step 2: Search for K in L;
Step 3: Output results
Table look-up algorithms (contd.)
• Here is refined version of the algorithm:
Input K;
Input (x1, y1);
Input (x2, y2);
Input (x3, y3);
Input (x4, y4);
Input (x5, y5);
If K = x1 then output (x1, y1);
If K = x2 then output (x2, y2);
If K = x3 then output (x3, y3);
If K = x4 then output (x4, y4);
If K = x5 then output (x5, y5)
Table look-up algorithms (contd.)
• This algorithm does the same thing, except that it is
compact:
Input K;
Repeat these operations 5 times:
[Input (x, y);
If K = x then output (x, y)
]
Table look-up algorithms (contd.)
• An even better algorithm:
Input n;
If n > 0 then
[Input K;
Repeat these operations n times:
[Input (x, y);
If K = x then output (x, y)
]
]
Visualizing Algorithms
• Use flowcharts to visualize the working of an algorithm
• For example:
start
S1
S2
true
C
false
S3
stop
Problem 1:
Find the roots of a quadratic equation of the form
a*x2 + b*x + c = 0
Formula:
x1 = [(-b) + (b2 – 4ac)]/2a
x2 = [(-b) - (b2 – 4ac)]/2a
Algorithm:
input a, b, c;
d = b*b – 4*a*c;
if (d ≥ 0) then
e = sqrt(d);
{
r1 = (-b + e) / (2*a);
r2 = (-b - e) / (2*a);
i1=0; i2 =0
}
else
e = sqrt(-d);
{
r1 = (-b) / (2*a);
r2 = r1;
i1= e / (2*a); i2 = -e/(2*a);
}
output r1,r2,i1,i2
Problem 2: Write algorithm to find factorial of n
Formula:
fact = n * n-1 * n-2 * …* 2 * 1
Algorithm:
input n;
i = 0; initialization
fact = 1;
looping
while (i < n)
{
i = i + 1;
fact = i * fact;
}
output fact
Execution
Let n = 4;
i = 0; fact = 1;
while loop
0 < 4; i = i +1=1; fact = 1 * 1;
1 < 4; i = i +1=2; fact = 1 * 2;
2 < 4; i = i +1=3; fact = 2 * 3;
3 < 4; i = i +1=4; fact = 6 * 4
4 < 4 is false so
exit the while loop;
output fact = 24
Problem 4: Reversing integer digits (3542  2453)
Algorithm:
input num;
rev = 0;
while (num > 0)
{ rev = rev*10 +
num mod 10;
num = num div 10;
}
output rev
Execution
num = 245;
rev = 0;
while loop
rev = 0 * 10 + 5 = 5;
num = 24;
rev = 5 * 10 + 4 = 54;
num = 2;
rev = 54 * 10 + 2 = 542;
num = 0;
exit while
output rev as 542
Problem5: Find maximum out of 100 numbers (+ve
integers)
Algorithm: Here number is read in the loop. At the end
of loop, read elements are not available.
max = 0;
i = 0;
while (i < 100)
{
i = i+1;
input num;
if (num > max) then max = num;
}
output max
• Array int numbers [5];
Input all 100 numbers in an array (vector). Then compute
maximum out of 100 numbers. At the end of loop we have all the
numbers.
Algorithm:
input num(i), i = 1,100;
max = 0;
i = 0;
while (i < 100)
{
i = i+1;
if (num(i) > max) then
max = num(i);
}
output max, num(i), i = 1,100
Execution
Let numbers are: 4, 2,7,1
max = 0; i = 0;
while loop
i = 1; 4 > max so max = 4;
i = 2; 2 < max so no change
i = 3; 7 > max so max = 7;
i = 4; 1 < max so no change
exit of while
output max as 7 and numbers
as 4, 2, 7, 1
Download