Week 1 Introduction To Problem Solving Techniques
Week 1 Introduction To Problem Solving Techniques
Solving Techniques
Steps involved in Problem Solving
5)List instructions that enable you to solve the problem using selected solution
Step by step instructions (make sure the person who use these can understand it – know the
persons knowledge base (what they know limited)
Algorithm :
An Algorithm is a set of instructions that describe a method for solving a problem. An algorithm is
normally given in some mix of computer code and English. This is often called pseudo-code
Generalized algorithm is a foundational element of logic and human reasoning. Generalization posits the
existence of a domain or set of elements, as well as one or more common characteristics shared by those
elements
Always avoid infinite loops in algorithm. An infinite loop is a sequence of instructions in a computer
program which loops endlessly, either due to loop having no terminating condition, having one that can
never be met or one that causes the loop to start over.
A set of rules must be unambiguous and have a clear stopping point
Avoid infinite loops by using counter value or a sentinel value. Sentinel value could be like break or
continue statements
Cont..
Algorithm Representation
As a Program
As flowchart
As pseudo codes
A program is a set of instructions that are arranged in a sequence that helps computer to solve complex problem. The process of writing a program is called
programming
A FlowChart
is a graphical representation of an algorithm
A flowchart shows the flow of processing from beginning to end
Each block in flowchart represents one instruction from an algorithm
Flow lines indicate direction of data flow
Why use flowcharts?
Flowcharts show errors in logic
A set of data can be tested easily using flowcharts
Cont..
Advantages of Flow-Charts
Better communication
Proper program documentation
Efficient coding (acts as a road map for programmers)
Systematic debugging
Systematic testing
Flowchart Symbols
Start / End/ Exit
Input or Output
Process
Flow Direction
Decision Making
Connector
When the programmer is faced with a problem to solve, the problem must be read carefully and
maybe, read a number of times to be very sure that the problem is understood. It is advisable to
delete all unnecessary information given in the problem statement.
Example 1: Calculate the floor area of the boardroom that is on the second floor of the building
next to the road to Johannesburg. The length of the room is 7 meters and the width is 4 meters.
It is the largest room on the second floor.
This problem statement can be simplified by deleting the unnecessary information as follows:
solution
Calculate the floor area of the board room. The length of the room is 7 meters and the width is 4
meters.
The problem statement must include enough information to enable the programmer to solve the
problem. But, if some data (values) are missing, it must be of such a kind that the user will be able
to supply the missing data.
The most important aspect of solving a problem by using a computer is to write an algorithm to
solve it. An algorithm is a set of steps that must be written in such a way that is it unambiguous and
precise. The computer cannot think for itself – you as the programmer must tell the computer
exactly what to do. You may never assume that the computer will do something if you have not
explicitly included the specific step.
Data Processing Criteria
The manager of a company asks a student to do some part time work for which he will be paid per hour.
The student will have to know how many hours he has to work at what rate of pay before he can calculate
the final amount he will receive.
In this case the input (data) to this algorithm will be the hours and the hourly rate of pay. The input (data)
must be processed to give the output (information) i.e. the amount the student will receive. It is clear that
the input (data) is meaningless unless it is processed, because if the student knows how much he will receive
per hour, but does not know how many hours he will work, the pay cannot be calculated! On the other hand,
if the student has worked for 20 hours but the manager does not tell him how much he will receive per hour,
it is also meaningless.
Example2
Calculate the sum of 2 numbers and display the result on the screen:
Input: Not available in the problem statement. The user has to supply the numbers.
Output: The sum as calculated in the processing phase is displayed on the screen of the computer.
There are different ways of planning to solve a computer related problem, but in this
course we are going to write algorithms in pseudocode to plan a solution. The solution for
a problem can also be planned with flowcharts
Pseudocode is a specific way of writing the steps of the algorithm in English-like
language using specific words.
example3
Calculate the sum of 2 numbers and display the result on the screen
Determine the weekly wage of an employee if the hours he has worked, and the hourly
rate are entered by the user.
Exercise 1: Write an algorithm that determines how many times a name occurs in a list of
names
Exercise 2: Write an algorithm to work out what grade a student got based on the following
information :- if student (above or equal) to 90 – A , 80 – B, 70 – C, 60 – D, less then or equal 60
–F
Input or Output
Process
Flow direction
Decision
Example 1: Calculate
Start Average of three Numbers
Read Three
Numbers
Calculate
average
Print
average
Stop
Group Exercises on flowchart
Sequences Logic
Iterations Logic
Selections Logic
Sequence Logic
Sequence logic is used for performing instructions one after another in sequence. In the
example below, module A consists of the sequence of modules B, C and D.
Selection Logic
Selection logic also known as Decision logic is used for making decision. In the example,
module A consists of one and only one of operations B, C or D
B C D
Iteration Logic
An iteration logic is used to produce loops in a program logic when one or more
instructions may be executed several times depending on some condition. In the example
below, module A consists of an iteration of zero or more invocations of module B.
B
Programming Languages
Natural Languages
High-Level Languages
Scripting Languages
Natural Languages
Programmer has to remember all code numbers and commands in the machine instruction set
High Level Language
Program written using human like language
Programs are more or less independent of a particular type of computer
Characteristics include:
Strong abstraction from the details of the computer
Uses natural language elements, easier to use, be more portable across platforms
Hides the details of CPU operations such as memory access models and management of scope
Scripting Language
Is a high level programming language that is interpreted by another program at runtime rather than compiled by the computer’s processor
Add functionality to a web page
High-level Programming Language Tools:
Compiler, Linker, Interpreter
Compiler: Used to transform a program written in a high level programing language from source
code into object code
Linker: Also called binder, is program that combines object modules to form an executable program
Interpreter: translate high level instructions into an intermediate form, which it then executes.
Interpreted programs are slower than compiled programs.
Editor: is a software where a programmer can write the source code, edit , compile and execute the
program
MATLAB (MATrix LABoratory): is a numerical computing environment and fourth generation
programming language. It allows matric manipulation, plotting of functions and data, implementation
of algorithms, creation of user interfaces and interfacing with programs in other languages
GUI (Graphical User Interface)-takes advantage of the computer’s graphics capabilities to make the
program easier to use
Selection Criteria for a Programming Language
Usability
Performance
Portability
Extensibility
Possibility of developing the language and its implementation, existence of function libraries, class libraries etc
Continuity
Continuity of manufacturer, language continuity, implementation continuity and existence and conformity to international standards
NB: Subprogram /Subroutine also called procedure/function/method/routine is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code
Progr
Program Testing and Debugging
A software bug is an error, flaw, mistake, failure, fault in a computer program that prevents it from behaving as intended
Formal Program Testing
Validation deals with appropriateness of the program whereas Verification deals with the correctness of a program
Technique of validation entails
Inspection code at a high level
Have the user/customer test software
Requires good software specification
Technique of Verification entails
Code walk through or inspection
Testing (which involve using input data and observing output
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or hardware
Difference between Testing and Debugging
Testing Debugging
Finds cases where a program does not meet its specification The process of debugging involves analyzing and removing bugs in
order to get a program with expected specification
Demonstrate that the program meets its design specification Detect the exact cause and remove known errors in the program
Complete when all desired verifications against specifications Complete when all known errors on the program have been fixed
have been performed
Can begin in the early stages of the software development Begin only after the program is coded
It is the process of validating the correctness of the program It is the process of eliminating the errors in a program
Types of Program Errors
Syntax/compilation errors
Run-time exception errors
Logical errors
Syntax Errors: Error during compiling prevents getting a compiled program. Errors occur
when the correct programing language rules have been violated.
Run-time exception errors: Error that occurs while running the compiled code
Logical Error: Error that occurs when a compiled program runs but fail to produce the desired
results