37
Lecture 2: Algorithm development BJ Furman 01SEP2012

Lecture 2: Algorithm development BJ Furman 01SEP2012

Embed Size (px)

Citation preview

Page 1: Lecture 2: Algorithm development BJ Furman 01SEP2012

Lecture 2: Algorithm development

BJ Furman

01SEP2012

Page 2: Lecture 2: Algorithm development BJ Furman 01SEP2012

The Plan for Today Recap important points from lecture 1 Problem solving process in developing a program Algorithms, pseudocode, and flowcharts Form of a C program Data types and variables

Page 3: Lecture 2: Algorithm development BJ Furman 01SEP2012

Learning Objectives

By the end of the lecture you should be able to: Describe the process of how to develop a program Explain what is meant by ‘structured programming’ Apply pseudocode and flowcharts in program

development Describe the structure of a C program List and describe the important data types Declare a variable List and describe operators in C Apply concepts for formatted output

Page 4: Lecture 2: Algorithm development BJ Furman 01SEP2012

Recap from last lecture

Mechanical and aerospace engineers use computers widely

Method for developing a program

Page 5: Lecture 2: Algorithm development BJ Furman 01SEP2012

Robot Programming Problem

Write a program to make a robot follow a square course

How do you go about the programming task?

Page 6: Lecture 2: Algorithm development BJ Furman 01SEP2012

Method for Developing a Program

1. Define the problem: State the problem you are trying to solve in clear and

concise terms.

2. List the inputs and the outputs Inputs: information needed to solve the problem Outputs: what the algorithm will produce as a result

3. Describe the steps needed to convert or manipulate the inputs to produce the outputs (develop the algorithm) Begin at a high-level first Refine (subdivide the high-level) steps until they are

effectively computable operations.

4. Test the algorithm: choose data sets, and verify that your algorithm works!

Note: these steps are to be done BEFORE you write any program code!

Page 7: Lecture 2: Algorithm development BJ Furman 01SEP2012

How do you develop an algorithm?

Apply concepts of structured programming Sequence Selection Repetition

Use tools to help you think through the ‘logic’ of the algorithm Pseudocode Flowchart

Page 8: Lecture 2: Algorithm development BJ Furman 01SEP2012

Structured Programming

Sequence Selection

IF IF – ELSE SWITCH

Repetition WHILE DO – WHILE FOR

Flowchart constructs

Page 9: Lecture 2: Algorithm development BJ Furman 01SEP2012

Algorithm

What is an algorithm? Well ordered, unambiguous, effectively

computable recipe that uses inputs to produce desired outputs

Page 10: Lecture 2: Algorithm development BJ Furman 01SEP2012

Pseudocode natural language-like statements that precisely

describe the steps of an algorithm Statements that describe actions Focuses on the logic of the algorithm Avoids language-specific elements Written at a level so that code can be generated

almost automatically from each statement Steps are numbered

Subordinate numbers and/or indentation are used for dependent statements in selection and repetition structures

Page 11: Lecture 2: Algorithm development BJ Furman 01SEP2012

Pseudocode example

Note: English-like statements that describe the actions of the algorithm, and indented to show the logical structure of the algorithm.

MS Word ‘multi-level list works really well for writing pseudocode. (example in MS Word)

See the handout, “Notes on Algorithms, Pseudocode, and Flowcharts” on the ME 30 website (Handouts link).

Page 12: Lecture 2: Algorithm development BJ Furman 01SEP2012

Flowcharts - 1

Flowcharts A graphical tool that diagrammatically depicts

the steps and structure of an algorithm or program Symbol Name/Meaning Symbol Meaning

Process – Any type of internal operation: data transformation, data movement, logic operation, etc.

Connector – connects sections of the flowchart, so that the diagram can maintain a smooth, linear flow

Input/Output – input or output of data

Terminal – indicates start or end of the program or algorithm

Decision – evaluates a condition or statement and branches depending on whether the evaluation is true or false

Flow lines – arrows that indicate the direction of the progression of the program

Page 13: Lecture 2: Algorithm development BJ Furman 01SEP2012

Flowchart Constructs - Sequence and Selection

from Deitel & Deitel, 6th ed., p. 122 Control Structures

Page 14: Lecture 2: Algorithm development BJ Furman 01SEP2012

Flowchart Constructs - Repetition

from Deitel & Deitel, 6th ed., p. 122

Control Structures

Page 15: Lecture 2: Algorithm development BJ Furman 01SEP2012

Example: D&D 3.15c - 1 “Obtain a series of positive numbers from the keyboard, and

determine and display their sum. Assume that the user types the sentinel value -1 to indicate "end of data entry"

Define the problem Statement pretty well defines the problem

List inputs and outputs inputs: number entered from keyboard outputs: sum of numbers

Page 16: Lecture 2: Algorithm development BJ Furman 01SEP2012

Example: D&D 3.15c - 1 “Obtain a series of positive numbers from the keyboard, and

determine and display their sum. Assume that the user types the sentinel value -1 to indicate "end of data entry"

Develop the algorithm High-level first, then refine:

Does this work?

1. Start2. Declare variables: ________3. Repeat while number not equal to -1

3.1. get number3.2. add to sum

4. Display sum

Page 17: Lecture 2: Algorithm development BJ Furman 01SEP2012

Example: D&D 3.15c - 2

1. Start2. Declare variables: num, sum

3. while num not equal to -1, continue doing:3.1. Display prompt “Enter positive number”3.2. Read number from the keyboard3.3. Display number entered3.4. add to sum

4. Display sum

1. Start2. Declare variables: ________3. Repeat while number not equal to -1

3.1. get number3.2. add to sum

4. Display sum

Develop the algorithm, cont.

Refine

Are we there yet?

Page 18: Lecture 2: Algorithm development BJ Furman 01SEP2012

Example: D&D 3.15c - 3

1. Start2. Declare variables: num, sum

3. while num not equal to -1, continue doing:3.1. Display prompt “Enter positive number”3.2. Read number from the keyboard3.3. Display number entered3.4. if num less than zero, then

3.4.1 continue 3.5. add to sum

4. Display sum

Develop the algorithm, cont. Add a test to exclude negative numbers

Are we there now?

Page 19: Lecture 2: Algorithm development BJ Furman 01SEP2012

Flowchart Start

Declare variables: num, sumIntialize variables: num = 0, sum = 0

Display "Enterpositive integer"if num ! = -1

if num < 0

Read num fromkeyboard

Display num

Yes

Yes

sum = sum + num

Display sum

Stop

No

No

1. Start2. Declare variables: num, sum

3. while num not equal to -1, continue doing:3.1. Display prompt “Enter positive number”3.2. Read number from the keyboard3.3. Display number entered3.4. if num less than zero, then

3.4.1 continue 3.5. add to sum

4. Display sum

Test the algorithm!

Page 20: Lecture 2: Algorithm development BJ Furman 01SEP2012

Structure of a C Program A formal letter has a

structure So does a program in C

Burford FurmanProfessorDept. of Mech. and Aero. EngSan José State UniversitySan Jose, CA 95192-0087

July 20, 2009

Dear Prof. Furman,

I’m writing you to see if I can get into ME 30……

Sincerely,

Jane Student

Title block

Date

Salutation

Body

Closing

Signature

Page 21: Lecture 2: Algorithm development BJ Furman 01SEP2012

C Code for D&D 3.15cProgrammer’s block

Pre-processor directive

Declare and initialize variables

While loop(repetition structure)

Main function (statements go between { } )

return statement

Page 22: Lecture 2: Algorithm development BJ Furman 01SEP2012

Programmer’s Block Include important information (comments)

to document the program: Title Date Author Description Inputs/Outputs Algorithm Revision history

Add comments using one of two methods:

1. /* put comment between */ (note: traditional C)

2. // comment (note: single line only)

Full program

Page 23: Lecture 2: Algorithm development BJ Furman 01SEP2012

# include (pre-processor directive)

Includes a library file for ‘standard io’ functions for things like printing, etc.

Full program

Page 24: Lecture 2: Algorithm development BJ Furman 01SEP2012

main() function

Full program

Your program needs a main() function Statements go between

the braces { } main() ends with the return keyword and usually the value zero

If main() runs successfully, it returns a value of zero

Page 25: Lecture 2: Algorithm development BJ Furman 01SEP2012

Declare and initialize variables Variables must be declared before you can use them

Full program

See the handout, “Notes on variable names” on the ME 30 website under Handouts

Page 26: Lecture 2: Algorithm development BJ Furman 01SEP2012

while() Repetition Structure while (condition)

repeat statements between { }

Note the operators = is assignment != is not equal to + is addition

Page 27: Lecture 2: Algorithm development BJ Furman 01SEP2012

Operators Operations Associativity

::

() [] left to right

Function_name() right to left

. -> left to right

‘ ! ` ++ -- + - * &(type) sizeof

right to left

* / % .* ./ left to right

+ - left to right

<< >> left to right

< <= > >= left to right

== != left to right

& left to right

^ left to right

| left to right

&& left to right

^^ left to right

|| left to right

?: right to left

= += -= *= /= %= |= <<= >>=

right to left

, left to right

Adapted from H. Cheng chap04.ppt, slide 5

Page 28: Lecture 2: Algorithm development BJ Furman 01SEP2012

What do computers actually do?

Perform arithmetic operations Addition, subtraction, multiplication, division

Compare two values And decide among alternative courses of action

If a > b, then do action c

Move data around internally (memory and peripherals)

Input data (keyboard, mouse, sensors, etc.) Output data (display, I/O ports, etc.)

And do all of this really fast ….

Page 29: Lecture 2: Algorithm development BJ Furman 01SEP2012

Computer Block Diagram

Bus

CPU

Memory

Ports

USB,Serial,

Keyboard,Mouse, etc.

Diskcontroller

Videochipset

Audiochipset

Networkingchipset

Hard drive,CD/DVD

Display

Speakers

Internet

Page 30: Lecture 2: Algorithm development BJ Furman 01SEP2012

Memory

Stores program instructions and data Memory (8-bit)Address

0 1 1 0 0 1 1 0

0 1 0 1 0 1 0 0

0 0 0 0 0 0 0 0

0x10FE

0x10FF

0x1100

0xFFFF

Bit 7 6 5 4 3 2 1 0

Each location hasan ‘address’

Each location storesthe information as ‘bits’ Binary ____its

Zero or one

8 bits is one byte Information is ‘coded’ Memory is ‘written’ or ‘read’

Page 31: Lecture 2: Algorithm development BJ Furman 01SEP2012

CPU The ‘brain’ of your computer

Carries out the instructions of your program

Essential components: Arithmetic Logic Unit (ALU)

Does arithmetic and logic functions Add and subtract (sometimes

multiply and divide) Bit-wise logic:

AND, OR, NOT, XOR Bit shift (left or right)

Control Unit (CU) Controls the actions inside the CPU

Registers Temporary locations to store data,

instructions, and addresses Clock

Synchronizes operations in the CPU

Adapted from Fundamentals of Computer Organization and Architecture, M. Abd-El-Barr, H. El-Rewini, John Wiley and Sons, 2005

CPU

ALU

Registers

CU

Clock

Data

Memory

Instructions

Page 32: Lecture 2: Algorithm development BJ Furman 01SEP2012

Ports Connection to the

external world USB Serial Keyboard Monitor Pins on a

microcontroller Voltage level

determines whether a 0 or a 1

Ex. 5 V logic: < 1.5 V = 0 > 3.5 V = 1

http://media.digikey.com/photos/Atmel%20Photos/453-64-TQFP.jpg

http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf

Page 33: Lecture 2: Algorithm development BJ Furman 01SEP2012

Software

The intermediary between you (the user) and the hardware Operating system (OS) see the next page

Windows, OS X, Linux Application programs

End-user applications Word processor, solid modeler, etc. Mathcad, Matlab, etc.

Application development software (programming languages)

C, Matlab (sort of), Lab View (sort of), Python, Java, FORTRAN, etc.

Page 34: Lecture 2: Algorithm development BJ Furman 01SEP2012

Operating System (OS) A program that:

Acts as an intermediary between hardware and application software

Provides a consistent, stable way for applications to interact with hardware APIs, so you don’t have to do it all

yourself

Examples: Windows XP/Vista Linux OS X

http://en.wikipedia.org/wiki/File:Operating_system_placement.svg

Page 35: Lecture 2: Algorithm development BJ Furman 01SEP2012

Not correct

Correct

Create/Editsource files(your program!)

Program Developmentfrom Figure 1.11, p. 32 in HK

note additions!

Compilesource files

Linkcompiled files

Loadexecutable file

Runyour program!

TestRepeat process

Page 36: Lecture 2: Algorithm development BJ Furman 01SEP2012

Embedded Computers and Microcontrollers

http://media.digikey.com/photos/Atmel%20Photos/313-64-TQFP.jpg

http://www.oxisso.com/Microcontrollers/Atmega128TinyBoard_Show.jpg

http://media.digikey.com/photos/Atmel%20Photos/453-64-TQFP.jpg

Microcontrollers

Single-board computer

http://www.embeddedsys.com/subpages/products/images/pdf/microsys_sbc1586_datasheet.pdf

http://www.electropages.com/articleImages/large/12813.jpg

ATtiny10 microcontroller, with 1K bytes of programmable Flash memory, and 32 bytes of internal SRAM

Back

Page 37: Lecture 2: Algorithm development BJ Furman 01SEP2012

Review

Next -->