Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Chapter 7: Problem Solving Techniques

Prepared by: Eng. Yousef Salah


Updated by: Mr. Mohammed Y. Tamimi
Reviewed by: Dr. Hani Salah

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

2 Software Development Life Cycle (SDLC) 8


2.1 Problem Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Solution Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.1 Flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.3 Design Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4 Program Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 Operational Release and Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Control Structures 11

4 Sequence Control Structure 11


4.1 Examples - Sequence CS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Exercises – Sequence CS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

5 Selection Control Structure 15


5.1 Selection’s General Form: If. . . Then. . . Else . . . . . . . . . . . . . . . . . . . . . . . 15
5.2 Examples of Using If. . . Then. . . Else Selection . . . . . . . . . . . . . . . . . . . . . . 16
5.3 Selection’s Special Form: If. . . Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.4 Example of Using If. . . Then Selection . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.5 Case Selection Control Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.6 Example of Using Case Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.7 Exercises – Selection CS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

6 Repetition Control Structure 22


6.1 Examples – Repetition CS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.2 Exercises – Repetition CS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

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.

• Computer Program: is a collection of instructions (commands) that directs the computer


to perform a specific task. These instructions are written in a specific language, that is called
programming language.

• Computer programs are also called computer software or computer applications.

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

– Low-level programming languages include:


1. Machine Language (First Generation).
2. Assembly Language (Second Generation).
– High-level Programming Languages, which includes:
1. Third Generation Languages (3GLs).
2. Forth Generation Languages (4GLs).
3. Visual Programming Languages.
4. Web-page Development Languages.

1.1 Machine Language (First Generation PL)


• This is an example of low-level languages, which have these properties:

– The syntax of these languages is close to the hardware.


– This language is machine-dependent, which means: any program – that is written using
this language – can run only on a specific computer.

• Here, the programs – that are written using such language – are bit-patterns (binary values
with digits containing (0)s and (1)s).

• A computer understands specific patterns, which form an instruction set.

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

• Some disadvantages of using machine language:

1. The programming is tedious, error-prone and time-consuming.


2. Debugging (or finding errors) is difficult.
3. Programs are machine-dependent.

1.2 Assembly Language (Second Generation PL)


• The instructions – in a computer program written in an assembly language – are meaningful
and readable names and symbols (called mnemonics), that correspond to specific bit-patterns.

• Example: if there was a computer that uses the following mnemonics:


Binary Pattern Mnemonic Meaning
11110000 ADD Add two numbers
11001100 SUB Subtract two numbers
Then, the assembly code (ADD 5,6) orders that computer to “add” the numbers (5) and (6).

• Programs written in assembly language cannot be delivered directly to the computer.


A system software called “Assembler” is used to translate an assembly program into its
equivalent code in machine language (Object Code).
Source code → Assembler → Object code
Written by programmer Translates “Source code” to Runnable by computer
“Object code”

• Some problems happen when using Assembly language:

1. Programs are still machine-dependent.


2. Debugging is still difficult.
3. Running (or executing) an assembly program requires more time in comparison to a
machine code program, because there is an additional translation process.

1.3 Third Generation Languages (3GLs)


• This is an example of high-level languages, which has the following properties:

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

• “3GLs” examples: C, C++, Pascal, FORTRAN, COBOL, Java, C# and more . . .

• A computer program that is written in a high level programming language:

– Should be translated into machine language.


– There are two types of translators: compiler and interpreter .
– Some programming languages have compilers, others have interpreters, and some have
both.

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.

• Using compilers have multiple advantages, like:

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.

• An advantage of using interpreters is: finding the errors becomes easy.

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

• “4GLs” are easier to use than “3GLs”:

– The programmer writes English-like instructions, or interacts with a graphical environment,


to get what he wants.
– Sometimes, using a single instruction in “4GL” may perform the same task, which is done
using many instructions of a “3GL”!

• 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 of “4GLs”: LISP, Prolog and SQL.

1.5 Web-Page Development Languages


• These are special programming languages and tools that are used to construct web pages.

• Examples include:

– HTML: Hyper-Text Markup Language.


– PHP: “PHP: Hypertext Preprocessor” Language.
– ASP: Active Server Pages.
– JSP: Java Server Pages.
– XML: eXtensible Markup Language.

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.

• SDLC consists of the following main steps:

1. Analyse the problem.


2. Design the solution.
3. Implement the design.
4. Test the solution.
5. Release for live operation and maintenance.

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

2.1 Problem Analysis


• Sometimes called “Requirements Analysis”; the requirements define what the users need from
the program to do / have.

• When specifying a problem’s requirement, we ask ourselves the following questions:

– What is the problem?


– What should the solution provide?
– What is needed to solve the problem?
– Are there constraints and/or special conditions?

• To analyse the requirements, the programmer should perform three major steps:

1. Review the requirements.


2. Meet the users.
3. Identify the program’s scope, by determining its inputs, processing, and outputs.

• This is followed by creating IPO charts, which clearly define:

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.

(a) Terminal (b) Process (c) Input/Output

(d) Selection (e) Connector (f) Flow Line

Figure 1: Some graphical symbols used in flowcharts

– Terminal: indicates the starting and ending points of an algorithm.


– Process: shows an instruction other than input, output or selection.
– Input/Output: shows an operation that is related to input or output.
– Selection: shows a process of two-way selection.
– Connector: provides continuation of a logical path, at another point.
– Flow Line: indicates the logical sequence of execution steps in an algorithm.

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

2.4 Program Testing


• In this stage, we test the program to ensure that it meets the users requirements, and to check
it has no errors.

• If errors were detected, a debugging process begins, which is used for locating the errors precisely,
in order to correct them.

• There are two types of errors that may occur in programs:

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

2.5 Operational Release and Maintenance


• In this step: the tested program – that is working fine at this point – is installed in the real
environment, where it is requested to be used, and the end-users will start using it.

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

4 Sequence Control Structure


• The sequence control structure is a group of statements, that are performed sequentially.

4.1 Examples - Sequence CS


In the following examples, pseudo-codes and flowcharts are used to represent the solutions of:

1. Finding the square of a number.


Pseudo-code
Start
Enter number n
Calculate s = n × n
Print s value
End
The flowchart is shown in Figure (2a).

(a) Example (1) (b) Example (2) (c) Example (3)

Figure 2: The flowchart representation of Examples (1 – 3)

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

3. Calculating a cube’s volume.


Pseudo-code
Start
Enter number length
Calculate volume = length × length × length
Print volume value
End
The flowchart is shown in Figure (2c).

4. Calculating f value in the expression: (f = a2 + 2ab + b).


Pseudo-code
Start
Enter numbers a, b
Calculate part1 = a × a
Calculate part2 = 2 × a × b
Calculate f = part1 + part2 + b
Print f value
End
The flowchart is shown in Figure (3a).

12
(a) Example (4) (b) Example (5) (c) Example (6)

Figure 3: The flowchart representation of Examples (4 – 6)

5. Finding the average of three numbers.


Pseudo-code
Start
Enter numbers a, b, c
Calculate total = a + b + c
Calculate average = total ÷ 3
Print average value
End
The flowchart is shown in Figure (3b).

6. Converting the Celsius temperature to Fahrenheit temperature, using the equation:


F = 1.8C + 32
Pseudo-code
Start
Enter number c
Calculate part1 = 1.8 × c
Calculate f = part1 + 32
Print f value
End
The flowchart is shown in Figure (3c).

13
4.2 Exercises – Sequence CS
Write an algorithm, for the following questions, using pseudocodes and flowcharts.

1. Calculate a person’s age, after entering birth-year and current-year.

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?

6. Find the value of the following expression: (x + x1 )( x1 ), where x = 10−1 .

(a) Triangle (b) Angles

Figure 4: Images for Sequence CS Exercises

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.

• Examples of logical conditions:

– (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 .

5.1 Selection’s General Form: If. . . Then. . . Else


• The selection can be represented using pseudocodes, as it appears in the following notation:
Pseudo-code – If. . . Then. . . Else Selection
If (logical condition) Then
(Statements 1 ). . .
Else
(Statements 2 ). . .

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

5.2 Examples of Using If. . . Then. . . Else Selection


In the following example, the pseudocode and flowchart are used to represent the solutions of finding
the larger value (out of two entered numbers):
Pseudo-code
Start
Enter numbers x , y
If (x > y ) Then
Print x value
Else
Print y value
End
The flowchart is shown in Figure (6).

Figure 6: The flowchart representation of Example (1)

5.3 Selection’s Special Form: If. . . Then


• This selection type can be represented using pseudocodes, as it appears in the following
notation:
Pseudo-code – If. . . Then Selection
If (logical condition) Then
(Statements 1 )
• This type of selection can be represented using flowcharts, as it appears in Figure (7).

5.4 Example of Using If. . . Then Selection


3. The user enters today’s temperature in Cent-grades (C), find the Fahrenheit value (F), using
the equation (F = 32 + 1.8C ), then output the Fahrenheit value.
If the Fahrenheit value is more than 100 degrees, then output a message (Too Hot!)

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

Figure 8: The flowchart representation of Example (3)

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.

• This type of control structures is called case selection.

• 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 }

Then the case selection of (x ) is:


If (x = v1 ) Then
Statements 1 . . .
Else
If (x = v2 ) Then
Statements 2 . . .
Else
.........
If (x = vn−1 ) Then
Statements (n-1). . .
Else
Statements (n)

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

5.6 Example of Using Case Selection


4. Write an algorithm, that reads four grades of a student, find and output the average, then
output the student rank, based on the following table:
Average Domain Rank
90 and more A
80 and more B
70 and more C
60 and more D
Less than 60 F
Pseudo-code

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)

Figure 9: Case selection representation using flowcharts

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.

• Repetition structures are often called loops or iterations.

• 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

Figure 10: The general flowchart representation of While loop

• Loops in general require three elements to be available, in order to work correctly:

1. The starting point of the repetition.


2. Testing some logical condition, that is used to decide whether to continue the repetition
to the next round (when evaluated to true), or to stop the repetition and go to the normal
sequence (when evaluated to false).
3. The method of changing the loop’s state, from the starting point until the loop ends.

22
6.1 Examples – Repetition CS
The following examples show the usage of pseudo-codes and flowcharts to represent repetition
algorithms:

1. The total of the formula: s


45
X i2
i=1 8

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.

Figure 11: The flowchart representation of Example (1)

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.

Figure 12: The flowchart representation of Example (2)

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)

6.2 Exercises – Repetition CS


Write algorithms, for the following questions, using pseudocodes and flowcharts:

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.

6. Compute the total of the digits, for some entered integer.

7. Compute the number of digits in an integer, if the digits count was (7), then output a message
(”A land-line phone number!”).

8. Convert an entered binary number into its decimal value.


Hint: write the numeric powers using any suitable form, like: (57 ) can be written as (5 to the
power 7).

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

You might also like