1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12...

49
1 בבבב בבבבב בבבבב בבבב בבבבב בבבבב בבבבבבבבבב, בבבבבבבבבב,67117 67117 יייי ייי יייייי14-12 יייי: ייי"י יייי ייייי ייי יייי: ייייי15-14 ( 6298-8 , ייי) 211 ייייייי: יייי ייי, ייי ייייhttp://www. cs . huji .ac. il /~ introcsm בבב:

Post on 20-Dec-2015

239 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

1

מבוא למדעי המחשב למתמטיקאים, מבוא למדעי המחשב למתמטיקאים, 6711767117

14-12ימים שני ושלישי מרצה: פרו"פ דפנה וינשל

(, רוס 6298-8 )15-14שעת קבלה: שלישי 211

מתרגלים: תומר הרץ, זיו יניב

http://www.cs.huji.ac.il/~introcsmאתר:

Page 2: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

2introduction

Introduction to CS for MathIntroduction to CS for Math

Goal:• Introduce basic concepts of Computer Science

• Acquire basic programming skills in a useful programming language - Java

Useful books: • Java software solutions: Lewis & Loftus

• An Introduction to Computer Science Using Java: Kamin, Mickunas, Reingold

• Java for Engineers & Scientists: Chapman

Page 3: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

3introduction

Course plan (syllabus)Course plan (syllabus):: basic programming concept:

• syntax, variables, assignment (unit 1, unit 2)• flow of control (unit 3)• arrays (unit 5)

object oriented programming• objects and classes (unit 4)• advance topics (unit 6, unit 8, unit 9)• error handling (unit 10) and input-output (unit 13)

topics in computer science • recursion or divide and conquer methods (unit 7)• data structures (unit 11)• theory of computation (unit 12)• computer architecture (unit 14)• internet concepts (unit 15)

Page 4: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

4introduction

Course policyCourse policy

You must get a passing grade on ALL exercises

final grade: 35% combined exercise grade + 65% final exam

detailed (and updated) syllabus will be made available on the course web-site a few days before class, including all ppt slides; you are encouraged to consult the site before the class

Page 5: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

5introduction

Introduction (lecture #1)Introduction (lecture #1)

What is a Computer?

Algorithms

How is a typical computer built?

How do computers perform their task?

Page 6: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

6introduction

What is a ComputerWhat is a Computer??

In the most general sense, computers are machines that carry out given tasks

Computers are very good at:• processing large amounts of data in a short time

• storing and retrieving large bulks of data

• rapid arithmetic calculations

Computers do only what we tell them to (deterministic)

Page 7: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

7introduction

What is a TaskWhat is a Task??

Inputs

Outputs

Process

task manipulation of data:

The computer runs a process that gets inputs related to the task, and outputs data which represent the result of executing the task

Page 8: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

8introduction

ExamplesExamples

Computing taxes for employees of a company• Inputs: textual and numerical data representing information

about the employees

• Output: formatted textual tax reports F15 flight simulator

• Inputs: Joystick signals and key strokes from the user

• Output: Image data representing the pilot’s view Search engine:

• Input: keywords to look for

• Output: locations in Internet where keywords found

Page 9: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

9introduction

AlgorithmsAlgorithms

An algorithm is a scheme for carrying out a given taske.g., card sorting

An algorithm is specific for a particular kind of task, and many inputse.g., different card decks

Given a task and a set of inputs, the algorithm describes a computational procedure (a sequence of computational steps) to get the desired outputse.g.,

find smallest card move card to the left find next smallest card move to the right of previous card ...

Page 10: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

10introduction

Page 11: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

11introduction

Page 12: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

12introduction

Page 13: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

13introduction

Real-world ExampleReal-world Example

Example - an algorithm for fixing a flat tire:

• Take out the screws of the tier• Put the car on a jack and lift it• Take out the wheel• Put the reserve wheel• Put back the screws• Lower the car• Fasten the screws

Page 14: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

14introduction

Mathematical ExampleMathematical Example

An algorithm for checking if an integer is prime

Input: An integer n

Output: true if n is a prime, false otherwise

For all integers i such that 1<i<n

If i divides n

Output false (task is accomplished)

Output true

Page 15: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

15introduction

AlgorithmsAlgorithms

An algorithm consists of a list of instructions, which can be described in different levels of details

The shown algorithms are intended for people; a description intended for a computer is more detailed and accurate

what is the origin of the word algorithm?

Page 16: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

16introduction

Choosing algorithm: exampleChoosing algorithm: example

Evaluation measures for algorithms:• what is the execution time of algorithm?• can we find a better algorithm for a given task?

Task: find someone’s phone number in a phone-book

Alternatives: • Linear search• Binary search

Page 17: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

17introduction

Divide and ConquerDivide and Conquer

Binary search: divide the list in 2 recursively, until desired number is found.If the phonebook has 100000 names, in the worst case we will be looking at the names in the following relative locations: 50000, 25000, 12500, 6250, 3125, 1563, 782, 391, 196, 98, 49, 15, 13, 7, 4, 2; total of 16 steps

Compare to serial search: 100,000 steps

How many steps in the general case of divide and conquer method?

Page 18: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

18introduction

Algorithms & Computer ScienceAlgorithms & Computer Science

The notion of an algorithm is fundamental to computer science

We deal with questions like:• Is there an algorithm for every task?

• How much time does it take to execute a given task by following a given algorithm?

• What is the best way to express an algorithm?

• How can we know if an algorithm is correct?

• What is the best algorithm for a given task?

Page 19: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

19introduction

Interacting with the computerInteracting with the computer

How can we represent the data of a task to the computer?

How can we describe to the computer the algorithm for executing the task?

How will the computer describe to us the output representing the result of executing the task?

Page 20: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

20introduction

Data representationData representation

There are two basic ways to store and manage data Analog

• continuous, in direct proportion to the data represented

• example: a mercury thermometer - the mercury rises in direct proportion to the temperature, an LP record

Digital• the information is broken down into pieces, and each

piece is represented separately• example: digital thermometer, music on a CD

Page 21: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

21introduction

Digital InformationDigital Information

Modern computers store all information digitally, including:• numbers, text, graphics and pictures, audio, video,

program instructions

In some way, all information is digitized - broken down into pieces which are represented as (natural) numbers

Page 22: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

22introduction

Representing Text DigitallyRepresenting Text Digitally

Each character, including spaces, digits, and punctuation, is stored as a number

Corresponding upper and lower case letters are separate characters

H i , H e a t h e r .

72 105 44 32 72 101 97 116 104 101 114 46

Page 23: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

23introduction

Binary NumbersBinary Numbers

Once information is digitized, it is represented and stored in memory using the binary number system

A single binary digit (0 or 1) is called a bit A single bit can represent two possible states, like a light

bulb that is either on (1) or off (0) Combinations of bits are used to store larger values

All modern computers rely on basic devices that store and move binary information (0 or 1); why?

Page 24: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

24introduction

Bit PermutationsBit Permutations

Each bit that is added to the string doubles the number of items that can be represented

N bits can represent 2N unique items1 bit 21 = 2 items

2 bits 22 = 4 items

3 bits 23 = 8 items

4 bits 24 = 16 items

5 bits 25 = 32 items

Page 25: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

25introduction

Representing ProgramsRepresenting Programs

Program are represented digitally (as a list of numbers)

Each number represents either the code of an instruction or some data

The computer reads the instructions and their data one by one and executes them

how does this miracle happen?

Page 26: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Hardware and SoftwareHardware and Software

Hardware• the physical, tangible parts of a computer• keyboard, monitor, wires, chips, disks

Software• programs and data• a program is a series of instructions

A computer requires both hardware and software Each is essentially useless without the other

Page 27: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Hardware ComponentsHardware Components

Central Processing Unit (CPU)• the chip that executes program commands

• Example: Intel Pentium processor, Sun Sparc processor

Input / Output devices• allow interaction with the user

• Example: keyboard, monitor, mouse

Page 28: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Hardware ComponentsHardware Components

Main memory• the primary storage area for programs and data in

active use

Secondary memory devices• long-term storage

• floppy disks, hard disks, tapes

Page 29: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Computer organizationComputer organization

Harddisk Main

memoryFloppy

disk Keyboard

CPU

Monitor

Page 30: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Computer ArchitectureComputer Architecture

Now we can examine the hardware components of a computer in more detail

The CPU and main memory are the two key hardware components

All other devices can be considered peripherals Controllers coordinate the activities of specific

peripherals Binary information moves between devices across

a group of wires called a bus

Page 31: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Computer ArchitectureComputer Architecture

Videocontroller

Controller

Monitor Harddisk

Diskcontroller

Mainmemory

CPU

Floppydisk

otherperipherals

bus

Page 32: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

MemoryMemory

Main memory is divided into many memory locations

Each memory location has an address which uniquely identifies it

Data is stored in one or more consecutive memory locations

On most computers, each memory location holds 8 bits, or 1 bytein modern computer, the number of locations in a basic memory cell is a power of 2 (4, 8, 16, 32, 64, etc); why?

Page 33: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

MemoryMemory

927892799280928192829283928492859286

address

large values arestored in consecutivememory locations

Page 34: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Storage CapacityStorage Capacity

Each memory device has a storage capacity, indicating the number of bytes it can hold

Capacities are expressed in various units of binary storage:

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)

Page 35: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Main memory is volatile - stored information islost if the electric power is removed

Secondary memory devices are nonvolatile

Main memory and disks are random access devices, which means that information can be reached directly

A magnetic tape is a sequential access device since its data is arranged in a linear order - you must read all intermediate data in order to access subsequent information

Types of memoryTypes of memory

Page 36: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

RAM vs. ROMRAM vs. ROM

RAM - Random Access Memory

ROM - Read-Only Memory

The terms RAM and main memory are basically interchangeable

ROM could be a set of memory chips, or a separate device, such as a CD ROM

Both RAM and ROM are random access devices

Page 37: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

The Central Processing Unit (CPU)The Central Processing Unit (CPU)

A CPU is also called a microprocessor It retrieves, interprets, and executes instructions, one after

another, continuously This process is called the fetch-decode-execute cycle The CPU contains:

• control unit - coordinates processing steps

• registers - small storage areas

• arithmetic / logic unit - performs calculations and decisions

Page 38: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

The Central Processing UnitThe Central Processing Unit

Registers

Control unit

Arithmetic / Logic unit

Mainmemory

CPU

Example: von-Neumann machine

Page 39: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

The Central Processing UnitThe 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 megahertz (MHz)

Page 40: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

40introduction

Operating SystemOperating System

Programs are stored on a secondary memory (disk)

In order to execute, a program must be loaded into the main memory

The operating system is a program that manages the resources of the computer; among other things, it allows us to load and execute programs

The operating system itself is loaded to the memory as part of the boot process which takes place when the computer is started

Page 41: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

Software CategoriesSoftware Categories

Operating system• controls all machine activities

• provides the user interface to the computer

• manages resources such as the CPU and memory

• Examples: Windows XP, Solaris, Linux, Mac OS

Application program• generic term for any other kind of software

Page 42: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

42introduction

Computer ProgramsComputer Programs

An algorithm is described to the computer as a computer program

Programs can be written in different programming languages

The computer can only execute programs expressed in the computer machine language

Programs written in other languages should be translated to the computer machine language

In this course we will learn how to program using the Java Programming Language

Page 43: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

43introduction

Machine LanguageMachine Language

A machine language program consists of a list of instructions and data

Each computer has a limited set of instructions The instructions are very elementry

• Add two integers• Test if a given integer is zero

The strength of computers is due to their capability of executing millions of simple instructions per second

Example: adding numbers in assembly

Page 44: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

44introduction

Programming LanguagesProgramming Languages

Low level languages are closely tied to the computer’s instruction set; they are good when:• the program must control some hardware that can

only be controlled in this low-level language

• the program must run extremely quickly

But: writing programs in machine language is difficult and time consuming

Page 45: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

45introduction

Higher-level LanguagesHigher-level Languages

Higher level languages like Pascal, C, C++, Java, are more disconnected from the hardware on which they run

They can be used to solve any kind of problem

They can run on any kind of computer

But: a program written in a high-level language must be translated into a machine language program in order to be executed

Page 46: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

46introduction

Compilers & InterpretersCompilers & Interpreters

Compiler • A compiler is a program that translates a program into a

machine language program.

• Resulting program can be executed many times

• Compiler is specific to programming language

Interpreter

• An interpreter executes a program by reading its statement one by one, translating each statement to machine language code and executing it

• Translation is done each time the program is run

Example: adding number is C++

Page 47: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

47introduction

JavaJava

Designed by a Sun team led by James Gosling Originally called Oak, it was intended for consumer devices like

TV-top boxes Being cross platform, and more stable than C++, were essential

goals When the TV-top market didn’t materialize, figured they’d try

the internet object-oriented has compiler AND interpretter

a bit of trivia: where did the name JAVA come from?

Page 48: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

48introduction

Object Oriented ProgrammingObject Oriented Programming

Object-Oriented Programming (OOP) is a methodology for writting computer programs

When we write a program for a given task, it is helpful to think of the task in terms of a real world task

Examples: A football computer game, computerized banking system

In order to represent and solve the task for the computer we make a model of the real-world; modeling precedes alogorithmic “how to”

Page 49: 1 מבוא למדעי המחשב למתמטיקאים, 67117 ימים שני ושלישי 14-12 מרצה : פרו " פ דפנה וינשל שעת קבלה : שלישי 15-14 (6298-8

49introduction

Object Oriented ProgrammingObject Oriented Programming

The real-world consists of objects• Football field, players, ball, fans, ...• Banks, accounts, clients, ...

Our software model also consists of objects that reflect the real-world objects

(Software) objects consists of data and methods

The program creates objects, interacts with them and let them interact with each other

Objects are defined by classes