Lecture Notes 12005 Pseudocode Pseudocode v Pseudocode standard

  • Slides: 15
Download presentation
Lecture Notes 1/20/05 Pseudocode

Lecture Notes 1/20/05 Pseudocode

Pseudocode v Pseudocode standard which we will follow in this class: - Statements are

Pseudocode v Pseudocode standard which we will follow in this class: - Statements are written in simple English; - Each instruction is written on a separate line; - Each set of instructions is written from top to bottom, with only one entry and one exit; - Groups of statements may be formed into modules, and that group given a name.

How to Write Pseudocode 6 basic computer operations: v receive information v put out

How to Write Pseudocode 6 basic computer operations: v receive information v put out information v perform arithmetic v assign a value to a variable or memory location v compare two variables and select one of two alternative actions v repeat a group of actions

Receive Information A computer generally gets information in one of the following ways: v

Receive Information A computer generally gets information in one of the following ways: v From a file v Pseudocode -> “Read” v From a keyboard v Pseudocode -> “Get”

Receive Information Examples: v Get file name v Read first line from the file

Receive Information Examples: v Get file name v Read first line from the file v Get number of students v Read the names of the students from the file

Put Out Information A computer generally outputs information in one of the three ways:

Put Out Information A computer generally outputs information in one of the three ways: v v v Print on a printer v “Print” Write to a file v “Write” Display on a monitor v “Display”

Put Out Information Examples: v Print “Hello everyone!” v Write “My first homework” to

Put Out Information Examples: v Print “Hello everyone!” v Write “My first homework” to hw_1. txt v Display average_age

Perform Arithmetic Either actual mathematical symbols or words can be used: Multiply Length by

Perform Arithmetic Either actual mathematical symbols or words can be used: Multiply Length by Width to compute Area v Area = Length * Width v

Perform Arithmetic Symbols that will be used in this class: Addition Subtraction Division Multiplication

Perform Arithmetic Symbols that will be used in this class: Addition Subtraction Division Multiplication Modulus Parentheses + / * % () Also can use the words : Compute and Calculate

Example Write the pseudocode for calculating the average of the students in our class.

Example Write the pseudocode for calculating the average of the students in our class. Prompt the user for number of students Get number of students Prompt for all of the ages Get the ages of all students Add all ages together to obtain Total_Age Divide Total_Age by the number of students Display the result

Compare Two Variables and Select One of Two Actions Examples: v v v If

Compare Two Variables and Select One of Two Actions Examples: v v v If it starts to rain, I’ll go inside the building. If the car starts, I’ll drive. Otherwise I’ll take a bus. If you are tall enough, you can go on this ride. Otherwise, you have to wait till next year. Special keywords IF, THEN, ELSE, ENDIF

Compare Two Variables and Select One of Two Actions Example: IF Age > 0

Compare Two Variables and Select One of Two Actions Example: IF Age > 0 THEN Add 1 to number_students ELSE Display “Age must be positive” ENDIF

Repeat a Group of Actions Examples: v v Shake the vending machine until the

Repeat a Group of Actions Examples: v v Shake the vending machine until the candy bar falls down. Wash your hands until they are clean. Special keywords: WHILE and ENDWHILE

Repeat a Group of Actions Example: set Total_Age and Age to zero set number_students

Repeat a Group of Actions Example: set Total_Age and Age to zero set number_students to 0 WHILE number_students < 60 get next student’s age and store it in AGE Total_Age = Total_Age + Age add 1 to number_students ENDWHILE display Total_Age

Homework for Tuesday v Picture. v Provide the defining diagram, solution algorithm and a

Homework for Tuesday v Picture. v Provide the defining diagram, solution algorithm and a desk check for a problem (find in online). v Read Chapter 2 in “C++ Programming”.