Download presentation
Presentation is loading. Please wait.
Published byClement Stafford Modified over 9 years ago
1
Lecture 2: Algorithm development BJ Furman 01SEP2012
2
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
3
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
4
Recap from last lecture Mechanical and aerospace engineers use computers widely Method for developing a program
5
Robot Programming Problem Write a program to make a robot follow a square course How do you go about the programming task?
6
Method for Developing a Program 1.Define the problem: SState the problem you are trying to solve in clear and concise terms. 2.List the inputs and the outputs IInputs: information needed to solve the problem OOutputs: 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) BBegin at a high-level first RRefine (subdivide the high-level) steps until they are effectively computable operations. 4.Test the algorithm: cchoose data sets, and verify that your algorithm works! Note: these steps are to be done BEFORE you write any program code!
7
How do you develop an algorithm? Apply concepts of structured programming SSequence SSelection RRepetition Use tools to help you think through the ‘logic’ of the algorithm PPseudocode FFlowchart
8
Structured Programming Sequence Selection IF IF – ELSE SWITCH Repetition WHILE DO – WHILE FOR Flowchart constructs
9
Algorithm What is an algorithm? Well ordered, unambiguous, effectively computable recipe that uses inputs to produce desired outputs
10
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 SSubordinate numbers and/or indentation are used for dependent statements in selection and repetition structures
11
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).
12
Flowcharts - 1 Flowcharts A graphical tool that diagrammatically depicts the steps and structure of an algorithm or program SymbolName/MeaningSymbolMeaning 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
13
Flowchart Constructs - Sequence and Selection from Deitel & Deitel, 6th ed., p. 122 Control Structures
14
Flowchart Constructs - Repetition from Deitel & Deitel, 6th ed., p. 122 Control Structures
15
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
16
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. Start 2. Declare variables: ________ 3. Repeat while number not equal to -1 3.1. get number 3.2. add to sum 4. Display sum
17
Example: D&D 3.15c - 2 1. Start 2. 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 keyboard 3.3. Display number entered 3.4. add to sum 4. Display sum 1. Start 2. Declare variables: ________ 3. Repeat while number not equal to -1 3.1. get number 3.2. add to sum 4. Display sum Develop the algorithm, cont. Refine Are we there yet?
18
Example: D&D 3.15c - 3 1. Start 2. 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 keyboard 3.3. Display number entered 3.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?
19
Flowchart 1. Start 2. 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 keyboard 3.3. Display number entered 3.4. if num less than zero, then 3.4.1 continue 3.5. add to sum 4. Display sum Test the algorithm!
20
Structure of a C Program A formal letter has a structure So does a program in C Burford Furman Professor Dept. of Mech. and Aero. Eng San José State University San 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
21
C Code for D&D 3.15c Programmer’s block Pre-processor directive Declare and initialize variables While loop (repetition structure) Main function (statements go between { } ) return statement
22
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
23
# include (pre-processor directive) Includes a library file for ‘standard io’ functions for things like printing, etc. Full program
24
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
25
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
26
while() Repetition Structure while ( condition ) repeat statements between { } Note the operators = is assignment != is not equal to + is addition
27
Operators Adapted from H. Cheng chap04.ppt, slide 5
28
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 ….
29
Computer Block Diagram Bus CPU Memory Ports USB, Serial, Keyboard, Mouse, etc. Disk controller Video chipset Audio chipset Networking chipset Hard drive, CD/DVD Display Speakers Internet
30
Memory Stores program instructions and data Memory (8-bit) Address 01100110 01010100 00000000 0x10FE 0x10FF 0x1100 0xFFFF Bit 76543210 Each location has an ‘address’ Each location stores the information as ‘bits’ Binary ____its Zero or one 8 bits is one byte Information is ‘coded’ Memory is ‘written’ or ‘read’
31
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
32
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
33
Software The intermediary between you (the user) and the hardware Operating system (OS) see the next page 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.
34
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
35
Not correct Correct Create/Edit source files (your program!) Program Development from Figure 1.11, p. 32 in HK note additions! Compile source files Link compiled files Load executable file Run your program! Test Repeat process
36
Embedded Computers and Microcontrollers http://media.digikey.com/photos/Atmel %20Photos/313-64-TQFP.jpg http://www.oxisso.com/Microcontrollers/At mega128TinyBoard_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/articl eImages/large/12813.jpg ATtiny10 microcontroller, with 1K bytes of programmable Flash memory, and 32 bytes of internal SRAM Back
37
Review Next -->
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.