Professional Documents
Culture Documents
Chapter 7: Problem Solving Techniques
Chapter 7: Problem Solving Techniques
1
Contents
1 Programming and Types (Generations) of Programming Languages 4
1.1 Machine Language (First Generation PL) . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Assembly Language (Second Generation PL) . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Third Generation Languages (3GLs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3.2 Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Fourth Generation Languages (4GLs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Web-Page Development Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Control Structures 11
2
List of Figures
1 Some graphical symbols used in flowcharts . . . . . . . . . . . . . . . . . . . . . . . . 9
2 The flowchart representation of Examples (1 – 3) . . . . . . . . . . . . . . . . . . . . 11
3 The flowchart representation of Examples (4 – 6) . . . . . . . . . . . . . . . . . . . . 13
4 Images for Sequence CS Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5 The general representation of If. . . Then. . . Else selection, using flowchart . . . . . . . 15
6 The flowchart representation of Example (1) . . . . . . . . . . . . . . . . . . . . . . . 16
7 The general representation of If. . . Then selection, using flowchart . . . . . . . . . . . 17
8 The flowchart representation of Example (3) . . . . . . . . . . . . . . . . . . . . . . . 17
9 Case selection representation using flowcharts . . . . . . . . . . . . . . . . . . . . . . 20
10 The general flowchart representation of While loop . . . . . . . . . . . . . . . . . . . 22
11 The flowchart representation of Example (1) . . . . . . . . . . . . . . . . . . . . . . . 23
12 The flowchart representation of Example (2) . . . . . . . . . . . . . . . . . . . . . . . 24
13 The flowchart representation of Example (3) . . . . . . . . . . . . . . . . . . . . . . . 25
3
1 Programming and Types (Generations) of Programming
Languages
• Programming is a problem solving activity; when you write a program, you are actually
writing a group of instructions for the computer, in order to solve something for you.
• Programming Language: is a set of words, abbreviations and symbols. This set is used to
create computer programs. Such language has its own grammar, that states the rules of writing
valid statements using this language. These rules are called the syntax of a programming
language.
• Programming languages can be classified into: low-level and high-level programming languages.
• Here, the programs – that are written using such language – are bit-patterns (binary values
with digits containing (0)s and (1)s).
• In early computers, the computers were given these patterns, using switches or punched cards.
• For simplicity, assume that we have a computer, which uses the following instruction set:
Binary Pattern Meaning
11110000 Add two numbers
11001100 Subtract two numbers
Then, (00000101 11110000 00000110) is a machine instruction (code) that instructs the
computer to “add” the numbers (5) and (6).
4
Binary Pattern Meaning
00000101 5
11110000 Add
00000110 6
• Source code: shortly called code, is the group of instructions (or statements) that are written
using a programming language. These instructions are executed directly by the computer’s
processor, after loading them into the main memory.
– The syntax of these languages is more like human language; it uses English-like words,
which are easier to be understood by people.
– These languages are machine-independent, which means: any program that is written
using high-level languages, can run on different types of computers and operating systems.
• The used commands and instructions are human readable (English-like keywords).
5
• For example: words like “include”, “return” and “while” are used in C++, which is a high
level programming language.
• Third Generation languages are also called Procedural Languages or Imperative Languages.
• This means that the programmer writes instructions that tell the computer what to do and
how to do it.
• “3GLs” also use symbols for arithmetic and logical operations, like: +, *, >, etc. . .
1.3.1 Compiler
• Compiler: is a software that translates instructions written in a high level language, into the
equivalent machine language (object code), where:
– After the source code instructions are written or modified, the entire source code is
translated into object code, before executing that program.
– The object code can be executed many times, once it is created.
3GL Source code → 3GL Compiler → Object code
Written by programmer Translates “Source code” to Runnable by computer
“Object code”
• Example: the C++ code (cout << 5 + 6;) orders the computer to “add” the numbers (5) and
(6), then print the result on the screen.
1. The execution is fast; there is no need for translation every time the program runs,
unless the source code is changed.
2. It is difficult to reconstruct the source code from the compiled object code.
• However, finding the errors in the source code – when using compilers – is difficult.
1.3.2 Interpreter
• Interpreter: is a software that reads a single statement from the source code, translates
it, and then delivers it to the computer for processing.
• However, more time is needed to execute a program, since every execution of the program
requires translation. This is a disadvantage of using interpreters.
6
1.4 Fourth Generation Languages (4GLs)
• Fourth Generation programming languages are also known as Non-Procedural Languages or
Declarative Languages.
• This means that the programmer writes what he wants without any details on how to do it.
• In “4GLs”, visual environments – where easy GUI items like: buttons, icons and tool-bars are
used – make it easy to create an application program, and you can see what will you get
visually during programming.
• Examples include:
7
2 Software Development Life Cycle (SDLC)
• Software Development Life Cycle, also known as Program Development Life Cycle, is the
process of building a computer program, according to user needs.
• Problem solving is the process of transforming the description of a problem into a solution.
• This is done by using appropriate problem solving strategies, techniques and tools.
• To analyse the requirements, the programmer should perform three major steps:
1. Input: the required data and information, that answer the following questions:
– What data do I need?
– How will I get the data?
– What format should the data take?
2. Processing: the performed operations using the defined inputs, in order to get the required
outputs.
– Differentiating data from information is important.
– How can I manipulate the data, to produce meaningful results?
3. Output: the results of the program’s processing.
– What outputs should I return to the user?
– What format should the outputs take?
8
2.2 Solution Design
• Designing the solution involves devising a solution algorithm to satisfy the requirements.
• Solution Algorithm, also called “Program Logic”: is a graphical or written description of
the step-by-step procedures, that are followed to solve the given problem.
• The program logic defines the logical order of execution, which is called program’s “Flow of
Control”.
• An algorithm mainly satisfies the following points:
– It may have input(s) and output(s).
– It should be clear: there should be no different meanings for the given algorithm, every
step must perform what it is supposed to do.
– It should be general: thus making it possible to be used with different inputs.
– It should be correct and efficient, so it can solve the targeted problem using the available
resources.
– It should execute and terminate in a finite time period .
• An algorithm can be represented using pseudo-codes or flowcharts.
• Pseudo-code: is a semi-formal, English-like language with limited vocabulary, that can be
used to write algorithms.
2.2.1 Flowchart
• Flowchart: is a graphical way of showing a step-by-step solution, by using specific symbols.
• It is an alternative to writing pseudocodes; a pseudocodes have verbal descriptions, while
flowcharts are graphical in nature.
• Figure (1) shows some graphical symbols, that are used within flowcharts.
9
2.3 Design Implementation
• It is the process of transforming the program logic design into a source code.
• In this stage, the program design is translated into computer instructions. These instructions
form the actual program (software product).
• If errors were detected, a debugging process begins, which is used for locating the errors precisely,
in order to correct them.
– Syntax Errors: which occur at compile time, if the language rules were violated .
– Semantic Errors: which occur at run-time (when we are running the program), and
may cause the program to produce incorrect outputs.
• This may lead to detect additional issues (that were not discovered in the testing step), or to
request changes – which may include adding new requirements. So the SDLC will be started
over again.
10
3 Control Structures
• There are three types for program flow, named control structures:
– Sequence Control Structure: is a series of statements that are executed in the order
they are written inside an algorithm.
– Selection Control Structure: runs block of one or more statements, depending on the
outcome of a condition.
– Repetition Control Structure: specifies a block of one or more statements, that are
repeatedly executed while a condition is satisfied.
11
2. Calculating a circle’s area, using the equation (Area = P I × Radius2 ).
Pseudo-code
Start
Enter number radius
Set PI = 3.14
Calculate area = radius × radius × PI
Print area value
End
The flowchart is shown in Figure (2b).
12
(a) Example (4) (b) Example (5) (c) Example (6)
13
4.2 Exercises – Sequence CS
Write an algorithm, for the following questions, using pseudocodes and flowcharts.
2. Find the perimeter of a circle, the user should input the circle’s diameter.
3. What is the area of the triangle in Figure (4a)? The width and height are read from the user.
4. In Figure (4b): what is the measurement of angle (B )? The measurement of angle (A) is read
form the user.
5. An employee’s annual salary was increased by (1500) Dinars. If his new annual salary now
equals (9000) Dinars, what is the percentage of that increment?
14
5 Selection Control Structure
• In the selection control structure, the execution sequence of the solution differs from one
case to another, based on some logical condition.
• The logical condition is a statement that is evaluated into one of two values, true or false,
by using logical and/or numerical comparison operations.
– (x > 10) can be evaluated into true or false, when the value of x is known.
– (salary ≥ 1000) and (cost < 275).
– (result 6= 0) or (result = 1).
• In normal words, yes is used in the place of true, and no is used in the place of false.
• The selection control structure has a general form, called (If..Then..Else) control structure,
from this general form, all selection types are derived .
– This form gives a clear point of what happens: when the logical condition is evaluated into
true, the block marked by Statements 1 will be executed , then the program execution will
skip Else and all its sub-statements, and will go to the next statements in the sequence.
– Otherwise, when the logical condition is evaluated into false, then Statements 1 will be
skipped , and the execution of Statements 2 will take place, then the remaining operations
are continued.
• This type of selection can be represented using flowcharts, as it appears in Figure (5).
Figure 5: The general representation of If. . . Then. . . Else selection, using flowchart
15
• Multiple selection control structures inside each others are called nested selection control
structures.
• Sometimes, the inner statements that come after (else) may begin with another selection control
structure.
16
Figure 7: The general representation of If. . . Then selection, using flowchart
Pseudo-code
Start
Enter number c
Calculate part1 = 1.8 × c
Calculate f = 32 + part1
Print f value
If (f > 100) then
Print “Too Hot!”
End
The flowchart is shown in Figure (8).
17
5.5 Case Selection Control Structure
• It is a control structure, that uses the general form (If. . . Then. . . Else selection).
• In some problems, an item can have a value from a domain, that the user can use in input.
• However, selecting such domain-values will cause the sequence to be performed in different ways.
• The general representation of this control structure, can be expressed using pseudocodes, as
appears next, as well as using flowcharts, which is shown in Figure (9a).
Pseudo-code – Case Selection
Assume that a variable (x ) has definite number of values (n), where the values set (V ) is:
V = {v1 , v2 , . . . vn−1 , vn }
• The last set of statements, that is named “Statements (n)”, are executed only when no related
logical condition in the whole solution, was evaluated to (true), such selection case is called
default case.
18
Start
Enter numbers a, b, c, d
Calculate total = a + b + c + d
Calculate average = total ÷ 4
Print average value
If (average ≥ 90) Then
Print “Rank is (A)”
Else If (average ≥ 80) Then
Print “Rank is (B)”
Else If (average ≥ 70) Then
Print “Rank is (C)”
Else If (average ≥ 60) Then
Print “Rank is (D)”
Else
Print “Rank is (F)”
End
The flowchart is shown in Figure (9b).
19
(a) General Form (b) Example (4)
20
5.7 Exercises – Selection CS
Write an algorithm for each of the following questions, using pseudocodes and flowcharts.
1. The input is a number, while the output should be printed as (Positive) when that number is
positive, (Zero) when that number is (0), otherwise, it should be (Negative).
2. The inputs are three numbers, and the output is the smallest number value.
3. The input is one number, which is the student average in the school’s final year, the output
should be (Pass) if the grade is greater than or equal to (50%), or (Fail) otherwise.
Also, as an additional output, the word (Awarded) should be printed if the average was (95%)
or more; no additional output should be printed if the average was less than (95%).
4. In some universities, the student is full-time, when he registers (12) credit hours or more, or
part-time if he registers less than (12) credit hours. Use the credits as input to print the
tuition fees for that student, given that full-time fees are fixed at (2000), and the part-time
fees are calculated as (the number of credits × 175).
5. The input is the total money spent at the mall, and the output is the number of points earned.
For all customers, the awarded points are (1 point per 1 spent dollar), and if a customer spends
more than (200) dollars, then he will get (30) more points.
6. The inputs are three numbers (a, b and c), representing the measurements of a triangle, the
output should indicate if the inputs can form a right triangle (has the largest angle = 90◦ ), this
can be done by using well-known equation: (c2 = a2 + b2 ).
7. The input is a number that represents water temperature, the output should be one of the
listed descriptions 1 , depending on the input value:
Water Temperature Description
(0) and less “Ice / solid”
1-4 “Melting”
5 - 20 “Cold liquid”
21 - 40 “Cool liquid”
41 - 70 “Warm liquid”
71 - 99 “Hot liquid / evaporating”
(100) and more “Boiling / gas”
8. The input is one number representing base salary (BS), the output should be the amount of
net salary (NS), which is calculated using the equation: (N S = BS + Bonus). The bonus is
calculated by multiplying the base salary with its respective bonus rate. Bonus rates are listed
in the following table:
Base Salary Domain Bonus Rate
Less than (1000) 0.015
1000 - 1499 0.020
1500 - 1799 0.030
1800 - 2099 0.055
(2100) and more 0.075
1
Please note that these details are used as examples, and may not reflect precise facts.
21
6 Repetition Control Structure
• Repetition Structures are used to repeat an action or calculation.
• The while loop is used to represent repetition in this document. Its flowchart represemtation
is shown in Figure (10), and its pseudo-code is:
Pseudo-code
While (logical condition)
Repeating Statements
22
6.1 Examples – Repetition CS
The following examples show the usage of pseudo-codes and flowcharts to represent repetition
algorithms:
Pseudo-code
Start
Set i = 1
Set total = 0
While (i ≤ 45)
Calculate a = (i × i ) ÷ 8
Calculate total = total + (square root of a)
Calculate i = i + 1
Print total value
End
The flowchart is listed in Figure 11.
23
2. Print out the following sequence: (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024).
Pseudo-code
Start
Set i = 2
Set result = 1
While (result ≤ 1024)
Calculate result = result × i
Print result value
End
The flowchart is listed in Figure 12.
3. A class of ten students took a quiz, their scores are ten integers of the domain [0 - 100]. Find
the class average.
Pseudo-code
Start
Set total = 0
Set count = 1
While (count ≤ 10)
Enter number grade
Calculate total = total + grade
Calculate count = count + 1
Calculate average = total ÷ 10
Print average value
End
The flowchart is listed in Figure 13.
24
Figure 13: The flowchart representation of Example (3)
1. Display a positive integer number, which is entered by the user, in reversed order.
Example: when the input is (2753), the output will be (3572).
Hint: use remainder and division by (10).
2. Eight integers should be entered by the user, use them to find the minimum and the maximum
numbers.
3. An integer is entered by the user, use it to find the sum of its divisors.
Example: if the user inputs (15) then the divisors will be: (1, 3, 5, 15) and the output will be
their total (24).
4. Compute the sum of all integers between two entered values (n1) and (n2), given that (n1)
should be greater than (n2).
5. Calculate the overall average of a class that has (38) students, the input should contain every
student’s grade.
7. Compute the number of digits in an integer, if the digits count was (7), then output a message
(”A land-line phone number!”).
25
9. Find the greatest common divisor of two positive integers. If any of these numbers is not
positive, print a suitable message.
10. The user enters a positive integer (n), then he should enter (n) values, and output the minimum
of these values. Finding the maximum number can be tried as well.
11. The user enters as much numbers as possible, this should continue if the input was not negative.
After a negative number is entered, the input stops, and the output should be the count of the
entered values. Do not include the last negative value into the count.
Example: if the entered values were (1, 5, 6, 0, 4, 8, 11, 36, 2, -10), the output should be (9).
12. The user enters a positive integer (n), this is followed by entering more (n) numbers, and then
output their average.
Example: if the user first enters (5) to be stored as (n), then he should enter (5) other values,
like: (9, 7, 3, 6, 10), the total of these values = 9 + 7 + 3 + 6 + 10 = 35, and their average is
(35 ÷ 5) = 7, so the output is (7).
26