68
Algorithms and Programming Languages CS 1 Rick Graziani Cabrillo College Spring 2011

Algorithms and Programming Languages CS 1 Rick Graziani Cabrillo College Spring 2011

Embed Size (px)

Citation preview

Algorithms and Programming Languages

CS 1

Rick Graziani

Cabrillo College

Spring 2011

Rick Graziani [email protected] 2

Algorithm

• An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

• Algorithms are part of many activities, even mundane ones.

• Note: Researchers believe that the human mind including imagination, creativity, and decision making, is actually the result of algorithm execution.– This is used in artificial intelligence

Rick Graziani [email protected] 3

Example

• Obtain a basket of unshelled peas and an empty bowl.

• As long as there are unshelled peas in the basket continue to execute the following steps:

a. Take a pea from the basket

b. Break open the pea pod

c. Dump the peas from the pod into the bowl

d. Discard the pod

Rick Graziani [email protected] 4

Defining the Algorithm

• An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.– Steps do not have to be executed in sequence.

• Non-Terminating Sequence: – Make a list of positive integers.– The above requirement could not be performed in an algorithm,

because it does not terminate (it is infinite). • Unambiguous

– The instructions must be clear, specific and direct– No room for creativity or interpretation

1 2 3 4 5 6 7 8 9 10 11 … (this could go on for ever!)

Non-terminating sequence

Ambiguous

Organize the CDs (By title? By artist? By genre?)

Rick Graziani [email protected] 5

Abstract Nature of Algorithms

• An algorithm can represented in several ways.

• Example: Algorithm to convert temperatures from Celsius to Fahrenheit: – As an algebraic formula:

• F = (9/5)C + 32– As a written instruction:

• Multiply the temperature reading in Celsius by 9/5 and then add 32

Rick Graziani [email protected] 6

Algorithm Representation

• Algorithm requires some form of a language.

• Algorithm is a form of communication– Don’t want

misunderstandings– Proper level of detail– Proper level of difficulty

• Problems arise when:– Steps not precisely defined– Not enough detail

Rick Graziani [email protected] 7

• Primitive – A well defined set of building blocks (terms) used in computer science.– Arithmetic and logic operations built into the language– Removes any ambiguity– Includes its own syntax

• Programming language – A collection of primitives (terms) and the rules that state how the primitives can be combined to represent more complex ideas.

Algorithm Representation

LOAD reg. R from cell XY

Op-code Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Rick Graziani [email protected] 8

• Machine language uses primitives

• High-level programming languages (C++, Java) use higher-level primitives, constructed from the lower-level machine language primitives.

• This results in an easier set of instructions to write.

• More later.

Store the bits found in register 5 in main memory cell A7

Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s

comp.) 6 ADD S and T into R. (floating

pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Rick Graziani [email protected] 9

Pseudocode

• Pseudocode – A notational system in which ideas can be expressed informally during the algorithm development process. (written sentence)

• Used independently of the programming language.– Each programming language has its own primitives and rules.

Pseudocode

1. Enter two numbers.

2. Add the numbers together.

3. Display the result.

Rick Graziani [email protected] 10

Pseudocode

Conditional selection• The selection of one of two possible activities depending upon the truth or

falseness of some condition

if condition then action

or

if condition then (activity)

else (activity)

• If this condition is true, perform this activity.If (sunny)

then (put on sunscreen)

• If this condition is true, perform this activity, otherwise perform a different activity.If (sunny)

then (go swimming)else (go bowling)

Rick Graziani [email protected] 11

Repeating structure

• Another common semantic structure is the repeated execution of a statement or sequence of statements as long as some condition remains true.

while condition do activity

• Also known as a while loop• Examples:

while (tickets remain to be sold) do (sell a ticket)

Rick Graziani [email protected] 12

Repeating structure

Task: Write Hello 500 times.

PseudocodeCounter = 1While counter is less than or equal to 500, write the word “Hello” and add 1 to Counter.

Programming CodeCounter = 1While (counter <= 500)

do(print “Hello”; Counter Counter + 1)

Counter

1

Hello

2Hello

3 Hello4Hello

500501

Hello

…<End of loop>

Rick Graziani [email protected] 13

For loop

• A for loop can be used to accomplish the same thing as a while loop.• Note: There are some differences between while and for loops.

Counter = 1For (Counter <= 500)

do(print the message “Hello”; Counter Counter + 1)

Counter

1

Hello

2Hello

3 Hello4Hello

500501

Hello

…<End of loop>

Rick Graziani [email protected] 14

• “I want you to write on the chalk board, ‘I will not throw paper airplanes in class’, 500 times.”

Software Development

Rick Graziani [email protected] 16

Definitions

Software or Program

Instructions that tell the computer what to do

Programmer

Someone who writes computer programs

Rick Graziani [email protected] 17

Instruction SetA vocabulary (list) of instructions which can be executed

by the CPU• The only instructions the CPU can run or execute• Example of a CPU’s Instruction Set

CPU Instruction Set

Instruction Set Instruction0001 Move0010 Compare0011 Bit test0100 Bit clear0101 Bit set0110 Add0111 See group 101000 See groups 11, 13, 141001 Move byte

Rick Graziani [email protected] 18

First Generation Languages(Machine Language)

• Programming computers using the CPU’s instruction set• Also known as Machine Language• (timeline)

Machine Code FileA software file which contains the instructions from the CPU’s instruction set.

Rick Graziani [email protected] 19

Advantages of First Gen.

• Software programs execute (run) relatively very quickly

• Software programs are relatively small in size

• (Insignificant advantages today)

Disadvantages of First Gen.

• Difficult to write, very detailed and takes a long time

• Difficult to read

• Difficult to debug

debug = the process to find mistakes in a software program

First Generation Languages(Machine Language)

Rick Graziani [email protected] 20

Second Generation Languages(Assembly Language)

Assembly Language = The English-like instructions which are equivalent to the CPU’s instruction set

Source Code= The actual instructions written by a programmer

Compiler = Software which translates source code instructions of a particular language into machine code

Op-code Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Rick Graziani [email protected] 21

Question: Which of these two files (source code file or machine code file) will the user need to run this software program?

Advantages of Second Gen.

• Easier to read than first gen.

• Easier to write than first gen.

• Easier to debug than first gen.

Disadvantages of Second Gen.

• Still very difficult to write programs

Second Generation Languages(Assembly Language)

Rick Graziani [email protected] 22

Using a compiler

Rick Graziani [email protected] 23

Third Generation Languages(High level languages)

Languages which are somewhere between machine language and the human language.

FORTRAN (Formula Translation) - 1950's

Language to allow scientists and engineers to program computers.

COBOL (Common Business Oriented Language) - 1960

Language primarily designed for US government and defense contractors to program business applications on the computer. Grace Hopper was one of the developers of COBOL.

BASIC (Beginner's All-purpose Symbolic Code) - 1960's

Alternative language to FORTRAN for beginning programming students.

Rick Graziani [email protected] 24

Pascal (named after Blaise Pascal, 17th century French mathematician) - 1970's

Language to teach proper structured programming.

Structured programming = Programming technique used to make programming more productive and easier to write. Stresses simplistic, modular programs.

ADA (named after Ada Lovelace (programmed the 19th century 'analytical engine') - late 1970's

Language developed to replace COBOL.

Third Generation Languages(High level languages)

Rick Graziani [email protected] 25

C (successor to BCPL or "B") - 1970's Popular programming language on computers from

microcomputers to super computers.Faster and more efficient language. Very powerful language.

Source code example of a C Program (Displays Hello World! on the screen.)#include <stdio.h>main(){ printf("Hello World!");}

C++ (pronounced "C plus plus") - 1980'sObject oriented language which is compatible with C.

JAVA

Third Generation Languages(High level languages)

Rick Graziani [email protected] 26

Advantages

• Easier to read, write and debug

• Faster creation of programs

Disadvantages

• Still not a tool for the average user to create software programs

• Requires very good knowledge of programming and of the language

Third Generation Languages(High level languages)

Rick Graziani [email protected] 27

Writing a Software Program

Steps in writing a software program

1. Hardware (CPU)

2. Operating System

3. Programming Language

4. Brand of Compiler

5. Writing the Program

Rick Graziani [email protected] 28

Task

Write a program to convert binary numbers to decimal and decimal numbers to binary

Writing a Software Program

Rick Graziani [email protected] 29

1. Determine what kind of computer you want your program to run on

Macintosh?

PC?

Writing a Software Program

Rick Graziani [email protected] 30

2. Determine which operating system this computer (and the user) will be using

Windows Vista?

Mac OSX?

Linux?

Writing a Software Program

Rick Graziani [email protected] 31

3. Determine which language you will be programming in.

C?

C++?

Java?

• C++

Writing a Software Program

Rick Graziani [email protected] 32

4. Determine the compiler for your language, operating system and hardware

Microsoft Visual C++?

Borland C++?

Watkins C++?

• Microsoft Visual C++

Writing a Software Program

Rick Graziani [email protected] 33

5. Write the program

Writing a Software Program

Rick Graziani [email protected] 34

Compile the program into a machine code file and distribute it to the users via floppy diskette.

Writing a Software Program

Rick Graziani [email protected] 35

Fourth Generation Languages

Languages which are more like natural human languages

• uses English phrases

• common with data base languages

search for name equals “graziani” and state equals “ca”

Examples

dBase FoxPro Access

Oracle Informix SQL

Rick Graziani [email protected] 36

Fourth Generation Languages

Advantages• Average users can quickly learn to “query” the

database• Average users can easily learn how to write

programs• Programs are written faster

Disadvantages• Can not write sophisticate programs like word

processors, graphics, etc.• Mostly for data base applications like phone

information.

Rick Graziani [email protected] 37

The Year 2000What is the big deal?

Older computer systems

• limited RAM memory

• limited disk storage

• slower processors (CPUs)

The YEAR was stored using two bytes instead of four bytes, in order to save bytes in RAM and storage

• 67 instead of 1967

Rick Graziani [email protected] 38

Example:• 500,000 records

Save 1 million bytes 500,000 x 4 bytes (19xx) = 2 million bytes 500,000 x 2 bytes (xx) = 1 million bytes

• less storage on disk• less RAM memory needed• faster processing

The Year 2000What is the big deal?

Rick Graziani [email protected] 39

Problem

The year 2000 will be “00” or looked at by the computers as the year “1900”

Will cause miscalculations for everything from pension funds to horoscopes.

The Year 2000What is the big deal?

Databases and Relationships

Rick Graziani [email protected] 42

One-to-One Relationships

Rick Graziani [email protected] 43

One-to-Many Relationships

Rick Graziani [email protected] 44

Many-to-Many Relationships

(Not recommended)

Programming LanguagesPart 2

Rick Graziani [email protected] 46

Goal of third generation languages

• Goal of third generation languages (C, C++, Java):– Statements (language) do not refer to the attributes of

any particular machine• Statements not part of the CPU’s machine language

• Program written an a third generation language could theoretically be compiled to run on any other computer.– In reality this is not completely accurate

Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s

comp.) 6 ADD S and T into R. (floating

pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Rick Graziani [email protected] 47

Rick Graziani [email protected] 48

Rick Graziani [email protected] 49

Programming language standards

• Programming language (C, C++, Java):– Standard commands– Vendor’s options

• Standard organizations:– ANSI (American National Standards Institute)– ISO (International Organization for Standardization)

• ISO is not a TLA but meaning “equal” in Greek

Rick Graziani [email protected] 50

Vendor Compiler Options

• Vendor compiler options:– Language extensions– Features to make it easier to program– Not compatible with other vendor’s compilers– Makes it difficult to change to another vendor’s compiler

Rick Graziani [email protected] 51

Evolution of Programming Languages

• Lineage does not represent that one language evolved from another, although this is the case with some languages.

Rick Graziani [email protected] 52

Evolution of Programming Languages

Rick Graziani [email protected] 53

Evolution of Programming Languages

Rick Graziani [email protected] 54

Procedural Languages

• Procedural Languages– Traditional approach to programming.– Procedure is a sequence of commands which can be

used multiple times by the main set of instructions.

• Procedural Languages include:– Fortran, COBOL, BASIC, ADA, Pascal, C

Main Program

Procedure 1

Procedure 2

Procedure 3

Rick Graziani [email protected] 55

Object Oriented Programming

• Object Oriented Programming (OOP)– Software system is viewed as a collection of units, called objects.– Object – has the capability of performing certain actions and to call other

objects.• Example: Screen icons

– Each icon is an object– Each object encompasses a collection of procedures known as methods– Each method describes how that object will respond to events such as:

• Double left click (run the program)• Right click (display a series of options)

• Example OOP Languages: C++, Visual Basic, Java

Left click

Right click

Rick Graziani [email protected] 56

Other types of languages

• Other types of languages include:– Functional languages: LISP, Scheme– Declarative languages: Prolog– Scripting languages: Perl, PHP, shell scripts, HTML

Rick Graziani [email protected] 57

Programming Concepts

• Program consists of:– Declarative statements

• Variables and data types– Imperative statements

• Procedures, instructions, and algorithms– Comments

• Enhance readability• Used throughout the program

Rick Graziani [email protected] 58

Variables and Data Types

• Variable – A location in RAM (main memory), given a descriptive name, which stores information.

• Data type – Variables are a type of day which can be:– Number

• Integer: 0, 1, 2, 3, 4, 5, 6, etc.• Real (floating point): 9.75, 300.5412

– Character: a, b, c, A, B, C, etc.– String of text: “123 Main Street”

• Working area or scratch pad for the program.

125

9.75

d

Integer

Float

Character

Variable

Rick Graziani [email protected] 59

Programming Concepts

Count 1

I will not throw paper airplanes in class.

2

I will not throw paper airplanes in class.

3

I will not throw paper airplanes in class.…

499

I will not throw paper airplanes in class.

500

I will not throw paper airplanes in class.

501Variable Count which is type integer

Rick Graziani [email protected] 60

Variables

• Variables are declared, defined in the declaration section of the program.– Usually at the beginning of the program– Examples:

int height_in_inches

char first_initial

float price

Rick Graziani [email protected] 61

Variables

• Variables:– Can be given an initial value within the program– Value may change from:

• Program instructions• User input

Count 1

I will not throw paper airplanes in class.

2

I will not throw paper airplanes in class.

3

I will not throw paper airplanes in class.…

499

I will not throw paper airplanes in class.

500

I will not throw paper airplanes in class.

501

Rick Graziani [email protected] 62

Data structure

• Variables are often associated with a data structure.

• Data structure – A conceptual shape of arrangement of data

• Homogeneous Array – Block of values of the same type– Such as a one dimensional list or a two dimensional array (row,

column)

• Example: String or array of characters– char Last_Name[25]

1 2 3 4 5 … … 24 25

B o o l o o

char Last_Name[25]

Rick Graziani [email protected] 63

Data structure

• Two dimensional array– Row and column– Integer pins [bowler, frame]

Dan

Sue

Gary

Mary

3

12

0

7

6

9

1

7

Integer pins [bowler, frame]

Frame1 2 3 4 5 6 7 8 9 10

pins [Mary, 2] = 7

Rick Graziani [email protected] 64

Assigning Value

• Assignment statement – Statement which assigns a value to a variable.– Value assigned from user input– Value assigned from another variable– Value assigned from a specific value– Value assigned from a calculation that can include both

variables and assigned values.

125

9.75

d

Integer

Float

Character

Fahrenheit = (9/5)Celcius + 32

Rick Graziani [email protected] 65

Open Source Software

• Open source software is computer software for which the human-readable source code is made available under a copyright license (or arrangement such as the public domain) that meets the Open Source Definition.

• This permits users to:– Use the software– Change the software– Improve the software – Redistribute it in modified or unmodified form

• It is often developed in a public, collaborative manner.

• Wikipedia

Rick Graziani [email protected] 66

Open Source Definition

1. Free Redistribution: the software can be freely given away or sold. 2. Source Code: the source code must either be included or freely

obtainable. (Without source code, making changes or modifications can be impossible.)

3. Derived Works: redistribution of modifications must be allowed. 4. Integrity of The Author's Source Code: licenses may require that

modifications are redistributed only as patches. 5. No Discrimination Against Persons or Groups: no one can be

locked out. 6. No Discrimination Against Fields of Endeavor: commercial users

cannot be excluded. 7. Distribution of License: The rights attached to the program must

apply to all to whom the program is redistributed without the need for execution of an additional license by those parties.

8. License Must Not Be Specific to a Product: the program cannot be licensed only as part of a larger distribution.

9. License Must Not Restrict Other Software: the license cannot insist that any other software it is distributed with must also be open source.

10.License Must Be Technology-Neutral: no click-wrap licenses or other medium-specific ways of accepting the license must be required.

Rick Graziani [email protected] 67

Projects• Apache Software Foundation • Audacity • Blender -- 3d animation • CodePlex • Debian • Drupal -- Content Management System • Eclipse Foundation • Fedora Project • FreeBSD • Freedesktop.org • Free Software Foundation • FUSE ESB, FUSE Message Broker, FUSE

Services Framework and FUSE Mediation Router - Supported versions of Apache projects

• GIMP -- Image Editing similar to photoshop • GNU • Inkscape -- Vector tool similar to illustrator • Java • JBoss • Joomla! -- Content Management System • KnowledgeTree -- Document Management for

Teams and Small to Medium-sized Organizations

• LibreSource • Linux • Macaulay2 -- algebraic geometry and

commutative algebra

• Miranda IM -- multiprotocol IM • Mozilla Foundation • MySQL • NetBSD • OpenBSD • Open-Xchange • Open Market For Internet Content Accessibility • OpenOffice.org -- Word processor, spreadsheet,

presentation tool, database, equation editor • OpenSees -- open system for earthquake

engineering simulation • OpenSuse • Open Solutions Alliance • Open Source Development Labs • Open Source Initiative • Open Source Geospatial Foundation • PHP -- Hypertext preprocessor • Povray -- Ray Tracer • Python • Restore -- RESTORE is an open source project for

heterogeneous system backup and restore. • SAGE -- Magma computer algebra system • Scribus -- Desktop publishing similar to pagemaker • SourceForge -- Repository of open source

software • Subversion (software) -- version control • Synfig -- 2d vector graphic and animation • TYPO3 -- Enterprise Level Content Management

System • ubuntu • Zenoss • Zimbra

Algorithms and Programming Languages

CS 1

Rick Graziani

Cabrillo College

Spring 2011