Академический Документы
Профессиональный Документы
Культура Документы
Problem Solving
PSPD Using C
Slide 2 of 40
If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams: program pseudocode
flowchart
algorithm
PSPD Using C Slide 3 of 40
PSPD Using C
Slide 4 of 40
By the time you have completed this chapter,you will have acquired the ability to:
Problem Solving
Design
PSPD Using C
Slide 6 of 40
Problem Solving
Design
DEVISING A PLAN
Second: Find the connection between the data and the unknown. Auxiliary problems may be devised if needed. You should obtain eventually a plan of the solution. Have you seen it before? Or have you seen the same problem in slightly different form? Do you know a related problem? Look at the unknown! Try to think of a familiar problem having the same or similar unknown. Split the problem into smaller, simple sub-problems. If you cannot solve the proposed problem try to solve first some related problem. Or solve more general problem. Or special case of the problem. Or solve the part of the problem.
Slide 7 of 40
PSPD Using C
Problem Solving
Design
Third: Carry out your plan. Fourth: Examine the solution obtained.
LOOKING BACK
Can you check the result? Can you derive the result differently? Can you use the result, or the method, for some other problem?
PSPD Using C
Slide 8 of 40
Problem Solving
Design
The software development method The software development method consists of the following steps:
Requirements specification Analysis Design
Implementation
Testing and verification Documentation
PSPD Using C Slide 9 of 40
Algorithmic problem: Any problem whose solution can be expressed as a set of executable instructions. Algorithm: A well defined computational procedure consisting of a set of instructions, that takes some value or set of values, as input, and produces some value or set of values, as output.
PSPD Using C
Slide 10 of 40
Derived from the name of Mohammed alkhowarizmi, a Persian mathematician in the ninth century. Al-khowarizmi--Algorismus(in Latin)--Algorithm An algorithm is like a recipe, that converts the ingredients into some culinary dish. The formal written version is a program. Algorithms/programs are the software.The machine that runs the programs is the hardware.
PSPD Using C
Slide 11 of 40
Ingredient
Recipe (software)
Al-gong Bah-kut-the
PSPD Using C
Slide 12 of 40
Characteristics of an Algorithm
Design
PSPD Using C
Slide 13 of 40
Characteristics of an Algorithm
Design
PSPD Using C
Slide 14 of 40
Characteristics of an Algorithm
Design
An Algorithm
should emphasize the WHATs and not the HOWs. Consider the problem below: PROBLEM:
You are required to design a complete system which will enable the sum of two values to be calculated.
PSPD Using C
Slide 15 of 40
Problem Solving
Design
To grapple with this problem, we have to understand the problem from the human perspective. A question to ask yourself is this, How Would You Calculate the Sum of Two Values?
PSPD Using C
Slide 16 of 40
Problem Solving
Design
As the computer is also a device similar to the way in which the human brain functions, the process of calculating the sum of two values can also be easily performed by the computer.
=
PSPD Using C Slide 17 of 40
Problem Solving
Design
Processing (Brains)
Input Output
PSPD Using C Slide 18 of 40
Problem Solving
Output Device
Design
CPU (Brains)
Input Device
PSPD Using C
Slide 19 of 40
Problem Solving
Design
Processing
5 10 5 + 10 = 15
15
Input
Output
Let us assume we are interested in calculating the sum of 5 and 10.
PSPD Using C Slide 20 of 40
Problem Solving
Design
As shown previously, the example values (5 and 10) have been specified explicitly. As the brain is flexible enough in calculating a wide range of numbers, the two input values have to be generalised.
PSPD Using C
Slide 21 of 40
Problem Solving
Design
Value2 Value1
Sum Notice that instead of using specific numbers, variables are used to represent these values.
PSPD Using C Slide 22 of 40
Variables are memory locations within the computer which allows pieces of data to be stored. The word variable comes from the word vary, which means that whatever you place within a variable can be changed.
Data (for example, name, age, salary) can be stored in these containers.
PSPD Using C Slide 23 of 40
PSPD Using C
Slide 24 of 40
Problem Solving
Design
Now that we have an exact idea about how the problem is solved, let us represent this in a clearer manner, using the defining diagram.
Input
Value1 Value2
Processing
Output
Sum
PSPD Using C
Slide 25 of 40
Problem Solving
Design
The next step is to identify the actual processing steps required to convert the input to become the output. Input
Value1
Value2
Processing
1) Read Value1, Value2
2) Calculate Sum 3) Display Sum
Output
Sum
PSPD Using C
Slide 26 of 40
Algorithm Development
Once the defining diagram has been developed, the next logical step is to develop the algorithm (which is much more detailed).
Design
Input
Value1
Processing
1) Read Value1, Value2
Output
Sum
Value2
2) Calculate Sum
3) Display Sum
Algorithm Development
Design
* / = ()
subtraction
multiplication division assignment brackets for grouping calculations
PSPD Using C
Slide 28 of 40
Algorithm Development
Design
Example of an algorithm (using pseudocodes) which can be used to carry out the tasks outlined in the defining diagram is as follows:1) 2) Read Value1, Value2 Calculate
Display Sum
Slide 29 of 40
Pseudocoding
Design
A Pseudocode language is semiformal, Englishlike language with a limited vocabulary that can be used to design and describe algorithms. The pseudocode language can be used for: Designing algorithms Communicating algorithms as programs Implementing algorithms as programs Debugging logic errors in program
PSPD Using C
Slide 30 of 40
The sequence control structure is a series of steps or statements that are executed in the order in which they are written in an algorithm.
For Example: read taxable income read filing status compute income tax
PSPD Using C
Slide 31 of 40
Contd
Design
PSPD Using C
Slide 32 of 40
Decision Making
Design
Being able to mimic the way the human brain works, the computer also has the ability to make decisions. Decision making can be represented pseudocodes using the IF...THEN construct. IF (expression) THEN : : ENDIF
PSPD Using C Slide 33 of 40
in
Decision Making
Design
The expression is a comparison between two values which evaluates to either true of false.
Decision Making
Design
Example:-
Decision Making
Design
Commonly used relational operators in expressions:> < Greater Than Less Than
=
<> >= <= ()
Equals To
Not Equals To Greater Than or Equals To Less Than or Equals To Brackets used for prioritising certain calculations
PSPD Using C
Slide 36 of 40
Decision Making
Design
Since all expressions works out to be either true or false, what the IF..THEN statement represents is a two-state condition. For example,
A potential employer is waiting for you to give a reply (on the spot) about the job offer with a salary of RM2000. Your decision would be to only take a job worth more than RM4000. What would you say?
IF (Salary>4000) THEN Say YES! ELSE Say NO! ENDIF
PSPD Using C Slide 37 of 40
Decision Making
Design
Certain conditions may give rise to more than one expression being evaluated. These are known as compound expressions. Example:-
You are interested in taking up a job which pays more than RM4000 and that the company must also provide a credit card.
IF (Salary>4000) And (CreditCard=YES) THEN Take Job!! ENDIF
PSPD Using C Slide 38 of 40
Decision Making
Design
Compound expressions can be represented using the following operators:AND Every expression must evaluate to be true in order for the whole expression to be true. As long as any one of the expression can be true, the entire IF statement will be true. The inverse (opposite) of the entire expression.
Slide 39 of 40
OR
NOT
PSPD Using C
Decision Making
Design
IF statements can be nested, that is, placed within another IF statement. This is used in situations when the expression is more complex than the simple decisions (as seen earlier).
PSPD Using C
Slide 40 of 40
Decision Making
For example, this statement.........
IF (Salary>4000) And (CreditCard=YES) THEN Say Yes I Will Take The Job!! ENDIF
Design
Decision Making
For good practice...........
IF (Salary>4000) THEN IF (CreditCard=YES) THEN Say Yes I Will Take The Job!! ELSE Say No Credit Card? Say Sorry!! ENDIF ELSE Say Not Enough Pay!! ENDIF
Design
........ ensure that statements are properly indented to indicate block of statements which belong together.
PSPD Using C Slide 42 of 40
Contd
Design
For Example:
print B is greater
end if
PSPD Using C
Slide 43 of 40
Contd
Design
Repetition Control Structure: The repetition control structure specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. Syntax is: while condition loop-body end-while
PSPD Using C
Slide 44 of 40
Looping Constructs
Design
Looping constructs (also known as repetition or iteration constructs) are a kind of construct found in pseudocodes which allows statements (or a group of statements) to be repeated. The main reason why looping constructs are provided is because most of the problems which we encounter everyday requires some degree of repetition.
PSPD Using C
Slide 45 of 40
Looping Constructs
Design
Payroll processing is very much an iterative process as the person processing the payroll applies the same calculations for each employee to produce the pay slip.
PSPD Using C
Slide 46 of 40
Looping Constructs
Design
PSPD Using C
Slide 47 of 40
Looping Constructs
Design
Looping Constructs
Design
The format of the FOR...NEXT construct is shown below:FOR (initialze TO expression) STEP increment : : : ENDDO
Group of statements
PSPD Using C
Looping Constructs
Design
Looping Constructs
Design
Take a look at the following example:You are required to develop a complete system which will allow the total payroll to be calculated.
The system is required to read in the amount to be paid for each employee.
The moment the system receives an input value of -99, the system is required to stop and display the total payroll.
PSPD Using C Slide 51 of 40
Looping Constructs
Design
Processing
1) Read Salary 2) Calculate Total 3) Display Total
Output
Total
PSPD Using C
Slide 52 of 40
Looping Constructs
Algorithm (Using Pseudocodes)
1) Display "Enter Salary" 2) Read Salary
Design
3) Total = 0
4) DOWHILE (Salary<>-99) Total = Total + Salary Display "Enter Salary" Read Salary ENDDO 5) Display "Total Payroll = ", Total
PSPD Using C Slide 53 of 40
Contd
Design
Example:
Dowhile (income is less than 50000) print Enter taxable income;should be greater than or equal to 50000 read income Enddo
PSPD Using C
Slide 54 of 40
A desk check table is used to verify the correctness of the design. This is to ensure that the program which will eventually be developed is going to produce the answer which is required. The desk check table is developed based on the following steps:-
1) 2) 3)
4)
PSPD Using C
Identify the data sets. Identify the expected results. Trace through the algorithm with the data sets using a trace table. Analyse & compare the results produced in step (3) and the expected results in step (2).
Slide 55 of 40
Processing
1) Read Value1, Value2 2) Calculate Sum
Output
Sum
3) Display Sum
Focus on the input section of the defining diagram and identify some possible values (data sets) which can be used to test the system.
PSPD Using C Slide 56 of 40
Processing
1) Read Value1, Value2 2) Calculate Sum
Output
Sum
3) Display Sum
Focus on the output section of the defining diagram and identify some possible values which the system will produce based on the data sets.
PSPD Using C Slide 57 of 40
Sum
Sum
Sum
Program Flowcharts
Design
As humans are more inclined towards understanding diagrams and pictures rather than words, pseudocodes tends to become tedious to understand if too lengthy.
Program flowcharts, because they are represented graphically, makes understanding easier.
PSPD Using C
Slide 61 of 40
Program Flowcharts
The following are the commonly used symbols for drawing program flowcharts. terminator process decision making
Design
document
input/output
arrowheads
PSPD Using C
connector
Slide 62 of 40
Program Flowcharts
Begin Read Value1, Value2
Design
Display Sum
End
PSPD Using C Slide 63 of 40
Program Flowcharts
Begin
Design
Read Amount
YES
Amount>20.00?
NO
Calculate Actual=Amount
End
PSPD Using C Slide 64 of 40
Flowcharting
Design
Definition:
PSPD Using C
Slide 65 of 40
Sequence Structure
Design
Flowchart:
Statement -1
Statement -2
Statement -n
PSPD Using C
Slide 66 of 40
Selection Structure
Design
Flowchart:
else
else-part end_if
true
then-part
PSPD Using C
Slide 67 of 40
Selection Structure
Design
Pseudocode:
Flowchart:
if condition
then-part end_if
condition
N
true
false
then-part
PSPD Using C
Slide 68 of 40
Repetition Structure
Design
Flowchart:
loop-body
end-while T
condition
F N
loop-body
PSPD Using C
Slide 69 of 40
Summary
Design
Problem Solving the process of transforming the description of a problem to its solution. To Solve complex problems, we use computers as a tool and develop computer programs that give us solutions. A commonly used method for problem solving using computers is the software development method,which consists of six steps.
PSPD Using C
Slide 70 of 40
Summary
Design
1.
The Requirements specification, provides us with a precise definition of the problem. In the analysis phase we identify problem inputs,outputs,special constraints, and formulas and equations to be used. The design phase is concerned with developing an algorithm for the solution of the problem.
2.
3.
PSPD Using C
Slide 71 of 40
Summary
Design
4.
The implementation of an algorithm is a computer program.When executed, it should produce the solution to the problem. Program Verification is the process of ensuring that a program meets user requirements. Program testing, on the other hand, is the process of executing a program to demonstrate its correctness.
5. 6.
7.
Program Documentation facilitates the use of the program,future program maintenance efforts,and program debugging.
PSPD Using C
Slide 72 of 40
Summary
Design
PSPD Using C
Slide 73 of 40
Summary
Design
control structures.They are the sequence,selection and repetition structures. fundamental problem-solving strategy. algorithm.
PSPD Using C
Slide 74 of 40
1. State the difference between the Dowhile Enddo structure and the Repeat Until structure. 2. Write an algorithm that will display the first hundred even numbers using the Do-While loop.
PSPD Using C
Slide 75 of 40
Follow Up Assignment
Design
Your source code will be discussed at the end of the next lesson.
PSPD Using C
Slide 76 of 40
Basic control structures The sequence structure The selection structure The repetition structure
PSPD Using C
Slide 77 of 40