Lecture Notes 8/24/04 (part 2)
Pseudocode (review) 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: receive information put out information perform arithmetic assign a value to a variable or memory location compare two variables and select one of two alternative actions repeat a group of actions
Receive Information A computer generally gets information in one of the following ways: From a file Pseudocode -> “Read” From a keyboard Pseudocode -> “Get”
Receive Information Examples: Get file name Read first line from the file Get number of students Read the names of the students from the file
Put Out Information A computer generally outputs information in one of the three ways: Print on a printer “Print” Write to a file “Write” Display on a monitor “Display”
Put Out Information Examples: Print “Hello everyone!” Write “My first homework” to hw_1.txt Display average_age
Perform Arithmetic Either actual mathematical symbols or words can be used: Multiply Length by Width to compute Area Area = Length * Width
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 age 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