Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Algorithms and Programming Languages CS 1 Rick Graziani Cabrillo College Spring 2011."— Presentation transcript:

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

2 Rick Graziani graziani@cabrillo.edu2 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

3 Rick Graziani graziani@cabrillo.edu3 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

4 Rick Graziani graziani@cabrillo.edu4 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?)

5 Rick Graziani graziani@cabrillo.edu5 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

6 Rick Graziani graziani@cabrillo.edu6 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

7 Rick Graziani graziani@cabrillo.edu7 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 1LOAD reg. R from cell XY. 2LOAD reg. R with XY. 3STORE reg. R at XY. 4MOVE R to S. 5ADD S and T into R. (2’s comp.) 6ADD S and T into R. (floating pt.) 7OR S and T into R. 8AND S and T into R. 9XOR S and T into R. AROTATE reg. R X times. BJUMP to XY if R = reg. 0. CHALT.

8 Rick Graziani graziani@cabrillo.edu8 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 OperandDescription 1LOAD reg. R from cell XY. 2LOAD reg. R with XY. 3STORE reg. R at XY. 4MOVE R to S. 5ADD S and T into R. (2’s comp.) 6ADD S and T into R. (floating pt.) 7OR S and T into R. 8AND S and T into R. 9XOR S and T into R. AROTATE reg. R X times. BJUMP to XY if R = reg. 0. CHALT.

9 Rick Graziani graziani@cabrillo.edu9 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.

10 Rick Graziani graziani@cabrillo.edu10 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)

11 Rick Graziani graziani@cabrillo.edu11 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)

12 Rick Graziani graziani@cabrillo.edu12 Repeating structure Task: Write Hello 500 times. Pseudocode Counter = 1 While counter is less than or equal to 500, write the word “Hello” and add 1 to Counter. Programming Code Counter = 1 While (counter <= 500) do (print “Hello”; Counter  Counter + 1) Counter 1 Hello 2 3 4 500501 Hello …

13 Rick Graziani graziani@cabrillo.edu13 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 = 1 For (Counter <= 500) do (print the message “Hello”; Counter  Counter + 1) Counter 1 Hello 2 3 4 500501 Hello …

14 Rick Graziani graziani@cabrillo.edu14 “I want you to write on the chalk board, ‘I will not throw paper airplanes in class’, 500 times.”

15 Software Development

16 Rick Graziani graziani@cabrillo.edu16 Definitions Software or Program Instructions that tell the computer what to do Programmer Someone who writes computer programs

17 Rick Graziani graziani@cabrillo.edu17 Instruction Set A 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

18 Rick Graziani graziani@cabrillo.edu18 First Generation Languages (Machine Language) Programming computers using the CPU’s instruction set Also known as Machine Language (timeline) Machine Code File A software file which contains the instructions from the CPU’s instruction set.

19 Rick Graziani graziani@cabrillo.edu19 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)

20 Rick Graziani graziani@cabrillo.edu20 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 1LOAD reg. R from cell XY. 2LOAD reg. R with XY. 3STORE reg. R at XY. 4MOVE R to S. 5ADD S and T into R. (2’s comp.) 6ADD S and T into R. (floating pt.) 7OR S and T into R. 8AND S and T into R. 9XOR S and T into R. AROTATE reg. R X times. BJUMP to XY if R = reg. 0. CHALT.

21 Rick Graziani graziani@cabrillo.edu21 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)

22 Rick Graziani graziani@cabrillo.edu22 Using a compiler

23 Rick Graziani graziani@cabrillo.edu23 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.

24 Rick Graziani graziani@cabrillo.edu24 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)

25 Rick Graziani graziani@cabrillo.edu25 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 main() { printf("Hello World!"); } C++ (pronounced "C plus plus") - 1980's Object oriented language which is compatible with C. JAVA Third Generation Languages (High level languages)

26 Rick Graziani graziani@cabrillo.edu26 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)

27 Rick Graziani graziani@cabrillo.edu27 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

28 Rick Graziani graziani@cabrillo.edu28 Task Write a program to convert binary numbers to decimal and decimal numbers to binary Writing a Software Program

29 Rick Graziani graziani@cabrillo.edu29 1. Determine what kind of computer you want your program to run on Macintosh? PC? Writing a Software Program

30 Rick Graziani graziani@cabrillo.edu30 2. Determine which operating system this computer (and the user) will be using Windows Vista? Mac OSX? Linux? Writing a Software Program

31 Rick Graziani graziani@cabrillo.edu31 3. Determine which language you will be programming in. C? C++? Java? C++ Writing a Software Program

32 Rick Graziani graziani@cabrillo.edu32 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

33 Rick Graziani graziani@cabrillo.edu33 5. Write the program Writing a Software Program

34 Rick Graziani graziani@cabrillo.edu34 Compile the program into a machine code file and distribute it to the users via floppy diskette. Writing a Software Program

35 Rick Graziani graziani@cabrillo.edu35 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

36 Rick Graziani graziani@cabrillo.edu36 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.

37 Rick Graziani graziani@cabrillo.edu37 The Year 2000 What 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

38 Rick Graziani graziani@cabrillo.edu38 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 2000 What is the big deal?

39 Rick Graziani graziani@cabrillo.edu39 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 2000 What is the big deal?

40 Databases and Relationships

41 Rick Graziani graziani@cabrillo.edu41 Relationships One-to-One One-to-Many Many-to-Many

42 Rick Graziani graziani@cabrillo.edu42 One-to-One Relationships

43 Rick Graziani graziani@cabrillo.edu43 One-to-Many Relationships

44 Rick Graziani graziani@cabrillo.edu44 Many-to-Many Relationships (Not recommended)

45 Programming Languages Part 2

46 Rick Graziani graziani@cabrillo.edu46 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 OperandDescription 1LOAD reg. R from cell XY. 2LOAD reg. R with XY. 3STORE reg. R at XY. 4MOVE R to S. 5ADD S and T into R. (2’s comp.) 6ADD S and T into R. (floating pt.) 7OR S and T into R. 8AND S and T into R. 9XOR S and T into R. AROTATE reg. R X times. BJUMP to XY if R = reg. 0. CHALT.

47 Rick Graziani graziani@cabrillo.edu47

48 Rick Graziani graziani@cabrillo.edu48

49 Rick Graziani graziani@cabrillo.edu49 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

50 Rick Graziani graziani@cabrillo.edu50 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

51 Rick Graziani graziani@cabrillo.edu51 Evolution of Programming Languages Lineage does not represent that one language evolved from another, although this is the case with some languages.

52 Rick Graziani graziani@cabrillo.edu52 Evolution of Programming Languages

53 Rick Graziani graziani@cabrillo.edu53 Evolution of Programming Languages

54 Rick Graziani graziani@cabrillo.edu54 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

55 Rick Graziani graziani@cabrillo.edu55 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

56 Rick Graziani graziani@cabrillo.edu56 Other types of languages Other types of languages include: –Functional languages: LISP, Scheme –Declarative languages: Prolog –Scripting languages: Perl, PHP, shell scripts, HTML

57 Rick Graziani graziani@cabrillo.edu57 Programming Concepts Program consists of: –Declarative statements Variables and data types –Imperative statements Procedures, instructions, and algorithms –Comments Enhance readability Used throughout the program

58 Rick Graziani graziani@cabrillo.edu58 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

59 Rick Graziani graziani@cabrillo.edu59 Programming Concepts Count 1 I will not throw paper airplanes in class. 2 3 … 499 I will not throw paper airplanes in class. 500 I will not throw paper airplanes in class. 501 Variable Count which is type integer

60 Rick Graziani graziani@cabrillo.edu60 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

61 Rick Graziani graziani@cabrillo.edu61 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 3 … 499 I will not throw paper airplanes in class. 500 I will not throw paper airplanes in class. 501

62 Rick Graziani graziani@cabrillo.edu62 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]

63 Rick Graziani graziani@cabrillo.edu63 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] Frame 1 2 345678910 pins [Mary, 2] = 7

64 Rick Graziani graziani@cabrillo.edu64 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

65 Rick Graziani graziani@cabrillo.edu65 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

66 Rick Graziani graziani@cabrillo.edu66 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.

67 Rick Graziani graziani@cabrillo.edu67 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

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


Download ppt "Algorithms and Programming Languages CS 1 Rick Graziani Cabrillo College Spring 2011."

Similar presentations


Ads by Google