CHAPTER 8- PROBLEM SOLVING AND ALGORITHMS

advertisement
CHAPTER 8- PROBLEM SOLVING AND ALGORITHMS
People make decisions every day to solve problems that affect their lives. The problems may
be important or unimportant. If a bad decision is made; time and resource are wasted, so it is
imp that people know how to make decisions well. There are six steps to follow to ensure the
best decision.
8.1. The way of problem solving
1- Analyze the problem
2- Find the possible alternative solutions (algorithms)
3- Check the algorithm and pick the best one
4- Code the algorithm into a program
5- Check the program
6- Run the program
Types of Solutions
1. Algorithmic Solutions: solutions that can be reached through a direct set of steps (series of
steps) are called algorithmic solutions.
2. Heuristic Solutions: solutions that cannot be reached through a direct set of steps are
called heuristic solutions. (trial and error)
3. Combination of these two kinds of solutions.
1. Algorithmic Solutions:
Algorithm: is the method of solution of a given problem.
An algorithm is a well-ordered collection of unambiguous and effectively computable
operations that when executed produces a result and halts in a finite amount of time
[Schneider and Gersting 1995].
With this definition, we can identify five important characteristics of algorithms.
1.
2.
3.
4.
5.
Algorithms are well-ordered.
Algorithms have unambiguous operations.
Algorithms have effectively computable operations.
Algorithms produce a result.
Algorithms halt in a finite amount of time.
To represent an algorithm’s performance in relation to the size of the problem, computer
scientists use what is known as Big-Oh notation
 executing an O(N) algorithm requires time proportional to the size of problem
 given an O(N) algorithm, doubling the problem size doubles the work
 executing an O(log N) algorithm requires time proportional to the logarithm of
the problem size
 given an O(log N) algorithm, doubling the problem size adds a constant
amount of work
Two types of algorithms:
1- Pseudocode (Plain English)
2- Flowchart (Graphical Tools)
Flowcharts
From the algorithms the programmer develops the flowcharts, graphic representations
of the algorithms. The algorithms and the flowcharts are the final steps in organizing a
solution.
Symbols of Flowchart
There are flowchart symbols for use with various types of processing.
- Start Symbol:
-Input symbol:
or
- Assignment:
variable = [expression]
• Assign the value of the following
expression to a variable
location
- Decision:
- Output:
- End Symbol:
A flowchart shows the flow of the processing from the beginning to the end of a solution.
Pseudocode
It is close to actual language the programmer will be using. (See examples.) Pseudocode must
be precise and clear enough so that a good programmer can convert it to syntactically correct
code.
Example 8.1: Develop the pseudocode and the flowchart for the calculation of circle area that
calculates area according to the input radius.
Example 8.2: Develop the pseudocode and the flowchart for a module that calculates the
retirement deduction rate according to the following codes:
R = 10%
P = 8%
G = 5%
Example 8.3: Write the pseudocode of the following flowchart:
Flowchart:
Example 8.4: Write an algorithm that asks the user to enter 5 numbers and, calculates and
displays the total of these numbers.
8.2. Structures of programming:
 Sequence Structure
• assignment statement
• arithmetic statement
• input statement
• output statement
 Selection Structure
This structure allows for a choice between two alternatives of action.
 Loop Structure
This structure allows repeating same tasks until given conditions become false.
8.3. Beginning Problem Solving Concepts for the Computer
i) Types of Problems
The problems that can be solved on computers are:
1- Computational Problems (involve mathematical processing)
2- Logical Problems (involve relational or logical processing, decision making on computer)
3- Repetitive Problems (involve repeating a set of mathematical or/and logical instructions)
ii) Constants and Variables
Constants and variables are data used in processing to solve problems.
Constant: A value that never changes during the processing of all the instructions in a
solution.
Variable: A value that may change during program execution. Each variable must have a
name as a reference name for a specific value of the variable.
There are no blank spaces in constant/variable names.
Ex:
City name  this is not a constant or variable name
city_name
CityName
Constant or variable name
Name of Variable: is the label the computer uses to find the correct memory location.
Value of Variable: is the content of the location.
iii) Data Types
The computer must have data to process solutions. Data go into the computer as input and are
processed by the program. Then output or information is returned to the user and it is printed
in the form of reports.
There are many types of data that the computers use. The data type of each variable or
constant must be told to the computer.
Most Common Data Types:
1- Numeric Data Type
2- Character (Alphanumeric) Data type
3- Logical Data Type
1- Numeric Data Type:
Include all types of numbers. It has two types:
- integers (positive/negative whole numbers)
Ex: 1, 6, 43…
- real numbers (floating point numbers)
Ex: 1.86, 12569.5799…
2- Character (Alphanumeric) Data Type:
Include all single digit numbers, letters and special characters placed with quotation marks.
Ex: “1” … “A”, “a” … “$”, “*”, “?” …
String: include more than one character which is put together within quotation marks.
Ex: “virus”, “computer” …
Concatenation: operation that joins character data or string data together with the “+” sign.
Ex: “Ali”+”Veli”  “AliVeli”
“Ali ” +”Veli”  “Ali Veli”
“1”+”3”  “13” (Not “4”)
3- Logical Data Type:
Include the words TRUE or FALSE
iv) Operators
Operators are data connectors within expressions and equations.
Types of Operators:
1- Mathematical Operators (+, -, *, /, \, MOD, ^)
 / : division
 \ : integer division
 MOD : remainder of dividing
 ^ : 2^3=8
2- Relational Operators (decision making: =, >, <, >=, <=, <>)
 <> : not equal
3- Logical Operators (operations on logical data: AND, OR, NOT)
Hierarchy of Operators:
Hierarchy (or precedence) of operations is the order in which their operations take place. The
processing of the operands always starts with the innermost parenthesis and works outward,
and processes from left to right.
Order of operations: function, power, \, MOD, *, /, +, -, =, <, >, <=, >=, <>, NOT, AND,
OR.
Example 8.5: Convert the following into a computer expression.
4Y
X (3Y+4) –
X 6
Example 8.6: Convert the following into a relational expression.
X is less than Y + 5
Example 8.7: Find out the resultant by applying hierarchical order of operations. (X=2, Y=3,
Z=6)
5*(X + Y) − (4*Y) /(Z + 6)
Example 8.9: Find out the resultant by applying hierarchical order of operations. (A=2, B=8)
A-2>B
Example 8.10: Find the resultant by applying hierarchical order of operations. A=TRUE,
B=FALSE, C=TRUE
A AND B OR C AND A
Example 8.11: Find the resultant by applying hierarchical order of operations. A=4, B=2,
C=TRUE, D=FALSE
F=NOT(A<B) AND (C OR D)
Download