CSL101: Introduction to Computers and Programming: Lecture 1: Introduction Instructors Prof Saroj Kaushik Room No : Phone Number : Email : Web Link : 416, Bharati School building (CSE) 1292 (internal) saroj@ cse.iitd.ernet.in http://www.cse.iitd.ac.in/~saroj Prof K K Biswas Room No Phone Number Email Web Link 313, Bharati School building (CSE) 6010 (internal) [email protected] http://www.cse.iitd.ac.in/~kkb : : : : 1st semester 2011-2012: Time Table 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 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 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 Submission of lab reports • Labs will be conducted by TAs in CSC. • Lecture slides, Lab exercises will be posted on web or Moodle http://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 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. 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 Introduction to Computers 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 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. Devices that comprise a computer system Monitor (output) Speaker (output) System unit (processor, memory…) Printer (output) Storage devices (CD-RW, Floppy, Hard disk, zip,…) Scanner (input) Mouse (input) Keyboard (input) 11 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. 12 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. 13 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. Hardware Components Of A Computer ? • Input devices. • Output devices. • Central Processing Unit • Memory. • Storage devices. 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. 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. 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. 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. 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. 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) 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 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. Read only Memory - ROM • 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. • Cache is sometimes called CPU cache • Transfer from cache to CPU is very fast as compared to from main memory to CPU. CPU Main Memory ALU + CU Cache 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. 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) 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. Reasons for having two types of storage 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 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. Contd… • How many distinct values can be stored in one byte? • Answer: 28 • Minimum Value: 0 0000 0000 • Maximum value: 28–1 1111 1111 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: • 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. 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. Table of units of measurement Name Equivalent Number of Bytes power of 2 byte 8 bits 1 20 Kilobyte (KB) Megabyte (MB) Gigabyte (GB) Terabyte 1024 bytes 1024 210 1024 KB 1,048,576 220 1024 MB 1,073,741,824 230 1024 GB 1,099,511,627,776 240 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. 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. 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. Example of Types of Software Application Software Operating system Software 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. 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.) 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. • 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 Programming Languages Machine Lang Assembly Lang High level Lang Consists of 0 & 1 Comp System directly executes it Pseudo inst Add 2 5 Assembler translates to M/L English like insts Input A, B 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 Machine Language • Consider machine of 16-bits Opcode (4-bits) Address (12-bits) • There are 16 possible opcodes (Hypothetical example) Opcode Action 0000 Load accumulator (ACC)from memory 0001 Store accumulator in memory 0010 Add the contents of Acc with specified location and leave the result in ACC 1111 stop M/L program Instructions Machine language • Load ACC from Loc 10 • Add ACC with contents of Loc 20 • Store ACC contents in Loc 30 • Stop • 0000 0000 0000 1010 • 0010 0000 0001 1010 • 0001 0000 0001 1110 • 1111 0000 0000 0000 Assembly Language High level Language (pascal, C, C++, Java) • • • • • • • • • Load Add Store Print Stop X Y Z Z 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 • 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. 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. 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 F C = 5 * (F – 32 ) / 9 print C end 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. • 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. 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: • 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.