Algorithms and Programming Languages

advertisement
Algorithms and Programming
Languages
CS 1
Rick Graziani
Cabrillo College
Spring 2011
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 graziani@cabrillo.edu
2
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 graziani@cabrillo.edu
3
Defining the Algorithm
Non-terminating sequence
1 2 3 4 5 6 7 8 9 10 11 … (this could go on for ever!)
Ambiguous
Organize the CDs
(By title? By artist? By genre?)
• 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
Rick Graziani graziani@cabrillo.edu
4
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 graziani@cabrillo.edu
5
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 graziani@cabrillo.edu
6
Algorithm Representation
Op-code
1
2
3
4
5
6
7
8
9
A
B
C
Description
LOAD reg. R from cell XY.
LOAD reg. R with XY.
STORE reg. R at XY.
MOVE R to S.
ADD S and T into R. (2’s comp.)
ADD S and T into R. (floating pt.)
OR S and T into R.
AND S and T into R.
XOR S and T into R.
ROTATE reg. R X times.
JUMP to XY if R = reg. 0.
HALT.
LOAD reg. R from cell XY
• 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.
Rick Graziani graziani@cabrillo.edu
7
Op-code
1
2
3
4
5
6
7
8
9
A
B
C
Operand
Description
LOAD reg. R from cell XY.
LOAD reg. R with XY.
STORE reg. R at XY.
MOVE R to S.
ADD S and T into R. (2’s comp.)
ADD S and T into R. (floating pt.)
OR S and T into R.
AND S and T into R.
XOR S and T into R.
ROTATE reg. R X times.
JUMP to XY if R = reg. 0.
HALT.
Store the bits found in register 5 in main memory cell A7
•
•
•
•
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.
Rick Graziani graziani@cabrillo.edu
8
Pseudocode
Pseudocode
1. Enter two numbers.
2. Add the numbers together.
3. Display the result.
• 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.
Rick Graziani graziani@cabrillo.edu
9
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 graziani@cabrillo.edu
10
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 graziani@cabrillo.edu
11
Repeating structure
Counter
500
501
21
3
4
<End of loop>
Hello
Hello
Hello
Hello
…
Hello
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)
Rick Graziani graziani@cabrillo.edu
12
For loop
Counter
500
501
21
3
4
<End of loop>
Hello
Hello
Hello
Hello
…
Hello
• 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)
Rick Graziani graziani@cabrillo.edu
13
•
“I want you to write on the chalk board, ‘I will not throw
paper airplanes in class’, 500 times.”
Rick Graziani graziani@cabrillo.edu
14
Software Development
Definitions
Software or Program
Instructions that tell the
computer what to do
Programmer
Someone who writes
computer programs
Rick Graziani graziani@cabrillo.edu
16
CPU Instruction Set
Instruction Set
0001
0010
0011
0100
0101
0110
0111
1000
1001
Instruction
Move
Compare
Bit test
Bit clear
Bit set
Add
See group 10
See groups 11, 13, 14
Move byte
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
Rick Graziani graziani@cabrillo.edu
17
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.
Rick Graziani graziani@cabrillo.edu
18
First Generation Languages
(Machine Language)
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
Rick Graziani graziani@cabrillo.edu
19
Second Generation Languages
(Assembly Language)
Op-code
1
2
3
4
5
6
7
8
9
A
B
C
Description
LOAD reg. R from cell XY.
LOAD reg. R with XY.
STORE reg. R at XY.
MOVE R to S.
ADD S and T into R. (2’s comp.)
ADD S and T into R. (floating pt.)
OR S and T into R.
AND S and T into R.
XOR S and T into R.
ROTATE reg. R X times.
JUMP to XY if R = reg. 0.
HALT.
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
Rick Graziani graziani@cabrillo.edu
20
Second Generation Languages
(Assembly Language)
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
Rick Graziani graziani@cabrillo.edu
21
Using a compiler
Rick Graziani graziani@cabrillo.edu
22
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 graziani@cabrillo.edu
23
Third Generation Languages
(High level languages)
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.
Rick Graziani graziani@cabrillo.edu
24
Third Generation Languages
(High level languages)
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's
Object oriented language which is compatible with C.
JAVA
Rick Graziani graziani@cabrillo.edu
25
Third Generation Languages
(High level languages)
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
Rick Graziani graziani@cabrillo.edu
26
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 graziani@cabrillo.edu
27
Writing a Software Program
Task
Write a program to convert binary numbers to decimal
and decimal numbers to binary
Rick Graziani graziani@cabrillo.edu
28
Writing a Software Program
1. Determine what kind of computer you want your
program to run on
Macintosh?
PC?
Rick Graziani graziani@cabrillo.edu
29
Writing a Software Program
2. Determine which operating system this computer
(and the user) will be using
Windows Vista?
Mac OSX?
Linux?
Rick Graziani graziani@cabrillo.edu
30
Writing a Software Program
3. Determine which language you will be programming
in.
C?
C++?
Java?
•
C++
Rick Graziani graziani@cabrillo.edu
31
Writing a Software Program
4. Determine the compiler for your language, operating
system and hardware
Microsoft Visual C++?
Borland C++?
Watkins C++?
•
Microsoft Visual C++
Rick Graziani graziani@cabrillo.edu
32
Writing a Software Program
5. Write the program
Rick Graziani graziani@cabrillo.edu
33
Writing a Software Program
Compile the program into a machine code file and
distribute it to the users via floppy diskette.
Rick Graziani graziani@cabrillo.edu
34
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 graziani@cabrillo.edu
35
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 graziani@cabrillo.edu
36
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
Rick Graziani graziani@cabrillo.edu
37
The Year 2000
What is the big deal?
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
Rick Graziani graziani@cabrillo.edu
38
The Year 2000
What is the big deal?
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.
Rick Graziani graziani@cabrillo.edu
39
Databases and Relationships
Relationships
•
•
•
One-to-One
One-to-Many
Many-to-Many
Rick Graziani graziani@cabrillo.edu
41
One-to-One Relationships
Rick Graziani graziani@cabrillo.edu
42
One-to-Many Relationships
Rick Graziani graziani@cabrillo.edu
43
Many-to-Many
Relationships
(Not
recommended)
Rick Graziani graziani@cabrillo.edu
44
Programming Languages
Part 2
Goal of third generation languages
Op-code
1
2
3
4
5
6
7
8
9
A
B
C
Operand
Description
LOAD reg. R from cell XY.
LOAD reg. R with XY.
STORE reg. R at XY.
MOVE R to S.
ADD S and T into R. (2’s comp.)
ADD S and T into R. (floating pt.)
OR S and T into R.
AND S and T into R.
XOR S and T into R.
ROTATE reg. R X times.
JUMP to XY if R = reg. 0.
HALT.
• 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
Rick Graziani graziani@cabrillo.edu
46
Rick Graziani graziani@cabrillo.edu
47
Rick Graziani graziani@cabrillo.edu
48
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 graziani@cabrillo.edu
49
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 graziani@cabrillo.edu
50
Evolution of Programming Languages
• Lineage does not represent that one language evolved from another,
although this is the case with some languages.
Rick Graziani graziani@cabrillo.edu
51
Evolution of Programming Languages
Rick Graziani graziani@cabrillo.edu
52
Evolution of Programming Languages
Rick Graziani graziani@cabrillo.edu
53
Procedural Languages
Main
Program
Procedure 1
Procedure 2
Procedure 3
•
•
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
Rick Graziani graziani@cabrillo.edu
54
Object Oriented Programming
Left
click
•
•
•
Right
click
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
Rick Graziani graziani@cabrillo.edu
55
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 graziani@cabrillo.edu
56
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 graziani@cabrillo.edu
57
Variables and Data Types
Variable
Integer
125
Float
9.75
Character
d
• 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.
Rick Graziani graziani@cabrillo.edu
58
Programming Concepts
Variable Count which
is type integer
Count
499
500
501
1
2
3
I will not throw paper airplanes in class.
I will not throw paper airplanes in class.
I will not throw paper airplanes in class.
…
I will not throw paper airplanes in class.
I will not throw paper airplanes in class.
Rick Graziani graziani@cabrillo.edu
59
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 graziani@cabrillo.edu
60
Variables
Count
499
500
501
1
2
3
I will not throw paper airplanes in class.
I will not throw paper airplanes in class.
I will not throw paper airplanes in class.
…
I will not throw paper airplanes in class.
I will not throw paper airplanes in class.
• Variables:
– Can be given an initial value within the program
– Value may change from:
• Program instructions
• User input
Rick Graziani graziani@cabrillo.edu
61
Data structure
1 2 3 4 5 …
… 24 25
B o o l o o
char Last_Name[25]
• 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]
Rick Graziani graziani@cabrillo.edu
62
Data structure
Frame
Dan
Sue
Gary
Mary
1
2
3
6
12
0
9
1
7
7
3
4
Integer pins [bowler, frame]
5
6
7
8
9
10
pins [Mary, 2] = 7
• Two dimensional array
– Row and column
– Integer pins [bowler, frame]
Rick Graziani graziani@cabrillo.edu
63
Assigning Value
Integer
125
Float
9.75
Character
•
d
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.
Fahrenheit = (9/5)Celcius + 32
Rick Graziani graziani@cabrillo.edu
64
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 graziani@cabrillo.edu
65
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 graziani@cabrillo.edu
66
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
Rick Graziani graziani@cabrillo.edu
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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
67
Algorithms and Programming
Languages
CS 1
Rick Graziani
Cabrillo College
Spring 2011
Download