80
1 INTRODUCTION TO COMPUTER SCIENCE CONCEPTS AND PROGRAMMING CPSC 206

1 INTRODUCTION TO COMPUTER SCIENCE CONCEPTS AND PROGRAMMING CPSC 206

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

1

INTRODUCTION TO COMPUTER SCIENCE

CONCEPTS AND PROGRAMMING

CPSC 206

2

Outline

• Part I: an overview of computer science.

• Part II: computer hardware and software.

• Part III: computer languages.

3

Part I: An Overview of Computer Science

4

What is Computer Science?

The study of how to solve problems with computers:

design and build a computer

- architecture, electrical engineering

use and share the computer efficiently

- operating systems

know if your problem is solvable

- computability theory

5

Computability Theory

Motivated by 2 fundamental questions.

1) What is an Algorithm?2) What are the capabilities &

limitations of algorithmic computation?

Area of research for mathematicians & logicians since the 1930’s.

6

communicate a solution to a computer:

- programming languages, compilers, semantics

7

Semantics

of a programming language.

Associates a meaning with each syntactically valid construct.

Describes the actions that will occur when the program associated with any valid construct in the language which is executed by a computer.

8

analyze your solution for correctness and efficiency

know if your solution is optimal

- complexity theory

9

Complexity Theory From computability to complexity, attention

shifts from exhibiting the existence of algorithms to analyzing their efficiency .

The performance is measured by the resources required by a computation. “How much question”.

A solvable problem may have No practical solution: every solution may require anunacceptable amount of resources.

10

find a solution automatically

-artificial intelligence (make your solution easier for people to use)

- computer-human interaction solve BIG problems

- software engineering exploit multiple computers

-parallel and distributed systems, networking

11

Software Engineering

Real-life programs are usually

• large (thousands of lines of code)

• created by teams of people

• modified over the course of years

Rules and methods are needed to cope with this complexity.

Computer Science is more than Programming!

12

Part II: Computer Hardware and Software

13

Outline

• The development of modern computer systems.

• Computer organization.

• Solving problems on computers.

• Programming and software engineering.

14

The Development of Modern Computer Systems

•Early electronic computers

•Mainframes

•Time sharing

•Microcomputers

•Networked computing

15

Early electronic computers

• ENIAC (1946) - Electronic Numerical Integrator and Calculator:– First general purpose purely electronic digital

computer.– Built for US Army to make calculations for

weather predictions & ballistics tables.– 18, 000 vacuum tubes, space 50 X 30 ft,

weighed 30 tons.– Numbers are entered by manually setting its

6000 switches.

16

• EDVAC - Electronic Discrete Variable Calculator– A general instruction set and a stored program.

– Both numbers and program instructions were stored electronically in the computer’s memory.

• UNIVAC - Universal Automatic Computer– First commercially built computer.

– Delivered to the U.S. Census Bureau in 1951 to tabulate the results of the previous year’s census.

17

• John von Neumann (1903-1957)

– “von Neumann machine”.– Binary number computation.– Memory for data storage.– Input and output devices.– Overall logical control.

18

Mainframes

• Generation– The development of modern computers is

often described in terms of the “generation”, in which a particular technology was used.

• Architecture– The overall design of a computer’s electronic

circuitry and its logical functionality is called its architecture.

19

• First generation – 1950s– Architecture

• Electronic circuitry: vacuum tubes.• Memory: magnetic cores.

– Stored program• Program: machine language.• Processing: a single program at a time.

– Peripheral devices• Input and output: punched cards.• Data storage: magnetic tapes.

20

• Second and third generations – 1960s

– Architecture• Electronic circuitry: transistors to integrated

circuits (ICs).

– Symbolic languages• Assembly language.

• FORTRAN - FORmula TRANslation, first high-level language.

• COBOL - Common Business-Oriented Language.

• LISP - LISt Processing.

21

– Translators: translating high-level language to machine-understandable form:

• Assemblers: converting program instructions from assembly languages into the machine language.

• Compilers: translating program statements from FORTRAN and COBOL to machine language.

• Interpreter: translating and executing each program statement dynamically.

22

– Operating Systems (OS): a program to control the overall processing of the machine.

• Batch processing: jobs that were similar in their requirements were grouped together into batches and run sequentially.

• JCL (Job Control Language): to minimize operator intervention, JCL cards were inserted between the card decks of each job to direct how each job was to be processed.

• Mass storage: disk drives provided permanent storage and fast access to large quantities of data.

23

Time Sharing

• Time sharing controls computer operations in such a way that input and output activity (I/O) would not slow down the primary computation.

• Multiprocessing (mid-1960s)– A program was broken into tasks (processes).

– Each process was allow a interval of time for execution before control was passed to another one.

• Interactive computing (early 1970s)– User could sit at a remote terminal and communicate

directly with the computer.

– Allow multiple users at the same time.

24

Microcomputers

• Fourth generation – 1970s– Chip circuitry: VLSI - Very Large Scale

Integration, which could contain all logic circuits on a single chip. These chips were known as microprocessors.

• Minicomputers – 1970s– Lower cost and more accessible computing

power.– Architecture is based on the 16-bits (vs. the

32-bits in use for larger computers) representation of data in computer memory.

25

• PC - Personal Computer – 1970s-1980s– Low cost computers intended for personal use.

• Input: keyboard.

• Output: screen.

• Data storage: removable floppy disks.

• Examples: Apple, IBM PC.

– DOS - Disk Operation System• DOS was developed by Microsoft for the IBM PC.

• DOS provides the basic functions for a single user to handle data storage and to control input and output devices and program execution.

26

GUI – Graphical User Interface:

• With the introduction of the Mac (Macintosh) computer by Apple in 1984.

• Information was presented to the user via pictures, known as icons.

• A new hand-held input device, called a mouse, was available for selecting choices from the screen.

• Mac utilized a bit-mapped display which is able to display both graphics and text.

• Each graphical image is composed of small dots called pixels which must be manipulated individually by the computer ‘s programs.

27

• PC software:

– Software are programs directing a computer’s operations and solving problems.

– Hardware are the physical devices that form the computer itself.

– The popularity of PCs grew rapidly in the 1980s as innovative application programs were developed to enable nontechnical users to do useful tasks on the computer.

28

• Word processors allow a user to write and edit text.

• Spreadsheet programs provider convenient computational power for data stored in tables.

• Business graphics programs are able to display data in the form of graphs and charts.

29

• Workstations – 1980s

For complex number crunching scientific and engineering programs.

– For a single user.– Using multiprocessing systems.– Well suited for networking use.

30

Networked Computing

• Computer networks – 1980s-present– Users access computers which are at a

distance.– Users share data.– Users transmit messages between computers.

• Telephone lines– An electronic device called a modem allows

computers to transmit data over telephone lines,

31

• LAN – Local Area Network– A LAN allows computers in reasonable proximity of

each other to be connected by cabling over which data can be transmitted from one machine to another without the use of telephone lines.

• Distributed computing

– Techniques of shared processing over networks are generally called distributed computing.

– A computer that provides the core of the services is called a server.

– Any computer requesting a service is known as a client.

32

Computer Organization

•Data representation

•Main Memory

•Central Processing Unit

33

Data Representation

• The information which a computer processes is generally known as data.

• Individual data values may represent numbers, alphabetic characters, or other coded information.

• The computer programs are also data, because the instructions which they contain must be stored in the computer’s memory before they are able to be processed.

34

• All data is stored and processed in binary form, that is, as a series of 0s and 1s.

• Each binary digit, called a bit, represents the smallest unit of information which can be stored in the computer.

• Bits are grouped into longer units known as bytes to hold more meaningful data.– 1 byte = 8 bits

35

• Two standard coding systems for representing a byte.– ASCII – American Standard Code for

Information Interchange• Hello 1001000 1000101 1001100 1001111

H – 72 128 64 32 16 8 4 2 1

27 26 25 24 23 22 21 20

0 1 0 0 1 0 0 0– EBCDIC – Extended Binary Coded Decimal

Interchange Code:• Hello 11001000 11000101 11010011 11010011

11010110

36

Main Memory

• Main memory is that part of a computer’s electronic circuitry which holds the binary data which the computer’s program will process.

• Memory is divided into cells.

• Each cell is assigned a specific address, from 0 to the maximum size of the computer’s memory capacity.

37

• The size of a computer’s memory is the number of the addressable cells it contains.

Bit 1

Byte 8 bits

Kilobyte210 bytes 1,000

Megabyte 210 kilobytes 1,000,000

Gigabyte 210 megabytes 1,000,000,000

Terrabyte 210 Gigabytes 1 trillion

e.g. A 256K bytes of memory have 256*1024=262,144 bytes.

38

• ROM – Read-Only Memory, is a specialized part of main memory which is designed to prevent data loss when the power to the computer is turned off.

– Read-Only.– Permanence.– Typical use: ROM is reserved for critical

program instructions which must be immediately available when a computer is first turned on.

39

Central Processing Unit

• CPU is the heart of a computer.– Control unit has the overall task of

controlling and coordinating the computer’s operations.

– Arithmetic/Logic Unit (ALU) performs all arithmetic computations and logic operations.

40

• Control unit:– Program execution:

• Each computer has a unique instruction set determined by the designers of its architecture. Each instruction includes a code that specifies the operation to be performed, and the memory address of the data value to be acted on.

• All the operations of a computer are directed by a set of instructions, known as a program, which is stored in the computer’s main memory.

• The control unit locates the appropriate instructions, controls their sequencing, and executes them by activating appropriate circuitry.

41

• Program Counter (PC) is a special-purpose memory location which always contains the memory address of the instruction that is currently being executed.

• Instruction counter (IC) is another special-purpose memory cell which contains the instruction currently being processed.

42

– Hardware characteristics• CPU synchronizes its operations by the regular

pulses emitted by an electronic device called a clock.

• The speed of a computer can be quoted as:– Clock speed, e.g. 100MHz means 100 million cycles per

second.

– MIPS, a million instructions per second

43

• Arithmetic/Logical Unit (ALU) performs all arithmetic computations and logical operations.

Arithmetic - Add, subtract, multiply, divide

exponentiation etc.

Logical - Testing for relationships.

A<B; Name = ‘mary’;

c>=10; possible Answers? True or False

The ALU contains special memory cells, known as registers, in which the arithmetic is carried out.

44

reads the individual program

instructions from main memory. Executes the instructions 1 at a time until completion

Fetch - Decode - Execute

The machine cycle algorithm is continuously repeated until program termination.

CPU

Machine Cycle

45

Fetch:

Retrieve the next instruction from memory [where the command is located (address) is stored in a register called the program counter]

update the program counter to the address of the next instruction.

The instruction just Fetched is placed in the instruction register

46

Decode the Bit pattern

in the instruction register

Having decoded the instruction, the control unit enters the execute phase. It activates the correct circuitry to perform the requested task.

Decode:

Execute:

47

If the instruction is a load from memory, the Control unit causes the load to occur.

If the instruction is for an arithmetic operation, the control unit activates the appropriate circuitry in the ALU with the correct input stored in registers.

When the instruction has been executed, the control unit again begins the machine cycle with the Fetch cmd (the address of the next cmd is in the program counter)

48

Diagram of Architecture

CPU

R1

PC R2

IC R3

R4

control unit ALU

bus

49

Diagram of Architecture (cont.)

First second third

Instr. Instr. Instr.

0 1 2 3 4

95 96 97 98 99…… ..

datamemory

50

Solving Problems on Computers

51

Designing Modular Solutions

• Top-down1. Start with what you want

2. Cut it up in parts

3. If the parts are trivial to solve, then solve them

4. If the parts are non trivial, apply top-down again

You will cut up the problem in smaller and smaller sub-problems until they can be solved trivially.

• Bottom-up1. Start with what you small parts

2. Put them together to form large parts

3. Until you end up with the thing that you wanted

52

Designing Modular Solutions

• Problem with top-down design– You must know how you break the problem

up.– You need intuition to tell you that this is the

right way to divide the problem into sub-problems.

• Problem with bottom-up design– Where do you start?– Usually used in an evolution way, thus

systems start simple, functionality increases as time goes by.

53

An example of top-down design

Compute Tax

Compute adjustments

Compute deductions

Compute credits

Compute adjusted gross income

Compute Income

Interest &dividends

Wages Capital gains

Business income

Misc. Income

54

Algorithm Development

• An algorithm is a specification of the series of steps which must be followed in order to solve a problem or accomplish a task.

• Algorithm development

55

IPOS Cycle

I - Input

Implies 3 Questions

Input what?

Input how?

Input where?

56

Input What?

Program & Data

Program [Load Module] in machine code form after being compiled & Linked

e.g.

05C0 5820C00E Hexidecimal Notation

in Binary form

0000 0101 1100 0000 etc.

0 5 C 0

57

Datae.g. Program to calculate a students avg grade

string data numeric data

Bob 72 84

will store data using:

Binary coding scheme

ASCII or EBCDIC

58

How input?Input Devices: Floppy disk, Magnetic Tape Drive,

Disk Drive, CRT, Voice, Light pen, Scanners etc.

Where input?

Memory of Computer

has evolved over the computer hardware generations.

59

P- processing

2 categories

Arithmetic - Add, subtract Multiply, DivideExponentiationTrig functions

etc.Logical - Testing for relationships.

A<B; Name = ‘mary’;c>=10; possible Answers?

True or False

60

Output - What?

Hard copy: physical, tangible, portable.

Print outs.

Soft copy: visual, CRT displays

Output devices: printers, floppy disk, Mag tape, Mag disk, voice, etc.

61

S - Secondary Storage Medium

Long Term Storage: Disks, Tapes, etc.

What - Program Itself

Program output

62

Example

An algorithm for a program converting Fahrenheit temperature values (F) to Celsius degrees (C).

Input{Processing}OutputStorage

F{apply F to C conver. form.}CDisk

C = 5/9*(F-32)

63

Algorithm Representation

• Pseudocode– The steps of an algorithm are sometimes

written in English in a shorthand form called pseudocode.

– There are no rules for writing pseudocode.– It is purpose is to summarize the steps of the

solution and to make the logical sequence of a program clear.

64

An Example A professor who is about to write a program to assign grades to the students in his Programming in C course might express his final grade algorithm in the following pseudocode.

locate a student’s scores|

compute the average quiz score |

compute the average homework score|

if the midterm exam was a make-upthen reduce the exam score by 10%

|compute the student’s course grade

|post the student’s course grade

65

Algorithm Representation (cont.)

• Flowcharts– For the problem whose solutions involve

decision and repeated steps.– Flowcharts typically show a program’s logic.– Symbols:

Start/stop Input/Output

Decisions Processing

Flow of control

66

An Example An algorithm to control an Automatic Teller Machines (ATMs).

Start

Stop

Read card

Correct pwd?

Access account info

Reject cardno

yesDeposit

Withdraw

Inquire

……

67

Algorithm Representation (cont.)

• Decision TreesAn example A telephone company bases its rates for long distance calls on the time of day and day of the week when a call is made.

Long distancecharges

Weekday

Weekend

8am to 5pm5pm to 11pm11pm to 8am

Saturday

Sunday

8am to 5pm5pm to 11pm11pm to 8am

full rateevening ratenight rate

night rateevening ratenight rate

night rate

68

Programming

• Programming is the process of translating a problem’s solution into instructions that a computer can process.

• Software life cycle– Analysis: analyze the problem.– Design: use top-down or bottom-up.– Implementation: use hardware and/or

software.– Testing: try it in the lab.– Maintenance: keep it working.

Program

ming

69

Programming (cont.)

• Paradigms– Procedural programming

• Gives commands to do things.• These commands change the state.

– Declarative programming• Givens definitions of operations.• Tells the system what you want to know, and it

will figure it out.

– Object oriented programming• Like procedural, but has operations on objects

only.• Operations change the state of an object.

70

Programming (cont.)

• Example languages– Procedural

• C, Pascal, Basic, FORTRAN, COBOL, Ada…

– Declarative• LISP, Prolog, …

– Objected oriented• Smalltalk, Java, C++,

71

Part III: Computer languages

72

Why computer language?

• Compute are stupid.– They do not “understand” things.– They have to be told very precisely what to do.

• Natural language are ambiguous.– “College Station’s largest (quality (free)

newspaper)”.

73

Why computer language? (cont.)

• Programming language are designed to be unambiguous.– They are defined precisely, by syntax and

semantics.– The meaning of each program is defined

precisely.• Priority of operators is defined, e.g., 2+3*4 means

2+(3*4).

• Associativity is defined, e.g., 2-3-4 means (2-3)-4.

74

A brief history of C

• BCPL was developed by Martin Richards

• B was developed by Ken Thompson

• C was developed and implemented by Dennis Ritchie on a DEC PDP-11 in the 1970s.

• The ANSI C standard was adopted in 1989.

75

C is a middle-level language

• C combines the best elements of high-level language with the control and flexibility of assembly language.

• C allows the manipulation of bits, bytes and addresses.

• C is portable. Portability means that it is easy to adapt software written for one type of computer or operation system to another type.

• C has only a small number of keywords. ANSI C defined 32 keywords (BASIC has over 100!).

76

C’s place in the world of programming languages

High level Ada, Modula-2, Pascal, COBOL, FORTRAN

BASIC

Middle level Java, C++, C, FORTH,

Macro-assembler

Low level Assembler

77

C is a structured language

• Compartmentalization of code and data. This is the ability of a language to section off and hide from the rest of the program all information and instructions necessary to perform a specific task. – Use of subroutines that employ local variables.

– Use of code block. if (X<10)

{

printf(“Too low, try again.\n”);

scanf(“%d”, &x);

}

78

C is a structured language (cont.)

– Use of procedures & functions.– No use of go-to statements.

• By use of structured languages it was for the first time possible to write moderately complex programs fairly easily.

• No structured – FORTRAN, BASIC, COBOL

• Structured – Pascal, Ada, C++, C, Java…

79

‘C’ was considered by many to be the best language implemented with a structured approach. Its creation was a direct result of the need for a structured, efficient, high-level language that could replace assembly code when creating systems programs.

80

It is a powerful, efficient, structured language that is relatively easy to learn.

It is a programmers language developed by professional programmers. Few restrictions, block structure, stand-alone

functions, a compact set of keywords. During the late 1970’s & early 80’s “C”

became the dominant computer programming language, still widely popular.– Initially, C was used for systems

programming, which forms a portion of the operating system or its utilities, such as editors, compilers, linkers.

– Now C is used to program all tasks.