200 likes | 671 Views
3/28/2012. Instructor: Damita Elliott Department of Computer Science. 2. Lecture Objectives. Six basic computer operationsWords and keywords used when writing pseudocodeStructure Theorem using pseudocodeDeveloping an algorithmDefining the problemDesigning a solution algorithmChecking the solu
E N D
1. CS241 Structured Programming and The “C” LanguageLecture 3PseudocodeDeveloping an Algorithm Instructor: Damita Elliott
Office: NSC 113
Phone: (713) 313-7991
Email: tsudye@sbcglobal.net
Webpage: http://itscience.tsu.edu/elliott
Department of Computer Science
Texas Southern University, Houston
2. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 2 Lecture Objectives Six basic computer operations
Words and keywords used when writing pseudocode
Structure Theorem using pseudocode
Developing an algorithm
Defining the problem
Designing a solution algorithm
Checking the solution algorithm
3. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 3 Six basic computer operations A computer can receive information
A computer can put out information
A computer can perform arithmetic
A computer can assign a value to a variable or memory location
A computer can compare two variables and select one of two alternative actions
A computer can repeat a group of actions These computer operations can be represented using words and phrases using pseudocode.These computer operations can be represented using words and phrases using pseudocode.
4. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 4 1. A computer can receive information Computers receive information or input from various sources (keyboard, disk, mouse).
Common pseudocode instructions for INPUT are: Read, Get
Read student name
Get tax_code
Read number_1, number_2
* Use a single verb followed by one or more nouns to indicate what data to be obtained.* Use a single verb followed by one or more nouns to indicate what data to be obtained.
5. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 5 2. A computer can put out information Computers display information or output to a device such as a printer, display monitor, or file.
Common pseudocode instructions for OUTPUT are: Print, Write, Put, Output, Display
Print ‘Program Completed’
Write customer record to master file
Put out name and address
Output total_tax
Display ‘End of data’
6. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 6 3. A computer can perform arithmetic Computers can perform mathematical calculations or execute formulas.
Common pseudocode symbols for arithmetic are:
+ for Add
- for Subtract
* for Multiply
/ for Divide
() for Parentheses Computers were designed to perform mathematical calculations.Computers were designed to perform mathematical calculations.
7. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 7 3. A computer can perform arithmetic (cont) Common pseudocode words for arithmetic symbols are:
Add, Subtract, Multiply, and Divide
Common Pseudocode instructions when performing calculations are:
bill = price + tax
average = total_sum divided by 2
Compute C= (F-32) * 5/9
Calculate the total number of list prices
Computers were designed to perform mathematical calculations.
The equal sign represents the assignment of some value as a result of some calculation.
Remember the order of precedence.Computers were designed to perform mathematical calculations.
The equal sign represents the assignment of some value as a result of some calculation.
Remember the order of precedence.
8. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 8 4. A computer can assign a value to a variable or memory location Computers can store information.
Common pseudocode instructions for assigning a value to a variable or memory location are:
Initialize total to zero
Set total_amout to 0
total_price = cost_price + sales_tax
Store temp_password in new_password The equal sign represents the assignment of some value as a result of some calculation.
The equal sign represents the assignment of some value as a result of some calculation.
9. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 9 5. A computer can compare two variables and choose one of two alternative actions Computers can make decisions
Psuedocode uses the keywords:
IF, THEN, ELSE and ENDIF for decisions IF condition p is TRUE THEN
execute statement(s) in true case
ENDIF
IF condition p is TRUE THEN
execute statement(s) in true case
ELSE
execute statement(s) in false case
ENDIF
IF condition p is TRUE THEN
execute statement(s) in true case
ENDIF
IF condition p is TRUE THEN
execute statement(s) in true case
ELSE
execute statement(s) in false case
ENDIF
10. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 10 6. A computer can repeat a group of actions A sequence of statements are executed repeatedly until a a terminating condition occurs.
Written in pseudocode as:
DOWHILE/ENDDO
WHILE/ENDWHILE WHILE there are uncounted students DO
Count the next student
ENDWHILE
Set student_total to zero
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDOWHILE there are uncounted students DO
Count the next student
ENDWHILE
Set student_total to zero
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDO
11. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 11 Structure Theorem States: That is possible to design a program based upon three control structures:
Sequence
Selection, and
Repetition
12. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 12 Developing an Algorithm Before we start designing an algorithm we must first ensure that we have a thorough understanding of the _______.
13. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 13 Defining the Problem Most difficult and most important part of a programming project.
Writing down the problem is the first step in the program documentation process.
To assist in the understanding of the problem definition, divide the problem into
Input
Process
Output
14. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 14 IPO Chart The three separate components (input, process, output) can be represented by using a tool called the IPO Chart.
Inputs - are information/data that you obtain to solve the problem
Processes - is the steps taken to produce the desired output from the input data
Outputs - are the goal of the problem solutionInputs - are information/data that you obtain to solve the problem
Processes - is the steps taken to produce the desired output from the input data
Outputs - are the goal of the problem solution
15. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 15 Meaningful names Choose meaningful names when defining variable and constant names.
No limit to the length of the name.
The name should represent what is stored in it.
Example
avg_temp, number_1, mowing_time
A variable is just a name representing a memory location in the computer.A variable is just a name representing a memory location in the computer.
16. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 16 Meaningful names (cont) The language used to represent the processing steps should include a verb followed by one or more nouns.
Example:
Read three numbers
Add numbers together
17. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 17 Example use of the IPO Chart Problem: A program is required to read three numbers, add them together, and print their total.
Prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the simple average temperature, calculated by (maximum temperature + minimum temperature) / 2. Inputs and outputs can be found by looking for nouns and adjectives.
Processing steps can be found by looking for verbs and adverbs
Inputs and outputs can be found by looking for nouns and adjectives.
Processing steps can be found by looking for verbs and adverbs
18. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 18 Designing a solution algorithm When you take a closer look at the IPO Chart, you realize that the steps identified in the processing column is the algorithm.
You write the algorithm by writing the processes steps of the IPO chart in pseudocode. Use the examples of the previous two IPO chart examples.Use the examples of the previous two IPO chart examples.
19. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 19 Checking the Solution Algorithm Desk checking is the process of checking the algorithm for correctness.
This is done by someone who executes each step in the algorithm and keeping track of the results.
Best that a different person checks the algorithm.
20. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 20 Steps in desk checking Choose some sample test data. How many sets will be needed depends on the algorithm.
Use some manual method to calculate the correct answers. These results are called the Expected Results.
Make a table of relevant variable names used by the algorithm.
Walk the first set of test data through the algorithm by carefully following algorithm instructions, one by one.
Repeat the walk through for other test data cases.
21. 3/28/2012 Instructor: Damita Elliott Department of Computer Science 21