0% found this document useful (0 votes)
54 views

Week 1 Introduction To Problem Solving Techniques

The document discusses problem solving techniques and provides steps to solve problems: 1) Identify and understand the problem. 2) Identify alternative solutions. 3) Select the best solution by evaluating pros and cons of each. 4) Provide instructions to implement the selected solution. 5) Evaluate the solution to check if it satisfies the problem. Well-identified problems, consideration of alternatives, and ability to implement the solution lead to good decisions. Pseudocode and flowcharts are presented as ways to plan algorithms to solve problems on computers.

Uploaded by

James Monchoi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Week 1 Introduction To Problem Solving Techniques

The document discusses problem solving techniques and provides steps to solve problems: 1) Identify and understand the problem. 2) Identify alternative solutions. 3) Select the best solution by evaluating pros and cons of each. 4) Provide instructions to implement the selected solution. 5) Evaluate the solution to check if it satisfies the problem. Well-identified problems, consideration of alternatives, and ability to implement the solution lead to good decisions. Pseudocode and flowcharts are presented as ways to plan algorithms to solve problems on computers.

Uploaded by

James Monchoi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Introduction to Problem

Solving Techniques
Steps involved in Problem Solving

 Identify/understand the problem


 – If you don’t know what the problem is you can’t solve it (understand what is involved in a problem)
  

 2) Identify alternative ways to solve the problem


 - Make the list complete – talk to other people to find solutions you not thought of – if alternatives are incomplete – you are not in a position to
make a proper decision – Is the alternative solution
  
 3) Select the best way to solve the problem from the list of alternative solutions
  
 Identify and evaluate pros and cons of each possible solution before selecting the best one.
 To do this select criteria for the evaluation – this will serve guidelines for evaluating each solution
  
Cont…

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)
 

6)Evaluate the solution


 Evaluate or test the solution means to check the result to see if its correct and to see if it satisfies
the needs of the person with a problem (e.g. if you need a bed buying a cot may not be
satisfactory)
 
What makes a good decision?

 Well identified problem


 All alternatives considered
 Information overloaded – appropriate alternatives
 Can the person carry out steps/instructions
  
Program Planning Concepts

 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

Pre-defined Process (module, function,


Understanding a problem

 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

 An algorithm to solve a computer based problem consists of 3 phase’s i.e.


 What you have available for the algorithm to solve the problem
 How you are going to solve the problem i.e. what steps you are going to take
 What is the required result
  
 In other words:
 Input is processed to render meaningful output.
 Data is processed to render meaningful information.
Example1

 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.
 

Processing: Add the 2 numbers to determine the sum.


 

Output: The sum as calculated in the processing phase is displayed on the screen of the computer.
 

 

Note the following:


The value of the numbers must be available before the sum can be calculated.
It is impossible to display the sum before it has been calculated.
These two remarks indicate that the steps must always be processed in a logical order.
 
Writing an Algorithm

 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

 Algorithm: Calculating sum of 2 numbers


 Step1: Request/Prompt for two numbers
 Step 2: Enter the numbers say a,b
 Step2: Compute sum=a+b
 Step3: Display (print) sum
  
  
Example 4

Determine the weekly wage of an employee if the hours he has worked, and the hourly
rate are entered by the user.
 

Algorithm: Calculate Wage


1. Request/Prompt for hours that the employee worked
2. Enter number of hours
3. Request/Prompt the hourly rate of pay
4. Enter hourly rate of pay
5. Compute Wage = number of hours X hourly rate of pay
Group Exercises

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
 

Exercise 3: Write an algorithm for computing area of triangle


Exercise 4: Write an algorithm for calculating average of three numbers
Exercise 5: Write an algorithm for computing largest of three numbers
Exercise 6: Write an algorithm for computing real roots of quadratic equation
Algorithms to Flowcharts

 From algorithms the programmer develops flowcharts


 Graphical representations of algorithms
 A flowchart shows the flow of processing form beginning to end
 Each block in flowchart represents one instruction from an algorithm
 Flowlines indicate direction of data flow
 Why use flowcharts?
 Flowcharts show errors in logic
 A set of data can be tested easily using flowcharts
 
Flowchart Symbols
Start / End/ Exit

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

Exercise2: Draw a flowchart for computing area of triangle


Exercise 3: Draw a flowchart for computing largest of three numbers
Exercise 4: Draw a flowchart for computing real roots of quadratic equation
Exercise 5: A Patient rings a doctor’s surgery to book an appointment. The receptionist
takes patient details and checks to see if the patient exists on their system. If the patient does
not exist on the system then the receptionist adds the patient to the system. If patient exists on
the system then receptionist checks next available appointment and informs patient of
appointment dates: Draw a Systems Flowchart for the above scenario
Principles of good programming style

 Clarity and simplicity


 Use meaningful identifiers
 One statement per line
 Use proper and consistent indentation of control structures
Pseudo-code for Basic Logic

 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

 Types of Programming Languages

 Natural Languages

 Machine Level Languages

 High-Level Languages

 Assembly Level Language

 Scripting Languages

  

 Natural Languages

 Is a language spoken, written or signed by humans for general purpose communication

 Machine Level Language

 It is the lowest-level programming language

 The only languages understood by computers

 Consists of strings of binary numbers (zeros and ones—0, 1)

 Limitations of machine languages are

 Programmer has to remember all code numbers and commands in the machine instruction set

 Programmer has to keep track of storage locations of data and instructions

 Debugging and modifying program is tedious and time consuming


Cont..

  
 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

 easy to learn and use

 Performance

 Good speed of execution , compilation

 Good stability (lack of defects)

 Portability

 Implementable in variety of computers

 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

You might also like