0% found this document useful (0 votes)
140 views56 pages

COS102 Problem Solving

The document is a lecture material for COS 102: Problem Solving at the University of Abuja, introducing students to the fundamentals of problem-solving and programming. It covers the definition of problems, problem-solving techniques, types of problems (routine and non-routine), and the importance of systematic approaches in addressing issues. The material emphasizes the development of cognitive and analytical skills necessary for effective problem-solving in various contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views56 pages

COS102 Problem Solving

The document is a lecture material for COS 102: Problem Solving at the University of Abuja, introducing students to the fundamentals of problem-solving and programming. It covers the definition of problems, problem-solving techniques, types of problems (routine and non-routine), and the importance of systematic approaches in addressing issues. The material emphasizes the development of cognitive and analytical skills necessary for effective problem-solving in various contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

UNIVERSITY OF ABUJA

DEPARTMENT OF COMPUTER SCIENCE


FACULTY OF SCIENCE

COS 102:PROBLEM SOLVING

This lecture material is prepared to introduce and acquaint students with the basic knowledge of
problem solving alongside the tools. In addition, the fundamentals of programming language are also
highlighted in relation to problem solving. Importantly, the materials are gathered from Books,
Journals and Web resources.

1|Page
Lecture 1
Problems and Problem solving
Overview
Every day people face problems generally; single, many problems through the day.
Occasionally these problems are straightforward, from time to time, sometimes, the problems
are more complex. “For example, say you have a work deadline, and you must mail a printed
copy of a report to your supervisor by the end of the business day. The report is time-sensitive
and must be sent overnight. You finished the report last night, but your printer will not work
today. What should you do? First, you need to identify the problem and then apply a strategy
for solving the problem.”
Problem-solving (PS) skills are needed in all areas of life, including classroom problem solving
activities. The capacity to decisively analyse a problem and set up an effective solution is one
of the most valuable skills one can acquire in life, in school, work or in social circle. Training
in this regard promotes cognitive, social development, and can prepare students with the skills
needed to address and solve problems all the way through the rest of their lives. Whatever
activity human being or machine does to achieve a specified objective comes under problem
solving.
Objectives: To introduce students to the fundamental concepts and methods of
problem/problem-solving, providing them with tools and techniques to approach and solve
problems effectively.
After going through this topic, students would be able to:
➢ Understand meaning of problem and problem-solving.
➢ Problem- identification and types.
➢ Methods of solving computing problems.
➢ Solution techniques to solving problems.

1. Problem and Problem-solving


A problem is a task. Generally, problem is a kind of barrier to achieve something and problem
solving is a process to get that barrier removed by performing some sequence of activities.
Thus, PS is an act of defining a problem; determining the cause of the problem; identifying,
prioritizing, and selecting alternatives for a solution; and implementing the solution.

2|Page
Problem solving is a cognitive process aimed at figuring how to achieve a specific goal.
Holistically, it is the act of defining a problem; determining the cause of the problem;
identifying, prioritizing, and selecting alternatives for a solution; and implementing a solution.
So, it is the act of finding a solution when a method for solution is not obvious. It can be story
problems or simply open-ended questions. Learning in a problem-solving method is by
working on problems. Students learn new knowledge by solving problems. This is done
through observation, understanding, analysing, interpreting to find solutions, and performing
applications that would give insight to a holistic understanding of the concepts. Overall, PS is
a process - ability to work from known and discover unknown.

Key Terms:
• Problem: A situation that requires a solution.
• Solution: The method or process of solving a problem.
Examples of PS:

Example 1: A simple example on input/process/output. Calculate the average grade for all
students in a class.
Input: get all the grades … perhaps by typing them in via the keyboard or by reading them
from a USB flash drive or hard disk.
Process: add them all up and compute the average grade.
Output: output the answer to either the monitor, to the printer, to the USB flash drive or hard
disk … or a combination of any of these devices. To solve the problem, understand the problem.
Thus, these questions???????
▪ What input data/information is available?
▪ What does it represent?
▪ What format is it in?
▪ Is anything missing?
▪ Do I have everything that I need?
▪ What output information am I trying to produce?
▪ What do I want the result to look like … text, a picture, a graph … ?
▪ What am I going to have to compute?

3|Page
In the example,
✓ the input is a bunch of grades/scores.
✓ we need to understand the format of the grades/scores.
✓ each grade might be a number from 0 to 100.
✓ it may be a letter grade from A+ to F.
✓ If it is a number, the grade might be a whole integer like 73 or it may be a real number
like 73.42.
✓ need to consider missing grades, if we do not have the grade for every student (e.g.,
some were away during the test) ????.
✓ understand what the output should be (output a whole or real number or a letter grade
??????)
✓ display results using charts.

The need to understand the processing part of the problem. Break the problem into smaller
problems that require some kind of simple mathematical computations in order to process the
data since we are going to compute the average. So, what is the model (or formula) for
computing the average of a bunch of numbers????? If there is no such “formula”, then, need to
develop one. Sometimes, we can look up certain formulas in a book or online if we get stuck.
To come up with a model, we need to fully understand the information available to us.
Assuming that the input data is a bunch of integers or real numbers x1,x2,…,xn representing a
grade percentage, we can use the following computational model:
Average1 = (x1 + x2 + x3 + … + xn) / n
Assuming the input number is from 0 to 100.
Conversely, the approach will not work if the input data is a set of letter grades like B-, C, A+,
F, D-, etc.. because we cannot perform addition and division on the letters. Then, thinking is
required.
Assuming, we may decide to assign an integer number to the incoming letters
as follows:
A+ = 12 B+ = 9 C+ = 6 D+ = 3 F = 0
A = 11 B=8 C=5 D=2
A- = 10 B- = 7 C- = 4 D- = 1
If we assume that these newly assigned grade numbers are y1,y2,…,yn, then we can use the
following computational model:
4|Page
Average2 = (y1 + y2 + y3 + … + yn) / n
where the result will be a number from 0 to 12.

Example 2: On school resumption day, a student is ready to go to school but yet he/she has not
picked up those books and copies which are required as per timetable. As a result, picking up
books and copies according to timetable is a kind of problem solving.
Example 3: Some students in a class plan to host an event and decide to share the expenses
among them. So, making up plans including budgets, noting all activities, risks, alternatives,
etc. is also a kind of problem solving.
The problems that we want to solve can come from any real-world problem or perhaps even
from the abstract world. We need to have a standard systematic approach to solving problems.
The three basic purposes include:
1. Seeking information
2. Generating new knowledge
3. Making decisions
Objectives of Problem-Solving
i. Willingness to try problems and improve their perseverance when solving problems.
ii. Improve the abilities to solve problems.
iii. Recognise problem-solving strategies.
iv. Understand the value of approaching problems in a systematic manner.
v. Understand that many problems can be solved in more than one way.
vi. Improve the abilities to select appropriate solution strategies.
vii. Improve the abilities to implement solution strategies accurately.
viii. Improve the abilities to get more correct answers to problems.
ix. The appreciation of the existence of a problem and a desire to solve it.
x. The accumulation of the facts and data which are pertinent to the problem.
xi. Logical interpretation of the data supported by adequate valid experience.

1.1 Identification of Problem and Problem Types

The first step to a successful problem solution is to identify the problem. A problem occurs
when there is a difference between what "should be" and what "is”, between the ideal and the
actual situation. So, problem Identification consists of clearly identifying the root cause of a
5|Page
problem. Developing a detailed problem statement that includes the problem's effect on the
affected system. Once a problem has been identified, its exact nature needs to be determined:
what are the goal and barrier components of the problem? Some of the main elements of the
problem can be outlined, and a first attempt at defining the problem should be made. This
definition should be clear enough for one to be able to easily explain the nature of the problem
to others. Identify the Problem before the Solution. When an individual faces a problem, the
natural tendency is to propose possible solutions. Consequently, the thought and discussion
focus on the merits and problems of the proposed solution(s), rather than an in-depth discussion
of the possible causes of the problem itself. Sometimes numerous negative symptoms are all
outcomes of a single root problem. So, solving the root problem will resolve many related
problems. Ask “Why? Why? Why?, Why?”

Example: Resolving work-related problems. John was told to deal with the problem of her
cashiers receiving customer complaints (the symptom) so he instituted a new policy that said,
“if an employee receives four or more customer complaints over a single pay period, (s)he will
be fired.” This no-talk policy may certainly produce short-term benefits, but if the supervisor
never identifies the lack of effective training as the real problem, then the customer complaints
are sure to continue. Firing the offending cashiers (an outcome of the new policy that treats a
symptom) will not resolve the issue if the replacement hires do not receive adequate training
(a necessary treatment for the root problem).

One may likely consider asking questions to resolve a barrier.

i. What is the problem and how did it happen?


ii. Why did it happen?
iii. How does it affect us?
iv. How can we solve the problem?
v. Which problems are solved by the solutions?
vi. Which problems are left unsolved or have potential to cause new problems?

In problem solving, the idea is to seeks to devise a method for transforming a problem from its
current state into a desired state when a solution is not immediately obvious. Thus, the hallmark
of problem solving is the invention of a new method for addressing a problem.

6|Page
1.1.1 Types of Problems

Problems may be classified as routine and non-routine.

Routine: It is referred to as routine if it follows a certain procedure (simple algorithm) or


formula to arrive at a solution. In a routine problem, the problem solver knows a solution
method and only needs to carry it out. One-step, two-step, or multiple-step routine problems
can be easily assessed with paper and pencil tests typically focusing on the algorithm or
algorithms being used. These problems are typically encountered frequently and can be solved
with minimal effort and time. Example of routine problems include mathematical computations
such as addition, subtraction, multiplication, and division. For example, for most adults the
problem "589 × 45 = ___" is a routine problem if they know the procedure for multicolumn
multiplication. Routine problems are sometimes called exercises, and technically do not fit the
definition of problem stated above. When the goal of an educational activity is to promote all
the aspects of problem solving (including devising a solution plan), then nonroutine problems
are appropriate.
Routine Problems:
• Problems that are familiar and can be solved using standard procedures or known
algorithms.
• Often encountered in daily tasks and academic exercises.
• Example: Calculating the total cost of items in a shopping list.
Characteristics of Routine Problems
• Predictability: The steps to solve the problem are known and predictable.
• Repetition: The problem type occurs frequently, allowing for familiarity and practice.
• Standard Methods: Solutions can be found using established methods or algorithms.
• Examples: Basic arithmetic, following a recipe, routine maintenance tasks.
Approaches to Solving Routine Problems
1. Identify the Problem:
o Clearly understand the problem and its requirements.
2. Select the Appropriate Method:
o Choose a standard procedure or algorithm known to solve this type of
problem.

7|Page
3. Apply the Method:
o Follow the steps systematically to arrive at the solution.
4. Verify the Solution:
o Check the result to ensure accuracy and correctness.
Example: Calculating the area of a rectangle
1. Identify the problem: Find the area of a rectangle with a length of 8 units and a width
of 3 units.
2. Select the method: Use the formula for the area of a rectangle (Area = length ×
width).
3. Apply the method: Area = 8 × 3 = 24 square units.
4. Verify the solution: Double-check the multiplication and the units.
Case Study: Routine Problem
• Scenario: A student needs to calculate their GPA for the semester.
• Solution Steps:
1. Identify the problem: Calculate the GPA.
2. Select the method: Use the GPA formula (sum of (grade points × credits) /
total credits).
3. Apply the method: Calculate using the provided grades and credits.
4. Verify the solution: Ensure all calculations are correct.
Practice Problem 1: Routine
• Scenario: Calculate the total cost of groceries if the prices are #2, #3, #4, and #5.

Non-routine: Non-routine problems are those that do not adhere to any set procedure or
formula, and the solution is not predictable. These problems require in-depth thinking, analysis,
and problem-solving skills to come up with a solution. Examples of non-routine problems
include creating a new business plan, developing a novel product, or solving a complex Math
problem. These types of problems help develop critical and creative thinking among students.
Non-routine problems enable solvers to use different solutions and approaches in attacking
the problem. It stresses the use of heuristics and often requires little to no use of algorithms.
Heuristics are procedures or strategies that do not guarantee a solution to a problem but provide
a more highly probable method for discovering the solution to a problem. For non-routine

8|Page
problem, it occurs when an individual is confronted with an unusual problem situation and is
not aware of standard procedure for solving it. This individual must create a procedure. To do
so, we must become more familiar with the problem situation, collect appropriate information,
identify an efficiency strategy, and use strategy to solve it.
Non-Routine Problems:
• Problems that are unfamiliar, complex, or lack a clear solution path.
• Require creative thinking and novel approaches.
• Example: Developing a new marketing strategy for a product.
Characteristics of Non-Routine Problems
• Unpredictability: The solution path is not clear and may require exploration.
• Novelty: The problem is unique or rare, requiring new thinking or approaches.
• Complexity: The problem may involve multiple variables and potential solutions.
• Examples: Designing an innovative product, resolving a complex conflict, strategic
planning.
Approaches to Solving Non-Routine Problems
1. Understand the Problem:
o Gather all relevant information and clearly define the problem.
2. Explore Possible Solutions:
o Brainstorm multiple approaches and consider various perspectives.
3. Develop a Strategy:
o Create a plan that outlines the steps and resources needed to tackle the problem.
4. Implement the Solution:
o Execute the plan while remaining flexible to adjust as needed.
5. Evaluate and Reflect:
o Assess the effectiveness of the solution and learn from the process.
Example: Developing a new marketing strategy
1. Understand the problem: Increase brand awareness in a competitive market.
2. Explore possible solutions: Consider social media campaigns, influencer partnerships,
and community events.
3. Develop a strategy: Create a marketing plan that combines social media, influencers,
and events, detailing the timeline and budget.

9|Page
4. Implement the solution: Launch the campaigns, monitor progress, and make
adjustments based on feedback.
5. Evaluate and reflect: Analyze the results, gather feedback, and identify areas for
improvement.
Case Study: Non-Routine Problem
• Scenario: A company faces declining sales and needs to innovate.
• Solution Steps:
1. Understand the problem: Identify reasons for declining sales.
2. Explore possible solutions: Brainstorm new product ideas, marketing
strategies, and customer engagement tactics.
3. Develop a strategy: Create a comprehensive plan that includes new product
development, marketing, and customer feedback mechanisms.
4. Implement the solution: Launch the new products and marketing campaigns.
5. Evaluate and reflect: Monitor sales performance, gather customer feedback,
and refine strategies as needed.
Practice Problem: Non-Routine
• Scenario: Design a community project to reduce plastic waste.

1.2 Method of solving computing problems (introduction to heuristics and


algorithms)

Problem solving is an essential cognitive skill humans employ in various aspects of life, from
everyday challenges to complex tasks. Different strategies are often used to find solutions. Two
commonly used strategies are heuristics and algorithm. Algorithms are systematic step-by-
step procedures that guarantee a solution. Heuristics are general rules or shortcuts that helps in
finding solutions more efficiently, though may not guarantee a correct answer.

1.2.1 Heuristics
In computer science, a heuristic is a problem-solving strategy or method that is not guaranteed
to find the optimal solution but is designed to find a satisfactory solution in a reasonable amount
of time. In other words, heuristics allow for quick solutions through exploration, intuition, and
sometimes an educated guess. As such, heuristics begin by exploring possible solutions to a

10 | P a g e
problem. The first step is to search the solution space to find an area in which the solution
might be located. Then the search for a solution is focused on this area.

The objective of a heuristic is to produce a solution in a reasonable time frame that is good
enough for solving the problem at hand. The trade-off criteria for deciding whether to use a
heuristic for solving a given problem:

i. Optimality: When several solutions exist for a given problem, does the heuristic
guarantee that the best solution will be found? Is it necessary to find the best solution?
ii. Completeness: When several solutions exist for a given problem, can the heuristic find
them all? Do we need all solutions? Many heuristics are only meant to find one solution.
iii. Accuracy and precision: Can the heuristic provide a confidence interval for the
purported solution? Is the error bar on the solution unreasonably large?
iv. Execution time: Is this the best-known heuristic for solving this type of problem?
Some heuristics converge faster than others. Some heuristics are only marginally
quicker than classic methods.

Characteristics of Heuristics

i. Speed: Provide quick solutions, useful for time-sensitive problems.


ii. Simplicity: Easy to apply and understand.
iii. Flexibility: Can be adapted to different situations.

Applications of Heuristics

Heuristics are attractive because they find solutions in a timely manner. Some examples
include:

Traveling Salesperson Problem (TSP): TSP is an optimization problem in which the


objective is to find an optimal route between a set of nodes. The question is, given a set of cities
(nodes) and distances between these cities, what is the shortest possible route that visits each
city. The TSP problem is difficult to solve. In other words, it is complex and hard to verify.
However, heuristics help us here to approximate an optimal route to all the cities.

11 | P a g e
Greedy Algorithms: Greedy algorithms attempt to find locally optimal solutions at each stage
in solving a problem. To clarify, the assumption is that a set of locally optimal solutions may
eventually lead to a globally optimal solution in the end. Hence, they are often applied to the
TSP problem.

Antivirus Software: When scanning for viruses, heuristics are used to search for samples of
code that resemble viruses in files. This significantly reduces the number of files that must be
searched for viruses.

Hill-climbing: An iterative optimization algorithm that starts with an initial solution and
incrementally improves it by making small adjustments.

Decision Making: Making quick decisions in uncertain conditions (e.g., choosing a


restaurant).

Problem Solving: Simplifying complex problems (e.g., using heuristics in game strategies).

Search and Optimization: Finding good enough solutions when exact solutions are not
feasible (e.g., heuristic search in AI).

1.2.2 Algorithm

A common type of strategy is an algorithm. In contrast to heuristics, an algorithm is a problem-


solving formula that provides a precise step-by-step instructions or procedures for solving a
specific task. It does not depend on intuition or guesses but provides instruction to obtain a
solution. Algorithms guarantee that the given set of rules will ultimately lead to the correct
answer. Think of an algorithm as a recipe with highly detailed instructions that produce the
same result every time they are performed. Algorithms are used frequently in our everyday
lives, especially in computer science.

An algorithm will usually consist of a sequence of steps with a starting point and a known
endpoint. Algorithms are usually represented using programming language, pseudocode, or
flowcharts.

12 | P a g e
Characteristics of Algorithms

• Precision: Provide exact solutions, minimizing errors.


• Reproducibility: Yield the same result when applied repeatedly.
• Complexity: Can be more complex to understand and implement.
• Examples:
o Mathematical formulas (e.g., quadratic formula)
o Computer programs (e.g., sorting algorithms like QuickSort or MergeSort)
o Step-by-step instructions (e.g., recipes)

Applications of Algorithms

Algorithms are precise, and this makes them suitable for use in various areas of computer
science. They are used to produce outputs from a given set of inputs. Some of the applications
include.

Search: Search algorithms are used to retrieve data elements, usually from data structures or
within a search space. It is important to note that they differ in the approach used for searching.
For instance, a linear search scans each element at a time, whereas a binary search splits the
search range in half and starts with the middle value.

Sorting: Sorting algorithms are used to order or arrange elements. For example, elements can
be arranged in ascending or descending order. Specifically, given an unsorted list of elements
as input, a sorting algorithm will arrange the elements and return a sorted list according to the
specified order. Some examples include quicksort, selection sort, and heapsort.

Cryptography: In cryptography, algorithms are used for authentication, encryption, and


decryption of messages. They prevent unauthorized access to data and resources. Some
examples of these are symmetric and asymmetric key algorithms.

Artificial Intelligence: In artificial intelligence, algorithms are used to train computer


programs, as well as to teach machines how to learn and function on their own. Some classes

13 | P a g e
of algorithms for artificial intelligence are supervised, unsupervised, reinforcement
learning algorithms.

Comparing Heuristics and Algorithms

• Accuracy: Algorithms provide exact solutions; heuristics offer approximate solutions.


• Speed: Heuristics are faster but less accurate; algorithms are slower but more precise.
• Complexity: Algorithms can be complex; heuristics are generally simpler.
• Use Cases: Use heuristics for quick, practical solutions; use algorithms for precise,
systematic solutions.

1.3 Solvable and Unsolvable Problems

Solvable Problem

Problems that can be solved using existing methods, tools, or algorithms. They are
computational problems that can be solved by a Turing machine. Turing machines (by Alan
Turing) are simple abstract computational devices intended to help investigate the extent and
limitations of what can be computed. Exactly solvable model in statistical mechanics, a system
whose solution can be expressed in closed form, or alternatively, another name for completely
integrable systems.

Characteristics of Solvable Problems

• Clear Problem Statement: Defined goals and constraints.


• Known Methods: Existing procedures or algorithms to find a solution.
• Verifiability: Solutions can be checked for correctness.
• Examples:
o Mathematical calculations (e.g., solving quadratic equations).
o Engineering design problems (e.g., designing a bridge to specifications).
o Computer science problems (e.g., sorting a list of numbers).

14 | P a g e
Approaches to Solvable Problems

1. Identify the Problem:


o Clearly define the problem and its constraints.
2. Select Appropriate Methods:
o Choose from known methods or algorithms suited to the problem type.
3. Implement the Solution:
o Apply the chosen methods systematically.
4. Verify the Solution:
o Check the solution for correctness and accuracy.

Example: Solving a Linear Equation

1. Identify the problem: Solve 2x+3=7.


2. Select the method: Use algebraic manipulation.
3. Implement the solution: 2x=4 then x=2.
4. Verify the solution: Substitute x back into the original equation.

Unsolvable Problem

An unsolvable problem denotes some kind of problem that does not have a step-by-step
process, or an algorithm, that can always find the right solution. In other words, no algorithm
can ever be written to find the solution. The Halting problem was used by Alan Turing as proof
that there is a category of problems that are unsolvable for a computer i.e., there is no
algorithmic solution for them.

Characteristics of Unsolvable Problems

• Ambiguous Problem Statement: Vague goals and constraints.


• Lack of Methods: No known procedures or algorithms to find a solution.
• Non-Verifiability: Solutions cannot be checked for correctness or do not exist.
• Examples:
o The Halting Problem: Determining if a computer program will eventually halt
or run forever.
15 | P a g e
o Certain philosophical questions (e.g., the meaning of life).
o Predicting the exact position and velocity of every particle in a chaotic system
over time.

Approaches to Unsolvable Problems

1. Understand the Limits:


o Recognize that a definitive solution is not possible.
2. Explore Alternative Approaches:
o Use heuristics, approximations, or simulations.
3. Redefine the Problem:
o Modify the problem to make it more tractable.
4. Focus on Insights:
o Gain a better understanding of the problem's nature and constraints.

Example: The Halting Problem

1. Understand the limits: Recognize it is impossible to determine if every program halts.


2. Explore alternatives: Focus on specific cases or use heuristics.
3. Redefine the problem: Look for ways to handle particular classes of programs.
4. Focus on insights: Study the implications and limitations of the problem.

Implications in Problem Solving

• For Solvable Problems:


o Efficient problem-solving and confidence in solutions.
o Development of systematic methods and algorithms.
• For Unsolvable Problems:
o Acceptance of limitations and focus on approximation.
o Encouragement of creativity and exploration of new approaches.
o Understanding the boundaries of current knowledge and technology.

16 | P a g e
1.4 Solution Techniques to Solving Problems
These techniques can be applied to a wide range of problems, from personal challenges to
complex business issues. They emphasize the importance of clearly defining the problem,
generating, and evaluating potential solutions, and creating an action plan for implementation.

The following techniques are usually called problem-solving strategies:

a. Abstraction: solving a problem in a model of the system before applying it to the real
system
b. Analogy: using a solution that solves an analogous problem
c. Brainstorming: suggesting many solutions or ideas and combining and developing
them until an optimum solution is found (particularly among groups of people).
d. Divide and conquer: breaking down a large, complex problem into smaller, solvable
problems.
e. Hypothesis testing: assuming a possible explanation to the problem and trying to prove
(or, in some contexts, disprove) the assumption.
f. Lateral thinking: approaching solutions indirectly and creatively.
g. Means-ends analysis: choosing an action at each step to move closer to the goal.
h. Method of focal objects: synthesizing seemingly non-matching characteristics of
different objects into something new.
i. Morphological analysis: assessing the output and interactions of an entire system.
j. Proof: try to prove that the problem cannot be solved. The point where the proof fails
will be the starting point for solving it.
k. Reduction: transforming the problem into another problem for which solutions exist
l. Research: employing existing ideas or adapting existing solutions to similar problems
m. Root cause analysis: identifying the cause of a problem.
n. Trial-and-error: testing possible solutions until the right one is found.

17 | P a g e
Lecture 2

Problem Solving Process Steps

Practicing different problem solving strategies as highlighted in lecture 1 can help individuals
develop efficient solutions to challenges encountered every day. Organisation, business,
institution, and career have unique challenges, which means employees may implement
different strategies to solve them. For those who are interested in learning how to solve
problems more effectively, understanding how to implement several common problem solving
strategies may be of immense benefit.

Objectives:
After going through this topic, students would be able to understand:
➢ General problem-solving process steps.
➢ Advantages of problem-solving process
➢ Common problem-solving strategies

2.1 General Problem-Solving Process Steps


i. Understand the problem. It is basic to comprehend the nature of a problem and its
related goals. Encourage students to frame a problem in their own words.
ii. Describe any barriers. There is need to understand barriers or constraints that may be
preventing from achieving their goal. What is creating the problem? Verbalize these
impediments - always an important step.
iii. Identify various solutions. After the nature and parameters of a problem are
understood, then, choose one or more suitable strategies to aid resolve the problem.
Here are some problem-solving possibilities:
a. Create visual images. Many problem-solvers find it useful to create “mind
pictures” of a problem and its potential solutions prior to working on the
problem. Mental imaging allows the problem-solvers to map out many
dimensions of a problem and “see” it clearly.
b. Guesstimate. Engage in some trial-and-error approaches to problem-solving. It
should be understood, however, that this is not a singular approach to problem-
solving but rather an attempt to gather some preliminary data.

18 | P a g e
c. Create a table. A table is an orderly arrangement of data. Creating tables of
information, help to group and organize most data relative to a problem.
d. Look for a pattern. Looking for patterns is an important problem-solving
strategy because many problems are similar and fall into predictable patterns. A
pattern, by definition, is a regular, systematic repetition and may be numerical,
visual, or behavioural.
e. Create a systematic list. Recording information in list form is a process used
quite frequently to map out a plan of attack for defining and solving problems.
Encourage recording ideas in lists to determine regularities, patterns, or
similarities between problem elements.
iv. Try out a solution and implement. When working through a strategy or combination
of strategies, it will be important to …
a. Keep accurate and up-to-date records of thoughts, proceedings, and
procedures. Recording the data collected, the predictions made, and the
strategies used is an important part of the problem-solving process.
b. Try to work through a selected strategy or combination of strategies until
it becomes evident that it's not working, it needs to be modified, or it is
yielding inappropriate data. As students become more proficient problem
solvers, they should feel comfortable rejecting potential strategies at any time
during their quest for solutions.
c. Monitor with great care the steps undertaken as part of a solution.
Although it might be a natural tendency to “rush” through a strategy to arrive at
a quick answer, it is vital observe assess and monitor progress.
d. Feel comfortable putting a problem aside for a period and tackling it later.
For example, scientists rarely come up with a solution the first time they
approach a problem. Problem solvers should also feel comfortable letting a
problem rest for a while and returning to it later.
e. Implement the Solution: Develop a plan and execute it. Ensure all
stakeholders are on board.
v. Evaluate the results/ monito and review. It is important to have multiple opportunities
to assess problem-solving skills and the solutions generated from using those skills.
The process of self-assessment is not easy, however. It involves risk-taking, self-
assurance, and a certain level of independence. But it can be effectively promoted by
asking questions such as “How do you feel about your progress so far?” “Are you
19 | P a g e
satisfied with the results you obtained?” and “Why do you believe this is an appropriate
response to the problem?” Evaluate the effectiveness of the solution. Make necessary
adjustments.
Case Studies and Examples
Example 1: Problem: A student is struggling to manage their time effectively. Solution Steps:
a. Identify the problem: Poor time management.
b. Analyze: Determine which activities consume most time.
c. Generate solutions: Create a timetable, prioritize tasks.
d. Evaluate: Choose the most realistic timetable.
e. Implement: Follow the timetable.
f. Monitor: Adjust as necessary based on effectiveness.
Example 2: Problem: A company is facing a decline in sales. Solution Steps:
a. Identify the problem: Declining sales.
b. Analyze: Research market trends, customer feedback.
c. Generate solutions: Improve product quality, launch marketing campaigns.
d. Evaluate: Assess cost-effectiveness and potential impact.
e. Implement: Roll out the marketing campaign.
f. Monitor: Track sales performance and customer response.

2.2 Advantages of Problem-Solving Process


Problem solving often incorporates logical reasoning and interpretation of meanings behind the
problem, and in many cases require abstract thinking and creativity to find novel solutions.
Understanding how a variety of problem-solving strategies work is important because different
problems typically require different approach to find the best solution. Mastering several
problem-solving strategies will help individuals to develop stronger critical thinking.

i. Development of Long-Term Knowledge Retention – the ability to retain and recall


information because of open discussion between peers reinforces understanding of
subject matter.

20 | P a g e
ii. Use of Diverse Instruction Types - Grouping “students” together allows them to work
together and enjoy team-based learning. Content as video, news article and more could
also be provided.
iii. Continuous Engagement – It is not hard to see the potential for engagement, as
students collaborate to solve real-world problems that directly affect or heavily interest
them.
iv. Development of Transferable Skills – It allows learning to become more profound
and durable, helping students apply skills they develop to other real-world scenarios.
v. Improvement of Teamwork and Interpersonal Skills – promotes interaction and
communication, leading to building skills related to teamwork and collaboration.

2.3 Commonly Problem-solving Strategies


Refer to lecture 1 for these strategies. Just to highlight few.

Table2.1: Some commonly used problem-solving strategies.

Method Description Example


Trial and error Continue trying different Restarting phone, turning off
solutions until problem is WiFi, turning off Bluetooth
solved to determine why your phone
is malfunctioning
Algorithm Step-by-step problem- formula Instruction manual
solving for installing new software
on your computer
Heuristic General problem-solving Working backwards;
framework breaking a task into steps
Means-ends analysis Analysing a problem at Envisioning the goal and
series of smaller steps to determining the best strategy
move closer to the goal for attaining it in the current
situation

2.4 Practice Problems


Problem 1: Scenario: You are assigned a group project, but one member is not contributing.
• Identify: Lack of contribution from a group member.
• Analyze: Determine reasons for lack of contribution (e.g., lack of understanding,
personal issues).
• Generate: Solutions could include redistributing tasks, offering help, or discussing
with the member.

21 | P a g e
• Evaluate: Select the best approach that ensures project completion.
• Implement: Communicate the plan and execute it.
• Monitor: Regularly check progress and make adjustments as needed.
Problem 2: Scenario: Your computer is running slowly.
• Identify: Slow computer performance.
• Analyze: Identify potential causes (e.g., too many programs running, malware).
• Generate: Solutions might include closing unnecessary programs, running a virus
scan, upgrading hardware.
• Evaluate: Choose the most effective and feasible solution.
• Implement: Take the necessary steps to improve performance.
• Monitor: Assess if the computer's performance has improved.

22 | P a g e
Lecture 3

The Role of Algorithms in Problem-solving


Students have a challenge! They need to thank the school management for providing well
equipped computer lab including public address system for them. How do they appreciate
these? They could send thank you note via mail. They could call the Head of Department (HoD)
on phone. They could send email message. They could send representatives to HoD office. In
fact, there are many ways to solve this problem; however, that is not the point. What is
important is that they must decide how to solve the problem and as well, use appropriate tool
to implement their plan.
Assuming representatives are sent to HoD office to deliver the “thank you”. So, the
representatives are the tool but need to take instruction from the whole students. They have
their mandate on what to say, the time to go, and the message to be delivered. A computer
program is like the instruction given to the representatives. The student story is a useful view
concerning computers and computer programs. Every problem solution starts with a plan. That
plan is called an algorithm.

Objectives

After completing this chapter, students will be able to:


• Understand the concept of algorithm.
• Know properties of an algorithm.
• Understand common elements of algorithm.
• Know types of algorithms.
• Understand importance of algorithm.
• Know different ways to represent algorithms.
• Understand search algorithms.
• Know quadratic sort algorithms.

3.1 Understanding the concept of algorithm.

Algorithm is a set of step-by-step measures or rules followed to complete a given task or solve
a particular problem. It is “a process that performs some sequence of operations in order to

23 | P a g e
solve a given problem”. The set of rules must be unambiguous and have a clear stopping point.
Note that there might be other ways to solve a problem; as a result, there may be more than one
algorithm for a problem. Algorithms are used for calculation, data processing, and many other
fields. They are essential because, algorithms provide the organized procedures that computers
require. A computer program is basically an algorithm that tells computer what specific tasks
to perform in what specific order to accomplish a specific task. A good algorithm is like using
the exact tool in a workshop. Jobs are done effortlessly. Using the erroneous algorithm or one
that is not plainly defined is like trying to cut a piece of plywood with a pair of scissors: though
it may get done, but how effective could it be in completing it. So, when solving a problem,
the key to arriving at the best solution is often choosing the right approach.

Example 1: Sort a sequence of numbers into non-decreasing order. This problem arises
regularly in practice and hence, a standard design techniques and analysis tools would suffice.
Let formally define the sorting problem:

Input: A sequence of 𝑛 numbers 〈𝑎1, 𝑎2, ⋯, 𝑎𝑛 〉.

Output: A reordering 〈𝑎1 ′, 𝑎2 ′, ⋯, 𝑎𝑛 ′ 〉 of the input sequence such that 𝑎1 ′ ≤ 𝑎2 ′ ≤ ⋯ ≤ 𝑎𝑛


′. For example, given the input sequence 〈31; 41; 59; 26; 41; 58〉, a sorting algorithm returns
as output the sequence 〈26; 31; 41; 41; 58; 59〉.

Example 2: Problem to solve: Write an algorithm to read two numbers and find their sum. Inputs to
the algorithm (accept two inputs):

• First num1
• Second num2
Expected output: Sum of the two numbers
Algorithm:
• Step1: Start
• Step2: input the first num1.
• Step3: input the second num2.
• Step4: Sum = num1+num2
• Step5: Print Sum
• Step6: End

24 | P a g e
3.2 Properties of an Algorithm

An Algorithm must possess the following properties:

Figure 3.1: properties of algorithm.

i. Finiteness: An algorithm must always terminate after a finite number of steps. It means
after every step one reach closer to solution of the problem and after a finite number of
steps algorithm reaches to an end point.
ii. Definiteness: Each step of must be precisely defined. Actions to be carried out must to
be unambiguously specified for every activity.
iii. Input: An algorithm should have some inputs. The values are given to the algorithm
before it begins.
iv. Output: An algorithm has one or more outputs after completing a specified task based
on the inputs.
v. Effectiveness: Algorithm operations should be basic and done exactly in a finite
amount of time by a person with pen/paper.

3.3 Common Elements of Algorithms

i. Acquire data (input): Some means of reading values from an external source; most
algorithms require data values to define the specific problem.
ii. Computation: Performing arithmetic computations, comparisons, testing logical
conditions, etc.
iii. Selection: Choosing among two or more possible courses of action, based upon initial
data, user input and/or computed results.
25 | P a g e
iv. Iteration: Repeatedly executing a collection of instructions, for a fixed number of
times or until some logical condition holds
v. Report results (output): Reporting computed results to the user or requesting
additional data from the user.

3.4 Types of Algorithms

Algorithms are classified according to what they are being used to achieve. The basic types of
computer science algorithms include:
i. Divide and conquer algorithms – divide the problem into smaller sub problems of the
same type; solve those smaller problems and combine those solutions to solve the
original problem.
ii. Brute force algorithms – try all possible solutions until a satisfactory solution is found.
iii. Randomized algorithms – use a random number at least once during the computation
to find a solution to the problem.
iv. Greedy algorithms – find an optimal solution at the local level with the intent of
finding an optimal solution for the whole problem.
v. Recursive algorithms – solve the lowest and simplest version of a problem then, solve
increasingly larger versions of the problem until the solution to the original problem is
found.
vi. Backtracking algorithms – divide the problem into sub problems, each which can be
attempted to be solved; however, if the desired solution is not reached, move backwards
in the problem until a path is found that moves it forward.
vii. Dynamic programming algorithms – break a complex problem into a collection of
simpler sub problems, and then solve each of those sub problems only once, storing
their solution for future use instead of re-computing their solutions.

3.5 Importance of Algorithm


The algorithmic thinking is vital in many different fields. Some of the benefits are:
i. Allows students to breakdown problems and conceptualize solution in distinct steps.
ii. Being able to understand and implement an algorithm necessitates students to exercise
planned assessment and reasoning abilities.
iii. By using an algorithm, decision making becomes a more rational process.

26 | P a g e
iv. Makes PS process more efficient and consistent.
3.6 Expressing/Representing Algorithms
Algorithms can be expressed in many notations including:
i. Natural languages: tend to verbose and ambiguous and rarely used for complex or
technical algorithms.
ii. Programming language: intended for expressing algorithm in the form that can be
executed by a computer.
iii. Flow charts and Pseudo codes: both are structured ways to express algorithms that
avoid many ambiguities common in natural language statements, while remaining
independent of a particular implementation language.

Sometimes it is helpful in the description of an algorithm to supplement small


flowcharts with natural language and/or arithmetic expressions written inside block
diagrams to summarize what the flowcharts are accomplishing.

More examples of algorithms


Write an algorithm for the following.
i. Write an algorithm to calculate the simple interest using the formula:
𝑆𝑖𝑚𝑝𝑙𝑒 𝑖𝑛𝑡𝑒𝑟𝑒𝑠𝑡 = 𝑃 ∗ 𝑁 ∗ 𝑅/100, Where 𝑃 is principal amount, 𝑁 is the number of
years and 𝑅 is the rate of interest.
Algorithm to calculate Simple Interest
Step 1: Start
Step 2: Read the three input quantities’ P, N and R.
Step 3: Calculate simple interest as Simple interest = P* N* R/100
Step 4: Print simple interest.
Step 5: Stop.
ii. Write an algorithm to find the area of the triangle.
Let 𝑏, 𝑐 be the sides of the triangle 𝐴𝐵𝐶 and 𝐴 the included angle between the given
sides.
Algorithm to find Area of Triangle.
Step 1: Start
Step 2: Input the given elements of the triangle namely sides 𝑏, 𝑐 and angle between
the sides 𝐴 (Process: Area = 1/2 ∗ 𝑏 ∗ 𝑐 ∗ 𝑠𝑖𝑛(𝐴))
27 | P a g e
Step 3: Output the Area
Step 4: Stop.

3.7 Search Algorithms

A search algorithm is an algorithm that is designed to solve search problems1. Search


algorithms are designed to check or retrieve an element from any data structure where that
element is being stored. In other words, search algorithms are procedures used to find specific
data within a data structure or to navigate through problem spaces. They are fundamental in
computer science for tasks such as data retrieval, pathfinding, and artificial intelligence.

Types of Search Algorithms

Discussion will be based on these important types of search algorithms: linear or sequential
search and binary search, and quadratic sorting algorithm (selection and insertion).

3.7.1 Linear or Sequential Search

Linear searching techniques are the simplest technique. It is also known as sequential search.
In this technique, the items are searched one by one. The procedure is also applicable for
unsorted data set. The algorithm works by sequentially iterating through the whole array or list
from one end until the target element is found. If the element is found, it returns its index, else
-1.

Example:

Given the array; arr = [3, 11, 13, 10, 8, 21, 35] and suppose the target element to search is 8.

Approach for Linear or Sequential Search

i. Start with index 0 and compare each element with the target.
ii. If the target is found to be equal to the element, return its index (index for 8 = 4).

1
https://en.wikipedia.org/wiki/Search_algorithm

28 | P a g e
iii. If the target is not found, return -1

3.7.2 Binary Search

This type of searching algorithm is used to find the position of a specific value contained in a
sorted array. In this procedure, the entire list is divided into two sub-lists. If the item is found
in the middle position, it returns the location, otherwise jumps to either left or right sub-list and
do the same process again until finding the item or exceed the range2.

Example 1

Given the array: arr = [2, 12, 15, 17, 27, 29, 45], suppose the target element to be searched is
17.

Approach for Binary Search

i. Compare the target element with the middle element of the array.
ii. If the target element is greater than the middle element, then the search continues in
the right half.
iii. Else if the target element is less than the middle value, the search continues in the left
half.
iv. This process is repeated until the middle element is equal to the target element, or the
target element is not in the array.
v. If the target element is found, its index is returned, else -1 is returned.

Example 2

Input and Output

Input: A sorted list of data: 10 15 38 42 67 78 85 98

The search key 78

Output: Item found at location: 5

2
https://www.tutorialspoint.com/Binary-Search

29 | P a g e
Applications:

• Data retrieval in databases.


• Searching in large datasets.
• Efficient data lookup.

3.8 Quadratic sorting Algorithm

Sorting is one of the frequently needed algorithms in program design. It is essential in many
application scenarios and allows data set to be sorted into order. Sorting algorithms are
procedures used to reorder elements in a list or array into a specific sequence, usually in
ascending or descending order. Sorting makes further processing easier. For example, binary
search is possible only when an array has been sorted.

Importance:

• Facilitates efficient data retrieval and processing.


• Essential for various applications in databases, networking, and software development.
• Optimizes performance and resource usage.

The discussion will be centred on two quadratic sorting methods: selection and insertion sort.

3.8.1 Selection sort

Selection Sort starts at the beginning of the array and loops through every value looking for the
smallest value. When it finds the smallest value, it saves that values index. At the end of each
iteration, smallest value at the index of the top loop. Also, it traverses the array, finds the largest
element, and exchanges it with the one at the last position. In the second iteration, it swaps the
second largest with the second last position. As the iterations proceed, the sorted part (right) of
the array grows while the non-sorted part (left) diminishes.

Characteristics:

• Divides the list into a sorted and an unsorted region.


• Repeatedly selects the smallest (or largest) element from the unsorted region and moves
it to the end of the sorted region.
30 | P a g e
The following example demonstrates the working of the algorithm for the input array
{50,30,40,10,20}.

Iteration State of the array


Computation
i=0 {50,30,40,10,20}
max = 50; maxpos = 0; lastpos = 4;
i=1 {20,30,40,10,50}
max = 40, maxpos = 2; lastpos = 3;
i=2 {20,30,10,40,50}
max = 30, maxpos = 1; lastpos = 2;
i=3 {20,10,30,40,50}
max = 20, maxpos = 0, lastpos = 1;
i=4 {10,20,30,40,50}

Figure 3.2: selection sorting example

Applications:

• Situations where memory space is limited.


• Small datasets and simple sorting needs.

3.8.2 Insertion sort

Insertion Sort begins by looking at the second value in the array and compares it to the value
before it. If the value before it is bigger, place it one spot to the right. Do same, compare against
the previous elements one-by-one and insert it at the appropriate position.

Characteristics:

• Builds the final sorted list one item at a time.


• Takes each element from the unsorted region and inserts it into the correct position in
the sorted region.

The following example demonstrates the working of insertion sort for the array {5,2,4,6,1,3}.

31 | P a g e
State of the Computation
Iteration array
temp = 2; 2 < 5; Right shift 5; A[0] = 2;
i=1 {5,2,4,6,1,3} temp = 4; 4 < 5, 4 > 2; Right shift 5; A[1] = 4
i=2 {2,5,4,6,1,3} temp = 6; 6 > 5;
i=3 {2,4,5,6,1,3} temp = 1; 1 < 6, 1 < 5, 1 < 4, 1 < 2; Right shift 2, 4, 5,
i=4 {2,4,5,6,1,3} 6; A[0] = 1
i=5 {1,2,4,5,6,3} temp = 3; 3 < 6, 3 < 5, 3 < 4, 3 > 2; Right shift 4, 5, 6;
{1,2,3,4,5,6} A[2] = 3
Figure 3.3: Example of insertion sort.
Applications:
• Small datasets.
• Nearly sorted datasets where it performs efficiently.
• Scenarios where simplicity and in-place sorting are required.

One common problem in science has been the overload that occurs when the demand on
working memory exceeds its capacity. Solution to this overload is to help students build
strategies that decrease a task's demand on working memory. These strategies have been
described as the tricks for simplifying problems. Problem solvers go through certain steps while
working a problem. These may be as follows.
✓ begin by reading the problem.
✓ perhaps more than once
✓ then write down hopefully, the key information.
✓ reread the question or a part of the question.
✓ draw a picture to help represent the question.
✓ then try something.
✓ try something else.
✓ look at where this leads.
These strategies are algorithms that automate individual steps in solving problem. Thus,
gradually exploring or playing with the question, individuals got closer and closer to the
answer.

32 | P a g e
Lecture 4

Flowchart
The task of computer programming rests on the ability of the computer programmer to properly
make use of the initial analysis and design tools available to him/her. Programming is one of
the essential components of the broad task of system analysis and design. For analysis and
design work to be useful, it must be expressed in one understandable form or the other. In
general practice, analysts and programmers make use of diagrams and charts as important tools.
This unit introduces you to the use of flowcharts, specifically, program flowchart.

A flowchart is a type of diagrammatic representation of an algorithm. It shows workflow, or


process, presenting steps as boxes of various kinds, and their order by connecting them with
arrows. Flowcharts are used in analysing, designing, documenting, or managing a process or
program in several fields. Like other types of diagrams, they help picture what is going on and
thereby help comprehend a process.

Objectives

At the end of this chapter, students will be able to:


• Understand flowchart.
• Understand flowchart types
• Understand flowchart symbols.
• Know general rules of flowcharting.
• Construct simple flowcharts for simple programs
4.1 Concept of flowchart.

Flowchart is a diagram that depicts the flow of data in sequential order through processing
systems. In other words, it makes operations and the sequence of these operations in a system
known. Flow charts are used in different fields to document study, plans, improve and
communicate difficult processes in clear, self-explanatory diagrams. It could be seen as an
outline of a design for solving a problem.
It uses rectangles, ovals, diamonds, and potentially other shapes to define the type of step, along
with connecting arrows to define flow and sequence. They can range from simple, hand-drawn

33 | P a g e
charts to comprehensive computer-drawn diagrams depicting multiple steps and routes. If we
consider all the various forms of flowcharts, they are one of the most common diagrams on the
planet, used by both technical and non-technical people in numerous fields.
Flowcharts are usually drawn at the initial stage of formulating computer solutions. They play
a significant role in the programming of a problem and aid in comprehending the logic of
complex and extensive problems. Once flowchart is drawn, it becomes easy to write programs.
4.2 Types of Flowcharts
Under the broad area of System Analysis and Design of which programming is a component,
diagrams and charts used by analysts and programmers can be classified as follows in brief:

Data Flow Diagram (DFD): A data-flow diagram is a way of representing a flow of data
through a process or a system (usually an information system). The DFD also provides
information about the outputs and inputs of each entity and the process itself. A data-flow
diagram has no control flow - there are no decision rules and no loops. A data flow diagram
(DFD) maps out the flow of information for any process or system. It uses defined symbols
like rectangles, circles and arrows, plus short text labels, to show data inputs, outputs, storage
points and the routes between each destination. Data flowcharts can range from simple, even
hand-drawn process overviews, to in-depth, multi-level DFDs that dig progressively deeper
into how the data is handled.

Figure 4.1: Common DFD symbols3

3
https://en.wikipedia.org/wiki/Data-flow_diagram

34 | P a g e
System Flowchart: A system flowchart shows the path taken by data in a system and the
decisions made during different levels. Different symbols are combined to show data flow,
including what happens to data and where it goes.

Forms Flowcharts: The DFDs and system flowcharts already considered give no indication
of the units of an organization that perform the data processing task and which units use the
information. However, Forms Flowcharts are simply employed to supply this information of
how documents and forms flow among the organizational units. They do not indicate how data
are processed.

Program Flowcharts: Program flowchart shows control over a program in a system. This
flowchart is very important while writing code or a program. It shows how data flows while
writing an algorithm. As stated, focus is on the program flowchart, which describes what
operations (and in what sequence) are required to solve a given problem. The program
flowchart can be likened to the blueprint of a building. A designer draws a blueprint before
starting construction on a building. Similarly, a programmer prefers to draw a flowchart prior
to writing a computer program. As in the case of the drawing of a blueprint, the flowchart is
drawn according to defined rules and using standard flowchart symbols prescribed by the
American National Standard Institute (ANSI), Inc.

4.3 Flowchart Symbols


Flowcharts are generally drawn by means of some standard symbols; nevertheless, some
special symbols can also be developed when needed. Below are some standard symbols that
are frequently used in many computer programs.

35 | P a g e
Figure 4.1: Flowchart symbols

4.4 General Rules for flowcharting


The basic guidelines for drawing a flowchart with the above symbols include:
i. In drawing a proper flowchart, all necessary requirements should be listed out in logical
order.
ii. The flowchart should be neat, clear, and easy to follow. There should not be any room
for ambiguity in understanding the flowchart.
iii. All boxes of the flowchart are connected with arrows. (Not lines)

36 | P a g e
iv. Flowchart symbols have an entry point on the top of the symbol with no other entry
points. The exit point for all flowchart symbols is on the bottom except for the Decision
symbol.
v. For Decision symbol, only one flow line can enter it but with two exit points; these can
be on the sides or the bottom and one side.
vi. Generally, a flowchart will flow from left to right or top to bottom. However, an upward
flow can be shown as long as it does not exceed 3 symbols.
vii. Connectors are used to connect breaks in the flowchart. Examples are:
a. From one page to another page.
b. From the bottom of the page to the top of the same page.
c. An upward flow of more than 3 symbols
viii. Subroutines and Interrupt programs have their own and independent flowcharts.
ix. All flow charts start with a terminal or predefined process (for interrupt programs or
subroutines) symbol.
x. All flowcharts end with a terminal or a contentious loop.

Flowcharting uses symbols that have been in use for a number of years to represent the type of
operations and/or processes being performed. The standardised format provides a common
method for people to visualise problems together in the same manner. The use of standardised
symbols makes the flow charts easier to interpret; however, standardizing symbols is not as
important as the sequence of activities that make up the process.

Examples of flowchart.

1. Consider finding the sum, average and product of 3 numbers given by the user.

Algorithm for the given problem is as follows:

Read X, Y, Z

Compute Sum (S) as X + Y + Z


Compute Average (A) as S / 3
Compute Product (P) as X x Y x Z
Display the Sum, Average and Product.

37 | P a g e
Flowchart for the above problem

Figure 4.2: Sum, average and product of three number

2. To comprehend flowchart techniques, let’s examine the figure below.

38 | P a g e
Figure 4.3: Flowchart connector technique

The figure shows basic flowchart process. Recall what circle shape stands for “connector”.
Here, it connects and extends processes from start to end.

3. Consider finding the largest number between A and B

Algorithm for the above problem is as follows:

Read A, B
If A is less than B
BIG=B
SMALL = A
Else
BIG=A
SMALL = B
Display BIG, SMALL
Flowchart for the above algorithm will look like:

39 | P a g e
Figure 4.4: Largest between A and B

Find the area of a circle of radius r.

Figure 4.5: Area of a circle.

40 | P a g e
4.5 Forms of Flowcharts
High-Level Flowchart: A high-level (also known as first-level or top-down) flowchart
illustrates the major steps in a process. It can also include the intermediate outputs of each step
(the product or service produced), and the sub-steps involved.
This type of flowchart presents fundamental picture of the process and identifies the changes
taking place within the process. It is very handy for identifying appropriate team members
(those who are involved in the process) and for developing indicators for checking the process
because of its focus on intermediate outputs.
Most processes can be effectively presented in four or five boxes that symbolize the major
steps or activities of the process.

Figure 4.6: High-level flowchart

Detailed Flowchart: It provides a detailed picture of a process by mapping all of the steps
and activities that occur in the process. This type of flowchart indicates the steps or activities
of a process and includes such things as decision points, waiting periods, tasks that frequently
must be redone (rework), and feedback loops. This type of flowchart is useful for examining
areas of the process in detail and for looking for problems or areas of inefficiency.
Given below is the Detailed Flowchart of an Order Filling Process which shows the sub-steps
involved in the process and also reveals the delays that occur when the materials required are
not available in the inventory.

41 | P a g e
Figure 4.7: Detailed flowchart.

4.6 Advantages of Using Flowcharts


Flow chart is used for representing algorithm in symbolic form. These pictorial representations
of a solution/system have many advantages. These include:
i. Communication: A Flowchart can be used as a means of communicating the logic of
a system and steps involve in the solution in a clearer manner.
ii. Effective analysis: A flowchart of a problem can be used for effective analysis of the
problem.

42 | P a g e
iii. Documentation of Program/System: Program flowcharts are a vital part of good
program documentation. Program document is used for various purposes like knowing
the components in the program, complexity of the program etc.
iv. Efficient Program Maintenance: A developed, and operational program is easier to
maintain with the help of a flowchart.
v. Coding of the Program: It is a good practice to draw flowcharts indicating solutions
for problems. Any design of solution of a problem is finally converted into computer
program with ease.
vi. Proper Debugging: The flowchart helps in debugging process.

43 | P a g e
Lecture 5
Pseudocodes
Pseudocode is a technique used to describe the distinct steps of an algorithm in a manner that
is easy to understand for anyone with basic programming knowledge. In the early state of
problem solving, it helps a lot if hassle of having to be bound by the syntax rules of a specific
programming language are eliminated towards confirming an algorithm.

Objectives

Students should at the end of the chapter understand the following:

• What is pseudo code and why is it useful for writing computer programs?
• Solving programming problems with pseudocodes

5.1 Pseudocodes: Meaning and definition.


Pseudocode is a term which is often used in programming and algorithm-based fields. It is a
methodology that allows the programmer to represent the implementation of an algorithm.
Literally, pseudo code means “fake code”. It is an artificial and informal approach to writing
sequence of actions and instructions (algorithms) in the form that humans can understand with
ease. In other word, it is a means of describing computer algorithms using a combination of
natural language and programming language. More, Pseudocode is a simplified, informal way
of describing the steps in an algorithm using plain language and a structured format. It is not
actual code but a high-level representation of the logic that can be easily translated into any
programming language. These aid programmers concentrate on the organization and sequence
of a computer algorithm without the need for following the exact coding syntax.

In pseudocode, you do not have to think about semi-colons, curly braces, and the syntax for
arrow functions, how to define promises, and other core language principles. It is simply an
implementation of an algorithm in the form of comments and informative text written in plain
English. It has no syntax like any of the programming language and thus cannot be compiled
or interpreted by the computer. It does not matter what language you use to write your
pseudocode. All that matters is comprehension.

44 | P a g e
Importance

• Helps in designing algorithms without worrying about syntax.


• Facilitates communication between programmers and non-programmers.
• Simplifies the debugging process and enhances understanding of the algorithm.

Characteristics of Pseudocode

• Language-Agnostic (language-neutral): Written in plain language, not tied to any


specific programming language.
• Structured: Follows a logical, step-by-step format.
• Clear and Concise: Focuses on the logic and flow of the algorithm without
unnecessary details.
• Readable: Easy to understand and interpret by anyone familiar with basic
programming concepts.

5.2 Benefits of Writing Pseudocode

i. Using familiar language devoid of core programming syntax and coding patterns
ii. No official rules to obey.
iii. Acts as a bridge between human brain and computer code executor. Allows plans for
instructions which follow logical pattern without including technical details.
iv. Great way to get started with software programming as a beginner.
v. It significantly aids problem solving as it involves pragmatic approach.
vi. Makes one take full control and can explain each line of codes.

5.3 Solving Programming Problems with Pseudocode

There are no set of rules for pseudocodes exact execution though it is frequently used.
However, below are steps to solving programming with pseudocode.

• Understand what the function does.

45 | P a g e
It is important to understand what a function does. That is, (optionally) accept data as
input compute the data and produce an output. The body of the function is what actually
solves the problem, and it does so line by line.
• Make sure you understand the question.
Next, read and understand the question properly. This is arguably the most important
step in the process. Adequate problem comprehension helps to figure out all possible
steps towards solutions.
• Break the problem down.
Break down the problem into smaller parts and sub-problems. With each smaller
problem you solve, you'll get closer to solving the main problem.
• Outline the Steps: Write down the steps needed to solve each part of the problem.
• Use Simple Language: Use plain language to describe each step without worrying
about programming syntax.
• Ensure Logical Flow: Make sure the steps follow a logical sequence and cover all
possible scenarios.

Once the problem is broken down to the work package, it helps to represent these problem-
solving steps in the clearest and most easily understandable way. It is an intermittent step
towards developing the real code. So, programming is the expression of logic. A program is a
set of instructions that defines how software behaves. Thus, coding is how it is implemented in
the computer system.

Best Practices for Writing Pseudocode

• Keep It Simple: Use simple and clear language.


• Be Consistent: Use consistent naming conventions and structure.
• Focus on Logic: Ensure the logical flow of the algorithm is clear.
• Avoid Syntax: Do not worry about specific programming language syntax.
• Review and Refine: Review the pseudocode for completeness and accuracy.

Worked examples.

1. Illustrating flowchart and pseudocode.

For a given value, Limit, what is the smallest positive integer Number for which the sum.

46 | P a g e
Sum = 1 + 2 + ... + Number is greater than Limit. What is the value for this Sum?

Flowchart:

47 | P a g e
Pseudocode:

Input: An integer Limit


Ouput: Two integers: Number and Sum

Enter Limit
Set Number = 0.
Set Sum = 0.
Repeat the following:
a. If Sum > Limit, terminate the repetition, otherwise.
b. Increment Number by one.
c. Add Number to Sum and set equal to Sum.
Print Number and Sum.

2. Write pseudocode that reads two numbers and multiplies them together and print out
their product.
READ 𝑛𝑢𝑚1, 𝑛𝑢𝑚2
SET product to 𝑛𝑢𝑚1* 𝑛𝑢𝑚2
Write product

3. Write pseudocode that tells a user that the number they entered is not a 5 or a 6

READ isfive
IF (isfive = 5)
WRITE "your number is 5"
ELSE IF (isfive = 6)
WRITE "your number is 6"
ELSE
WRITE "your number is not 5 or 6"
END IF
4. Write pseudocode that will count all the even numbers up to a user defined stopping
point.

GET count
SET x to 0;
WHILE (x < count)
SET even to even + 2
ADD 1 to x
WRITE even
END WHILE

48 | P a g e
Lecture 5: Programming Languages
Objectives
At the end of the lecture, students should be able to:
• Understand computer program
• Know programming language and its types
• Understand computer programming

5.1 Computer Program


A program is a set of instructions following the rules of the chosen language. Without
programs, computers are useless. A program is like a recipe, it contains a list of ingredients
(called variables) and a list of directions (called statements) that tell the computer what to do
with the variables.

5.2 Programming Language


A vocabulary and set of grammatical rules (syntax) for instructing a computer to perform
specific tasks. Programming languages can be used to create computer programs.

5.2.1 Common Programming Paradigms


Now that we have introduced what programming paradigms, let us consider the most popular
ones, explain their main characteristics, and compare them. Keep in mind this list is not
exhaustive. There are other programming paradigms not included here. It can be shown that
anything solvable using one of these paradigms can be solved using the others; however, certain
types of problems lend themselves more naturally to specific paradigms

• Imperative Programming

Imperative programming consists of sets of detailed instructions that are given to the computer
to execute in a given order. Control flow in imperative programming is explicit: commands
show how the computation takes place, step by step. Each step affects the global state of the
computation. It is called "imperative" because programmers dictate exactly what the computer
has to do, in a very specific way. Imperative programming focuses on describing how a
program operates, step by step. Example; compute result of certain number of people.

49 | P a g e
result = []
i=0
start:
numPeople = length(people)
if i >= numPeople goto finished
p = people[i]
nameLength = length(p.name)
if nameLength <= 5 goto nextOne
upperName = toUpper(p.name)
addToList(result, upperName)
nextOne:
i=i+1
goto start
finished:
return sort(result)

• Procedural Programming

Procedural programming is a derivation of imperative programming, adding to it the feature of


functions (also known as "procedures" or "subroutines"). In procedural programming, the user
is encouraged to subdivide the program execution into functions, as a way of improving
modularity and organization. It is an Imperative programming with procedure calls. Example,
baking a cake, processes may be divided into separate functions groups;

function pourIngredients() {
- Pour flour in a bowl
- Pour a couple eggs in the same bowl
- Pour some milk in the same bowl
}
function mixAndTransferToMold() {
- Mix the ingredients
- Pour the mix in a mold
}
function cookAndLetChill() {
- Cook for 35 minutes
- Let chill
}
pourIngredients()
mixAndTransferToMold()
cookAndLetChill()

50 | P a g e
The functions could be written and stored elsewhere but only needed to be called. So, the
implementation of functions could just read the three function calls at the end of the file and
get a good idea of what our program does. That simplification and abstraction is one of the
benefits of procedural programming. But within the functions, we still got same old imperative
code.

• Functional Programming

Functional programming takes the concept of functions a little bit further. In functional
programming, functions are treated as first-class citizens, meaning that they can be assigned
to variables, passed as arguments, and returned from other functions. Another key concept is
the idea of pure functions. A pure function is one that relies only on its inputs to generate its
result. And given the same input, it will always produce the same result. Besides, it produces
no side effects (any change outside the function's environment). With these concepts in mind,
functional programming encourages programs written mostly with functions.

• Declarative Programming

Declarative programming is all about hiding away complexity and bringing programming
languages closer to human language and thinking. Programming by specifying the result you
want, not how to get it. It is the direct opposite of imperative programming in the sense that the
programmer does not give instructions about how the computer should execute the task, but
rather on what result is needed.

Control flow in declarative programming is implicit: the programmer states only what the
result should look like, not how to obtain it. Example.

select upper(name)
from people
where length(name) > 5
order by name

No loops, no assignments, etc. Whatever engine that interprets this code is just supposed go
get the desired information and can use whatever approach it wants. (The logic and constraint

51 | P a g e
paradigms are generally declarative as well.). What is nice about this is that it's easier to read
and comprehend, and often shorter to write.

• Object-Oriented Programming

One of the most popular programming paradigms is object-oriented programming (OOP). The
core concept of OOP is to separate concerns into entities which are coded as objects. Each
entity will group a given set of information (properties) and actions (methods) that can be
performed by the entity. So, OOP is based on the sending of messages to objects. Objects
respond to messages by performing operations, generally called methods. Messages can have
arguments. OOP makes heavy usage of classes (which are a way of creating new objects
starting out from a blueprint or boilerplate that the programmer sets). Objects that are created
from a class are called instances.

Because objects operate independently, they are encapsulated into modules which contain both
local environments and methods. Communication with an object is done by message passing.
Objects are organized into classes, from which they inherit methods and equivalent variables.
The object-oriented paradigm provides key benefits of reusable code and code extensibility.
The ability to use inheritance is the single most distinguishing feature of the OOP paradigm.
Inheritance gives OOP its chief benefit over other programming paradigms - relatively
easy code reuse and extension without the need to change existing source code. The
mechanism of modelling a program as a collection of objects of various classes, and
furthermore describing many classes as extensions or modifications of other classes, provides
a high degree of modularity.

Programming paradigms as discussed are different ways in which one can face programming
problems and organize code. Imperative, procedural, functional, declarative, and object-
oriented paradigms are some of the most popular and widely used paradigms today. And
knowing the basics about them is good for general knowledge and also for better understanding
of other topics with regards to coding.

52 | P a g e
5.2.2 Types of Programming Language
There are three types of programming language:

I. Machine language (Low-level language): This is a binary code of 0’s and 1’s which the
language understands by the computer. It is a collection of binary digits or bits that the
computer reads and interprets. Machine languages are the only languages understood
by computers, while easily understood by computers, machine languages are almost
impossible for humans to use because they consist entirely of numbers.
II. Assembly language (Low-level language): This work between the machine language
and high-level language. It uses mnemonics codes, Low-level languages are closer to
the language used by a computer. A program written in assembly language consists of
a series of instructions mnemonics that correspond to a stream of executable
instructions, when translated by an assembler that can be loaded into memory and
executed. Assembly languages use keywords and symbols, much like English, to form
a programming language but at the same time introduce a new problem. The problem
is that the computer doesn't understand the assembly code, so we need a way to convert
it to machine code, which the computer does understand. Assembly language programs
are translated into machine language by a program called an assembler.
III. High-level language: High-level languages are closer to human languages. High-level
languages allow us to write computer code using instructions resembling everyday
spoken language (for example: print, if, while) which are then translated into machine
language to be executed. The term programming language usually refers to high-level
languages, which is the language man mostly understand.

Examples of High-level Language:


• FORTRAN
• ADA
• C
• C++
• JAVA
• BASIC
• COBOL
• PASCAL
53 | P a g e
• PHYTON

A program written in any programming language need to be converted into machine language
so that the computer can understand it. There are two ways to do this:

I. Compile the program: Compile transform a program written in a high-level


programming language from source code into object code. This is done by using a tool
called compiler. A compiler reads the whole source code and translates it into a
complete machine code program to perform the required tasks which is output.
II. Interpret the program: Interpreter is a program (software) that executes instructions
written in a high-level language. An interpreter reads the source code one instruction
or line at a time, converts this line into machine code and executes it.

5.2.3 Comparing Programming Language Types

Machine Language Assembly High-level


Language Languages
Time to execute Since it is the basic A program called A program called a
language of the an ‘assembler’ is compiler or
computer, it does required to convert interpreter is
not require any the program into required to convert
translation, and machine language. the program into
hence ensures Thus, it takes machine language.
better machine longer to execute Thus, it takes more
efficiency. This than a machine time for a computer
means the language program. to execute.
programs run
faster.
Time to develop Needs a lot of skill, Simpler to use than Easiest to use.
as instructions are machine language, Takes less time to
very lengthy and though instruction develop programs
complex. Thus, it codes must be and, hence, ensures
takes more time to memorized. It takes better program
program. less time to develop efficiency

54 | P a g e
programs as
compared to
machine language.

5.3 Computer Programming


Computer programming is the process of writing, testing, debugging/troubleshooting, and
maintaining the source code of computer programs. This source code is written in a
programming language like C++, JAVA, Python etc.

Choosing a Programming Language


Before you decide on what language to use, you should consider the following:

• Your server platform


• The server software you run
• Your budget
• Previous experience in programming
• The database you have chosen for your backend

55 | P a g e
References
An Introduction to Computer Science and Problem Solving. COMP1405/1005.

Bodner, G. M. (1987). The role of algorithms in problem solving. Journal of Chemical Education .

INSTITUTE OF DISTANCE & OPEN LEARNING. Algorithms, Flowcharts & Program.

Quain, M. C. (2012). Intro Problem Solving in Computer Science.

Swarnika, P. Problem Solving Method. Department of Education, Patna University.

https://www.owlnet.rice.edu/~ceng303/manuals/fortran/FOR3_3.html
https://www.freecodecamp.org/news/what-is-pseudocode-in-programming/

https://www.brainkart.com/article/Algorithm,-Pseudocode-and-Flowchart_6945/

56 | P a g e

You might also like