Introduction to Computer Science Ariel Shamir, Inter Disciplinary Center Herzliya Israel, Fall 2007 10 October 2007 © Ariel Shamir 1 Course Overview • Introduce basic terms and concepts •Introduce of computer science • Acquire basic programming skills •Acquire • Understand general principles in CS •Understand • Learn a modern object oriented •Learn programming language 10 October 2007 © Ariel Shamir 2 1 Course Methodology Lectures Reading & books Recitations Programming exercises & tutors The course site & the web Support Structures FOR YOU! 10 October 2007 © Ariel Shamir 3 The Course Web Site Administration & regulation Information & downloads Exercises (receive & submit) Newsgroup!!! Go to: www1.idc.ac.il/coursesftp/cs/intro1/ www1.idc.ac.il/coursesftp/cs/intro1/ 10 October 2007 © Ariel Shamir 4 2 Information Source Possible course books: •• ““Java: Java: and ”, W. Programming Savitch and introduction introduction to to CS CS & & Programming” Programming”, W. Savitch. Savitch.. •• "Java ”, Design "Java Software Software Solutions Solutions -- Foundations Foundations of of Program Program Design” Design”, John John Lewis Lewis & & William William Loftus. Loftus. •• "The “, Mary Tutorial Campione Walrath "The Java Java Tutorial“ Tutorial“, Mary Campione, Campione,, Kathy Kathy Walrath. Walrath.. •• "The “, Ken Language "The Java Java Programming Programming Language“ Language“, Ken Arnold, Arnold, James James Gosling. Gosling. •• More …(check the books More books… books…(check the site!): site!): Go to www1.idc.ac.il/coursesftp/cs/intro1/ www1.idc.ac.il/coursesftp/cs/intro1/ Literature.htm Literature.htm 10 October 2007 © Ariel Shamir 5 Lecture Overview 1. What is a computer? 2. How is a typical computer built 3. Algorithms 4. Programming 5. Introducing Java 10 October 2007 © Ariel Shamir 6 3 Why Use Computers? Computers are very good at: • • processing processing data data in in aa short short time, time, • • storing storing and and retrieving retrieving large large bulks bulks of of data, data, • • transferring transferring large large bulks bulks of of data data across across sites sites Things that people are bad at, or need help at or simply hate to do. 10 October 2007 © Ariel Shamir 7 What Is a Computer? Computers Computers are are computational computational machines machines that that can … tasks can carry carry out out given given tasks… tasks… (???) (???) Computational Computational machines: machines: hardware hardware Given Given tasks: tasks: software software 10 October 2007 © Ariel Shamir 8 4 “Computational Machine” Storage Computation Network Do it faster, and do it with larger amounts of data. The main parameters are therefore Time and space! space! 10 October 2007 © Ariel Shamir 9 Time & Space! 10 October 2007 © Ariel Shamir 10 5 The Machine (Hardware) Storage Memory Disks Computation CPU Network I/O Network Other peripherals: Video, Audio… 10 October 2007 © Ariel Shamir 11 John Von Neumann 1945: 1945: John John von von Neumann Neumann wrote wrote "First "First Draft Draft of of aa Report Report on on the the EDVAC" EDVAC" in in which which he he outlined outlined the the architecture architecture of of aa stored-program stored stored-program computer. computer. http://inventors.about.com/library/blcoindex.htm 10 October 2007 © Ariel Shamir 12 6 Where We Were The The ENIAC ENIAC contained contained 17,468 17,468 vacuum vacuum tubes, tubes, along along with with 70,000 70,000 resistors, resistors, 10,000 10,000 capacitors, capacitors, 1,500 1,500 relays, relays, 6,000 6,000 manual manual switches switches and and 55 million million soldered soldered joints. joints. It It covered covered 1800 1800 square square feet feet (167 (167 square square meters) meters) of of floor floor space, space, weighed weighed 30 30 tons, tons, consumed consumed 160 160 kilowatts kilowatts of of electrical electrical power. power. In In one one second, second, the the ENIAC ENIAC could could perform perform 5,000 5,000 additions, additions, 357 357 multiplications multiplications or or 38 38 divisions. divisions. 10 October 2007 © Ariel Shamir 13 Today… Blue Gene (LLNL) is the world's fastest computer, at 360 TFLOPS (1000000000000 12 FLOPS): = 1012 Cluster of nodes where up to 1024 Compute nodes plus additional IO nodes in a standard 19" cabinet 10 October 2007 © Ariel Shamir 14 7 Von - Newman Architecture Long term storage I/O devices 10 October 2007 © Ariel Shamir 15 Opening the Hood of Your PC Peripherials Monitor CPU Chip Motherboard 10 October 2007 Memory Chips © Ariel Shamir 16 8 Computer Architecture • The CPU and main memory are the two •The key hardware components • All other devices are considered •All peripherals. Controllers coordinate the activities of specific peripherals • Binary information moves between •Binary devices across a group of wires called a bus 10 October 2007 © Ariel Shamir 17 The Bus Main memory CPU Bus Video controller Monitor 10 October 2007 Disk controller Hard disk Floppy disk © Ariel Shamir Controller other peripherals 18 9 Memory Hierarchy The computer memory divided into: •• Main Main memory. memory. •• Secondary ). tapes Secondary memory memory (disks, (disks, flash, flash, tapes). tapes). Main memory is volatile - stored information is lost if the electric power is removed, but it is FAST! Secondary memory devices are nonvolatile, but generally much slower. 10 October 2007 © Ariel Shamir 19 Storage Capacity Units Every memory device has a storage capacity, indicating the number of bytes it can hold. Capacity unit Symbol Actual size kilobyte KB 210 = 1024 megabyte MB 220 = 1048576 gigabyte GB 230 (over 1 billion) terabyte TB 240 (over 1 trillion) 10 October 2007 © Ariel Shamir 20 10 Main Memory ••The The main main memory memory is is divided divided into into memory memory locations locations called called cells. cells. ••Each Each memory memory location location has has an an address address which which uniquely uniquely identifies identifies it. it. ••Data Data stored stored in in the the main main memory memory can can be be stored stored in in one one or or more more consecutive consecutive memory memory locations. locations. •• It It is is termed termed RAM RAM –– Random Random Access, Access, as as access access to to any any random random address address takes takes (approximately) (approximately) the the same …) tapes same (compare (compare for for instance instance to to tapes… tapes…) 10 October 2007 © Ariel Shamir 21 Main Memory Cells 3456 3457 01101010 Memory location - cell (usually holds 8 bits, or 1 byte) 3458 3459 3500 3501 Each memory cell has a unique numeric address which identifies it 10 October 2007 © Ariel Shamir 22 11 Main Memory Cells 3456 3457 3458 3459 01101010 3500 10110001 3501 The address of such values will be the address of the first byte of the cells holding the value 10 October 2007 Values larger than 8 bits are stored in consecutive cells © Ariel Shamir 23 The Central Processing Unit • A central processing unit (CPU) is •A also called a microprocessor • The CPU is a single "chip" on the •The order of one square inch in size • It is the brain of the computer •It • The job of the CPU is to execute •The programs: it continuously follows the fetch-decodefetch decode-execute cycle fetch-decode-execute 10 October 2007 © Ariel Shamir 24 12 Inside the CPU Arithmetic / Logic Unit Coordinates processing steps Control Unit Small memory units capable of storing information Registers 10 October 2007 Performs calculations and decisions © Ariel Shamir 25 Machine Execution? How does a piece of physical hardware machine carry out instructions? 10 October 2007 © Ariel Shamir 26 13 Executing a Program • Programs are represented digitally in •Programs machine language as a list of numbers. • Each number represents either the code of •Each an instruction or some data. • The program is loaded from disk. •The • The CPU fetches the instructions from •The memory one after another and executes them (execution cycle). 10 October 2007 © Ariel Shamir 27 Machine Language Each computer has a limited set of machine language instructions The instructions are very elementary •• Add Add two two integers integers •• Test Test if if aa given given integer integer is is zero zero The strength of computers is due to their capability of executing millions of simple instructions per second 10 October 2007 © Ariel Shamir 28 14 Machine Code Instructions The machine code is a sequence of numbers representing •• Instruction Instruction codes codes •• Data Data values values …… 0011 0011 92FD 92FD 0007 0007 0014 0014 …… …… LOAD LOAD 92FD 92FD ADD ADD 20 20 …… ““Load Load whatever is in address 92FD in memory and add 20 to it” it” 10 October 2007 © Ariel Shamir 29 Program Loading & Fetching 01001011 10111001 01010011 11001100 Load 01001011 10111001 01010011 11001100 Fetch 01110 CPU Disk Memory 10 October 2007 © Ariel Shamir 30 15 Fetch-decode-execute Cycle Retrieve an instruction from main memory fetch execute decode Determine what the instruction is Carry out the instruction 10 October 2007 © Ariel Shamir 31 Execution Cycle Fetch D3 …5F 88 D3 04 01 F7… Decode D3 = ADD Execute 10 October 2007 © Ariel Shamir 32 16 Execution Cycle …5F 88 D3 04 01 F7… Fetch D3 Decode D3 = ADD Execute 4 10 October 2007 © Ariel Shamir 33 Execution Cycle Fetch D3 …5F 88 D3 04 01 F7… Decode D3 = ADD Execute 4+1 10 October 2007 © Ariel Shamir 34 17 Execution Cycle Fetch D3 …5F 88 D3 04 01 F7… Decode D3 = ADD Execute 4+1=5 10 October 2007 © Ariel Shamir 35 Execution Cycle Cont. Fetch F7 …5F 88 D3 04 01 F7… Decode Execute 10 October 2007 © Ariel Shamir 36 18 CPU Control • The speed of a CPU (i.e. the fetch-decodefetch decode•The fetch-decodeexecute cycle) is controlled by the system clock • The system clock generates an electronic •The pulse at regular intervals which coordinate the activities of the CPU • The speed is measured in megahertz •The ((MHz) MHz) or Gigahertz (GHz) 10 October 2007 © Ariel Shamir 37 CPU & Memory Connection The main memory stores not only data but also the programs! The CPU continuously retrieves information from the memory in the execution cycle! The CPU – memory connection is the bottleneck of computation! 10 October 2007 © Ariel Shamir 38 19 “Given Tasks” - Software Computers do only what we tell them to – they are deterministic machines! Inputs What is a task? A process that gets inputs related to the task, and outputs data which represent the result of executing the task. Process Outputs 10 October 2007 © Ariel Shamir 39 Task Examples Computing Computing taxes taxes for for employees employees of of aa company company •• Inputs: Inputs: textual textual and and numerical numerical data data representing representing information information about about the the employees employees •• Output: Output: formatted formatted textual textual tax tax reports reports F15 F15 flight flight simulator simulator •• Inputs: Inputs: joystick joystick signals signals and and key key strokes strokes from from the the user user •• Output: ’s view pilot Output: image image data data representing representing the the pilot’ pilot’s view Search Search engine engine •• Input: Input: keywords keywords •• Output: Output: locations locations in in internet internet where where keywords keywords found found 10 October 2007 © Ariel Shamir 40 20 Algorithms – the “How” An algorithm is a procedure for executing a given task. Inputs An algorithm consist of a list of instructions. 10 October 2007 Algorithm Outputs © Ariel Shamir 41 Al-Khowarizmi Born in Uzbekistan (780AD). Worked as a mathematician in Baghdad (considered as founder of modern algebra). He believed that any math problem, no matter how difficult it may be, could be solved if broken into smaller steps and logically derive the correct answer! 10 October 2007 © Ariel Shamir 42 21 Real Life Example An algorithm for fixing a flat tier: Take Take out out the the screws screws of of the the tier tier Lift Lift the the car car on on aa jack jack Flat tier car Remove Remove the the flat flat tier tier Mount Mount the the reserve reserve wheel wheel Algorithm Tighten Tighten back back the the screws screws Lower Lower back back the the car car Drivable car Fasten Fasten the the screws screws 10 October 2007 © Ariel Shamir 43 Mathematical Algorithm Algorithm: Algorithm: For For all all integers integers ii such such that that 1<i<n 1<i<n A number n IfIf ii divides divides nn Output Output false false Algorithm Output Output true true What does this algorithms do? 10 October 2007 True or False © Ariel Shamir 44 22 Measuring Algorithms: Complexity • The efficiency of the algorithm is •The often called it’ ’s complexity it it’s • In general terms: •In Complexity Complexity = = cost cost in in time time and and space space • Usually computed as a function of •Usually the size of the input 10 October 2007 © Ariel Shamir 45 Example: Searching Find a phone number in a 1000 number phone book. •• Linear Linear search: search: could could take take 1000 1000 checks! checks! •• Binary Binary search: search: 1000,500,250,125,63,32,16,8,4,2,1, 1000,500,250,125,63,32,16,8,4,2,1, mostly mostly 10 10 steps! steps! How many jumps in the general case? 10 October 2007 © Ariel Shamir 46 23 Algorithms in CS • • Is Is there there an an algorithm algorithm for for every every task? task? • • How How can can we we know know if if an an algorithm algorithm is is correct? correct? • • What What is is the the time/space time/space complexity? complexity? • • What What is is the the best best algorithm algorithm for for aa given given task? task? • • What What is is the the best best way way to to express express an an algorithm? algorithm? 10 October 2007 © Ariel Shamir 47 From Algorithm to Execution Cycle Algorithm/Model Execution Cycle Hardware 10 October 2007 © Ariel Shamir 48 24 Computer Programs • An algorithm is described to the •An computer as a computer program. program. • A computer program is simply a list •A of unambiguous instructions meant to be followed by a computer. • The operations sequence has to be •The correct or the program will fail. 10 October 2007 © Ariel Shamir 49 Program Compilation • A program written in any high level •A computer language (java, C++, basic) must be translated into the machine language in order to be executed. • The translation process is called •The Compilation (or Assembly) 10 October 2007 © Ariel Shamir 50 25 Summary Algorithm/Model Java Compilation Next few lectures Machine Code Execution Cycle Hardware 10 October 2007 © Ariel Shamir 51 26
© Copyright 2026 Paperzz