CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj...

54
CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik 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 Prof K K Biswas Room No : 313, Bharati School building (CSE) Phone Number : 6010 (internal) Email : [email protected] Web Link : http://www.cse.iitd.ac.in/~kkb

Transcript of CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj...

Page 1: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

CSL101: Introduction to Computers and Programming: Lecture 1: Introduction

Instructors

Prof Saroj Kaushik Room No : 416, Bharati School building (CSE)Phone Number : 1292 (internal)Email : saroj@ cse.iitd.ernet.inWeb Link : http://www.cse.iitd.ac.in/~saroj

Prof K K Biswas Room No : 313, Bharati School building (CSE)Phone Number : 6010 (internal)Email : [email protected] Link : http://www.cse.iitd.ac.in/~kkb

Page 2: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Lecture Time: 2.00-3.20 PM

Lect Venue : IV LT3

Practical: 11.00-12.50 PM (CSC): 2 hr/week lab for each group

Cycle 1 : G1, 6 - Wednesday

Cycle 2 : G2, 7 - Thursday

Cycle 3 : G3, 8 - Friday

Cycle 4 : G4, 9 - Monday

Cycle 5 : G5, 10 -Tuesday

1st semester 2011-2012: Time Table

Page 3: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Course contents• Introduction to Computers• Problem solving using computers • Development of algorithms?• Introduction to programming in a high level

language: Python– Syntax and semantics

• Algorithms for various applications• Advanced features of Python• Number Representations• Sorting algorithms

Page 4: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Course Grading Structure

• These weights are indicative, and may change as semester progresses– Minor 1 & 2, each with 15-20% weightage– Major, 30-35% weightage– Quizzes and home assignments, 5-10%

weightage– Programming exercises, 20-25% weightage

Page 5: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Submission of lab reports• Labs will be conducted by TAs in CSC.• Lecture slides, Lab exercises will be posted on web

or Moodlehttp://www.cse.iitd.ac.in/~saroj

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

• Heavy penalty for late submission

Page 6: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

To pass the course • A student has to pass EACH of the course components

indicated above, by getting at least 30% in each.

• Student should get minimum 35% overall to pass the course.

• Attend all classes and should be above 75%.

• Classes will begin on time and entry will not be allowed later than 5 minutes.

• Cheating in tests or assignments will result in ‘F’ grade in the course.

• Attend the “Special Help sessions” if you have difficulty in the course.

• Keep meeting your TA ( Teaching Assistant) for any difficulty in the course outside the lab timings as well.

Page 7: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Text books

• For Algorithm:– R. G. Dromey “How to solve it by computer”,

Prentice Hall, Indian edition.

• Link to an online material on Python Programming:

www.greenteapress.com/thinkpython/thinkpython.pdf

Page 8: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Introduction to Computers

Page 9: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

What is a Computer?

• Electronic device

• 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

Page 10: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

What Does A Computer Do?

• Computers can perform four general operations, which comprise the information processing cycle.– Input– Process– Output– Storage

• To do each of these computer has devices.

Page 11: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

11

Devices that comprise a computer system

Printer (output)

Monitor (output)

Speaker (output)

Scanner (input)

Mouse (input)

Keyboard (input)

System unit(processor, memory…)

Storage devices

(CD-RW, Floppy, Hard disk, zip,…)

Page 12: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

12

Why Is a Computer So Powerful?

• The ability to perform the information processing cycle with amazing speed.

• Reliability (low failure rate).• Accuracy.• Ability to store huge amounts of data and

information.• Ability to communicate with other

computers.

Page 13: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

13

How Does a Computer Know what to do?

• It must be given a detailed list of instructions, called a computer program or software, that tells what exactly it has to do.

• Before processing a specific job, the computer program corresponding to that job must be created and stored in memory.

• Once the program is stored in memory the computer can start the operation by executing the program instructions one after the other.

Page 14: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Computer System

• A computer system is made up of both – Hardware electronic components

– Software Program consisting of set of instruction

• Example: Some of the Anology:– Mobile is hardware and corresponding

facilities available is software.– CD is hardware and music on it is software.

Page 15: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Hardware Components Of A Computer ?

• Input devices.• Output devices.• Central Processing

Unit• Memory.• Storage devices.

Page 16: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

The Keyboard

• The most commonly used input device is the keyboard on which data is entered by manually keying in or typing certain keys.

• A keyboard typically has 101 or 105 keys.

Page 17: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

The Mouse

• Is a pointing device which is used to control the movement of a mouse pointer on the screen to make selections from the screen.

• A mouse has one to five buttons. The bottom of the mouse is flat and contains a mechanism that detects movement of the mouse.

Page 18: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Output Devices

• Output devices make the information resulting from the processing available for use.

• The two output devices more commonly used are the printer and the computer screen.

• The printer produces a hard copy of your output, and the computer screen produces a soft copy of your output.

Page 19: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

The Central processing Unit, CPU

• The CPU contains electronic circuits that cause processing to occur.

• Does the `work‘ of fetching, storing and manipulating values

that are stored in the computers memory. • It is considered the “brain” of the computer.

Page 20: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Main Components of CPU

• Arithmetic Logic Unit (ALU)– The ALU carries out arithmetic operations (e.g.

addition and subtraction) and logical operations (e.g. and, or, not)

• Control Unit (CU)– The CU controls the execution of instructions.

Page 21: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Memory

• Memory consists of electronic components that store data including numbers, characters, graphics and sound.

• It has two types of memory.– Random Access Memory (RAM)– Read only Memory (ROM)

Page 22: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Random Access Memory – RAM

• RAM is where programs and data are kept during execution when the processor is actively using them.

• RAM is called Main memory or Primary memory.

• RAM

– Is Volatile - temporary

– Has random access to any memory location

– Read & write access

– Program + data reside in this memory

Page 23: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Main Memory

• Main memory is: – very closely connected to the processor. – the contents are quickly and easily changed. – holds the programs and data that the processor

is actively working with.

– interacts with the processor millions of times per second.

Page 24: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Read only Memory - ROM

• Read only Memory (ROM)– Non volatile

– Only read access

– Permanent programs reside

Page 25: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Cache Memory

• A special and very high speed memory used to increase the speed of processing.

• Cache is sometimes called CPU cache• Transfer from cache to CPU is very fast as

compared to from main memory to CPU.

Main Memory

CPU

ALU + CU

Cache

Page 26: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Cache Memory

• It lies between CPU and main memory.• The cache is usually filled from main memory

when instructions or data are fetched into the CPU.• Often the main memory will supply a wider data

word to the cache than the CPU requires, to fill the cache more rapidly.

• It is expensive and usually small in size.• Stores only segments of programs currently being

executed by CPU.• It is also sometimes called buffer.

Page 27: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Storage Devices- Secondary memory

• Connected to main memory

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

• The processor only occasionally interacts with secondary memory.

• The most common types of secondary storage used are

– Hard disks

– CD-ROM drives

– Flash Drives (USB, pen drives)

Page 28: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Hard Disk

• The hard disk has enormous storage capacity compared to main memory– 300 times the amount of storage in

main memory

• The hard disk is usually contained in the systems unit of a computer.

Page 29: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Reasons for having two types of storage

Primary memory

1. Fast

2. Expensive

3. Low capacity

4. Connects directly to the processor

Secondary memory

1. Slow

2. Cheap

3. Large capacity

4. Not connected directly to the processor

Page 30: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Data Representation in Memory

• Data is stored inside the memory in the form of 1’s and 0s, Bits (Binary Digits) as shown below (ON/OFF positions). – For example

1100 0111

0011 1111

0101 1110

• One byte consists of 8 bits.

Page 31: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Contd…

• How many distinct values can be stored in one byte?

• Answer: 28

• Minimum Value:

0 0000 0000

• Maximum value:

28–1 1111 1111

Page 32: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Organization of Main Memory • Main memory consists of a very long list of bytes. • In most modern computers, each byte has an address that

is used to locate it. • The picture shows a small part of main memory:

Page 33: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

• Each row is called memory location that is a single byte and has an address.

• The addresses are the integers to the left of the boxes: 0, 1, 2, 3, 4, ... 255 for location of one byte.

• The addresses for most computer memory start at 0 and go up in sequence until each byte has an address.

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

• When people say that a computer has "128 MB of RAM" they are talking about how big its main memory is.

Page 34: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Units of Memory

• 1 KB (1 KiloByte) contains 1024 Bytes. • 1 MB (1 MegaByte) consists of 1024 KB.• 1 GB (1 GigaByte) has 1024 MB.• 1 TB (1 Terabyte consists of 1024 GB.

Page 35: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Table of units of measurementName Equivalent Number of Bytes power of 2

byte 8 bits 1 20

Kilobyte (KB)

1024 bytes 1024 210

Megabyte (MB)

1024 KB 1,048,576 220

Gigabyte (GB)

1024 MB 1,073,741,824 230

Terabyte 1024 GB 1,099,511,627,776 240

Page 36: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Software

• Computer programmers/users write the codes/ instructions that make-up software applications/programs.

• Set of instructions is also referred to as lines of code.

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

Page 37: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Types of Programs

• There are two categories of programs. – Application programs (usually called just

"applications") that people use to get their work done. – Systems programs keep all the hardware and software

running together smoothly.

• The difference between "application program" and "system program" is fuzzy.

• Often it is more a matter of marketing than of logic.

Page 38: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Application Programs

• Word processors• Game programs• Spreadsheets • Data base packages• Graphics programs • Web browsers

Systems Programs

• Operating system. • Networking system. • Database system. • Compilers • Web site server. • Data backup.

Page 39: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Example of Types of Software

Application Software

Operating system Software

Page 40: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Operating Systems (OS)

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

• When an application program is running, the operating system manages the details of the hardware for it.

• It is there in the background managing resources, doing input and output for the application, and keeping everything else running.

• The operating system is always present when the computer is running.

Page 41: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Resources managed by OS• OS 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.)

Page 42: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Various Modules of OS• 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.

Page 43: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

• 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

Page 44: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Programming Languages

Machine Lang Assembly Lang High level Lang

Consists of 0 & 1 Pseudo inst

Add 2 5

English like insts

Input A, B

Comp System directly executes it

Assembler translates to M/L

Compiler translates to M/L

Difficult to write Simple to write Simpler to write

Highly error prone Less error prone Lesser error prone

Machine dependent Machine dependent Machine independent

Page 45: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Machine Language• Consider machine of 16-bits

Opcode (4-bits) Address (12-bits)

• There are 16 possible opcodes (Hypothetical example)Opcode Action0000 Load accumulator (ACC)from memory0001 Store accumulator in memory0010 Add the contents of Acc with specified

location and leave the result in ACC

1111 stop

Page 46: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

M/L program

Instructions

• Load ACC from Loc 10

• Add ACC with contents of Loc 20

• Store ACC contents in Loc 30

• Stop

Machine language

• 0000 0000 0000 1010

• 0010 0000 0001 1010

• 0001 0000 0001 1110

• 1111 0000 0000 0000

Page 47: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Assembly Language

• Load X• Add Y• Store Z• Print Z• Stop

High level Language

(pascal, C, C++, Java)

• Input X, Y• Z = X+Y• Output Z• Stop

• One assembly instruction is equivalent to machine instruction

• One high level inst corresponds to many machine instruction

Page 48: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

• All CPUs have an instruction set (or language) that they understand.

• Eventually all assembly level / high level programs must be translated (or compiled) into instructions from this set.

• Roughly speaking, all processors have the same sort of instructions available to them.

• An executable program is machine language program that runs on a CPU and is always represented as a series of binary digits.

• This is achieved by compiling (translating) a high-level program with a special piece of software called a compiler.

• Compilers are incredibly complicated programs that accept other programs as input and generate a binary executable object file as output.

Page 49: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Types of Files• As far as the hard disk is concerned, all files are the same and

are named collections of bytes. Of course, what the files are used for is different.

• The OS can take a – program file, copy it into main memory, and start it running. – data file, and supply its information to a running program when it asks.

• Often then last part of a file's name (the extension) shows what the file is expected to be used for. For example, – "mydata.txt" the ".txt" means that the file is expected to be used as a

collection of text, that is, characters.– “netscape.exe" the ".exe" means that the file is an "executable," that is,

a program that is ready to run. – "program1.java" the ".java" means that the file is a source program in

the language java (there will be more about source programs later on in these notes.)

– To the hard disk, each of these files is the same sort of thing: a collection of bytes.

Page 50: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Example Problem :

Consider the problem of converting F (Fahrenheit) to C(Centigrade) which is suitable for solution by computer.

Formula: C = 5 * (F – 32) / 9• Set of possible high level instructions:Input : F (given parameters)Output: C (output parameters)

read FC = 5 * (F – 32 ) / 9print Cend

Page 51: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Compiler• A source program is set of instructions written in a high level

language.. • It is translated into a machine language program by a system

software called compiler.• It takes a source file as input and produces an executable

program (machine language program) as output.

Page 52: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

• The source program is stored in a file created using a text editor.

• The source file is kept on the hard disk. • When you have to run your program (Source program), first

compile by compiler and form an executable file.• The source file remains unchanged; a new executable file is

created. • The executable file is also kept on hard disk. • Compilers are specific to high level languages (like "C") and a

specific to processor type (like "Pentium"), and only runs under a specific operating system (like "Windows".)

• The above is what goes on with most languages: Ada, Pascal, C, C++, FORTRAN and others.

• Java adds a few more steps, which will be discussed later.

Page 53: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

Interpreter

• There are some language which are not compiled but interpreted by interpreter (BASIC, SML, Python …).

• An interpreter is a program that acts like a processor that can directly execute a high level language.

• This is a fairly complicated thought. The figure might help:

Page 54: CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No: 416, Bharati School building (CSE)

• Here the source program "program.bas" has been written in

BASIC (a programming language) by a programmer with a text editor.

• It is being interpreted by the BASIC interpreter, which is running on the processor.

• The BASIC interpreter will read each command in the source program and do what it says.

• When an interpreter is running a BASIC source program, both the interpreter and the source program are in main memory.

• The interpreter consists of machine instructions that the hardware can execute directly.

• Translator:   takes a complete document in one language and creates a complete document in another language, which can then be used at any time.

• Interpreter:   acts as an intermediate between source language and machine language and converts instruction wise.