COS102 Problem Solving
COS102 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.
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.
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).
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
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.
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
Applications of Heuristics
Heuristics are attractive because they find solutions in a timely manner. Some examples
include:
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.
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
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
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.
13 | P a g e
of algorithms for artificial intelligence are supervised, unsupervised, reinforcement
learning algorithms.
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.
14 | P a g e
Approaches to Solvable Problems
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.
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.
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
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
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.
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.
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
Objectives
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:
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
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.
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.
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.
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.
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).
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.
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
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.
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
2
https://www.tutorialspoint.com/Binary-Search
29 | P a g e
Applications:
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:
The discussion will be centred on two quadratic sorting methods: selection and insertion 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:
Applications:
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:
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.
Objectives
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.
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.
35 | P a g e
Figure 4.1: Flowchart symbols
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.
Read X, Y, Z
37 | P a g e
Flowchart for the above problem
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.
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
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.
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.
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
• What is pseudo code and why is it useful for writing computer programs?
• Solving programming problems with pseudocodes
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
Characteristics of 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.
There are no set of rules for pseudocodes exact execution though it is frequently used.
However, below are steps to solving programming with pseudocode.
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.
Worked examples.
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:
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
• 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
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.
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:
54 | P a g e
programs as
compared to
machine language.
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 .
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