KEMBAR78
Problem Solving | PDF | Algorithms | Computer Science
0% found this document useful (0 votes)
12 views89 pages

Problem Solving

Uploaded by

AbdulPlayz dxb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views89 pages

Problem Solving

Uploaded by

AbdulPlayz dxb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

7.

1 Development life cycle


Program Development Life Cycle
• Program development is the process of creating application programs.

• There are 5 stages in program development

➢ Analysis

➢ Design

➢ Coding

➢ Testing

➢ Maintenance (not covered in this chapter)


Analysis
• To solve a problem clear requirement specifications need to be set out.
• Analysis stage uses abstraction and decomposition tools to identify what exactly
is required from the program.
• Abstraction is the process of filtering out the key element required for the solution
to the problem and discard unnecessary details.
• Eg: To get coffee from a coffee machine, we only need to know how to turn on/off
the machine.
• Decomposition breaks down a complex problem into smaller parts, which can
then be subdivided into even smaller parts.
Design
• The specification from analysis stage is used to show how the program should
be developed.

• Determine what your software needs, how it will look, and what the timeline for
development is going to be.
Coding and iterative testing
• The program or set of program is developed.

• Each module of the program is written using a suitable programming language.

• The program is tested to see if it works.

• Iterative testing means that modular tests are conducted, code amended, and

test repeated until the module performs as required.


Testing
• The completed program or set of programs is run many times with different sets
of test data.

• This ensures that all the tasks completed work together as specified.
7.2 Algorithms
Linear Search

• A search is used to check if a value is stored in a list

• Linear search inspects each item in the list to see if the item matches the value
searched for.
Linear Search in Pseudocode

Searching for a name in a class list of 5 student names, where all the names are
different
Bubble Sort

Bubble sort is a sorting algorithm that compares two adjacent elements and swaps
them until they are not in the intended order.
Steps/Passes in Bubble Sort
Bubble Sort in Pseudocode

Sort a list of ten temperatures stored in the array, Temperature[], into ascending
order.
7.3 Standard methods
Computer System, Sub-systems, Decomposition
• Computer system is made up of software, data, hardware, communication and people.

• Each computer system can be divided into subsystems and each sub- system can be further divided into
subsystems until each sub-system just perform a single action.

• Eg: Alarm app on your smartphone is a very small computer system


How does a computer system work?
• A computer system works by dividing into subsystems.

• The division is often shown by using top-down design to produce the structure diagram

• Each sub-system can be developed by a programmer as a subroutine.

• Flowcharts or pseudocode can be used to show how a subroutine works.


Top down Design
Decomposition of a computer system into a set of subsystems, then breaking each sub- system down into
a set of smaller sub-systems, until each sub-system just performs a single action.
Top down Design
• Works for the development of both large and small computer systems.

• Several programmers can work independently to develop and test different sub- systems for the same
system at the same time.

• This reduces the development and testing time.


Decomposing a problem
• Decomposition is when we break a problem down into smaller parts to make it easier to handle.

• Any problem that uses a computer system for its solution needs to be decomposed into its component
parts.
Decomposing a problem
The component parts of any computer system are:

1. inputs – the data used by the system that needs to be entered while the system is active.

2. processes – the tasks that need to be performed using the input data and any other previously stored
data.

3. outputs – information that needs to be displayed or printed for the users of the system.

4. storage – data that needs to be stored in files on an appropriate medium for use in the future.
Decomposing a problem - An alarm app
The alarm app can be decomposed into:

1. inputs – time to set the alarm, remove a previously set alarm time, switch an alarm off, press snooze
button

2. processes – continuously check if the current time matches an alarm time that has been set, storage
and removal of alarm times, management of snooze

3. outputs – continuous sound/tune (at alarm time or after snooze time expired)

4. storage – time(s) for alarms set.


Methods
• Solutions to problems need to be designed and developed rigorously.

• The use of formal methods enables the process to be clearly shown for others to understand the
proposed solution

• Structure diagram

• Flowchart

• Pseudocode
Structure Diagrams
• Used to show top-down design in a diagrammatic form.

• Shows the steps needed to solve a problem.

• Hierarchical, showing how a computer system solution can be divided into sub- systems with each level
giving a more detailed breakdown.

• Each sub-system can be further divided.

• When reading a structure diagram start at the top and work towards the bottom.

• If a level contains more than one branch, read from left to right before moving to the next level.
Alarm app for a smartphone
Flowchart
• Diagrammatically shows the steps required to complete a task and the order that they are to be
performed.

• These steps, together with the order is called algorithm.

• effective way to communicate how the algorithm that makes up a system or sub-system works.

• Flowcharts are drawn using standard flowchart symbols.


Example - Checking for the alarm time
Flowchart Symbols
Begin/End
Terminator symbols are used at the beginning and end of each flowchart
Process
• Process symbol are used to show actions.

• Eg: Assign values to variables.

• If a process has been defined elsewhere then the name of that process is shown.
Input/output
The same flowchart symbol is used to show the input of data and output of information
Decision
• Used to decide which action is to be taken next.

• These are used for selection and repetition/ iteration.

• There are always two outputs from a decision flowchart symbol.


Flow lines
• Flow lines use arrows to show the direction of flow, which is usually,

but not always, top to bottom and left to right.


Pseudocode
• A simple method of showing an algorithm.

• Described using English key words that are very similar to those used in high level programming
languages

• Pseudocode is not bound to any strict syntax rules.

• To ensure that pseudocode is easily understandable it is written in a consistent way.


Pseudocode
For consistency and better understanding..
Arithmetic/Mathematical Operators
Comparison Operators
Assignment Operator
• A value is assigned to an item or variable using the operator.

• The variable on the left of the is assigned the value of the expression on the right.

• The expression on the right can be single value or several values combined with any of the mathematical
operators.
Examples of pseudocode assignment statements
Pseudocode for Conditional Statements
• Conditional statements can be used to decide which action should be taken

based on the values of variables in the algorithm

• There are 2 conditional statements

• IF....THEN... ELSE.... ENDIF

• CASE OF...... OTHERWISE.... ENDCASE


IF....THEN...ELSE...ENDIF
• For an IF condition the THEN path is followed if the condition is true and the ELSE path is followed if the
condition is false.

• There may or may not be an ELSE path.

• The end of the statement is shown by ENDIF


Example1
Example2
Using a TRUE or FALSE condition Boolean variable
Example 3
Using comparison operators
Nested IF
This makes use of two IF statements; the second IF statement is part of the first ELSE

path. This is called a nested IF.


CASE OF ... OTHERWISE ... ENDCASE Statement
• The value of the variable

decides the path to be taken.

• OTHERWISE is the path taken

for all other values.

• The end of the statement is

shown by ENDCASE.
CASE OF ... OTHERWISE ... ENDCASE Statement
7.4 Validation and Testing
Validation
• For computer systems to only accept data inputs that are reasonable and accurate, every item of data
needs to be examined.

• Validation ensures that only data that is reasonable is accepted.

• If data is rejected, a message should be output explaining why it is rejected.


Verification
• Verification is checking that data is accurately copied from one source to another.

• It can be from paper to screen or from one file to another.

• Two types of verification

➢ Double entry

➢ Screen/Visual Check
Double Entry
• The data is entered twice,

sometimes by different operators.

• The computer system compares

both entries and if they are

different, an error message is

output requesting that data is

entered again.
Screen/ Visual Check
• It is a manual check done by the user who is entering the data.

• The data is displayed on the screen and the user is asked to confirm before continuing.

• The user checks against the original data to make sure both match.
Testing
• Process of finding and removing errors from your code.

• Spotting and removing syntax errors is easy as your interpreter will usually point you in the right direction
by telling you that the errors are there.

• Finding Logical errors on the other hand can be a lot trickier and can end up being a very time
consuming process.

• It is vital that proper testing is completed before code is published.


7.5 Identifying errors
Trace Table
• A trace table provides a structured method of recording and studying the results and data for each step
of the algorithm.

• A TRACE TABLE can be used to record the results from each step in an algorithm; it is used to record
the value of an item (variable ) each time that it changes. This manual exercise is called a DRY RUN.

• A trace table is set up with a column for each variable and a column for any output.

• Test data is then used to dry run the flowchart and record the results in the trace table

• During a dry run:

• Every time a value of a variable changes, the new value is entered in that column of trace table.

• Every time a value is output, the value is shown in the output column.
Example
Perform the dry run with the test data and complete the trace table.
Example
Perform the dry run with the test data and complete the trace table.
Example
Perform the dry run with the test data and complete the trace table.
Question
Use a trace table and the below test

data to do dry run of the flowchart.

400, 800, 190, 170, 300, 110, 600,150, 130, 900


Answer
Question
Answer
Question
Answer
Identifying Errors
Trace tables and test data can be used to identify errors in a pseudocode or flowchart
Flowchart
This is a better flowchart but still has drawback

as it cannot accept all values


Flowchart
This is a much better algorithm as this will

accept any data and output the highest and

lowest number.
Stages when producing an algorithm for a given problem
1. Make sure that the problem is clearly specified- the purpose of the algorithm and the tasks to be
completed by the algorithm
2. Break the problem down in to sub-problem (input, process, storage, output)
3. Decide on how any data is obtained and stored, what is going to happen to the data and how results
are going to be displayed.
4. Design the structure of your algorithm using a structure diagram
5. Decide how you are going to construct your algorithm, either flowchart or pseudocode
6. Construct your algorithm, making sure that it can be easily read and understood by others
7. Use several sets of test data to dry run the algorithm and show results in trace table
8. If any errors found, correct them and repeat the process until you have a perfect algorithm.
Try writing a pseudocode
Tickets are sold for a concert at $20 each. If 10 tickets are bought then the discount is 10%, if 20 tickets
are bought the discount is 20%. No more than 25 tickets can be bought in a single transaction.

a) Use pseudocode to write the algorithm to calculate the cost of buying a given number of tickets.
Testing
b) Explain how you would test the algorithm.
Let's solve an Example
Write pseudocode to input ten positive numbers and find the total and the average
Question
Answer
Question
Answer
Question
Answer
Question
Answer

You might also like