CSC 102 Lecture Note-1
CSC 102 Lecture Note-1
Introduction
Problem solving is an integral part of Computer Science. Computer is used as a problem
solving tool in both public and private organizations. This statement is a testimony to the rate
at which computer and ICT solutions are used for driving services and operations of
government and private establishments in Nigeria and beyond. From different sectors of the
economy, computers are being put to use. They are used for manufacturing, educating,
researching, distributing, and increasing productivity. While we want to develop a software
application, set up a network, troubleshoot a computer problem (hardware or software) we
engage n problem solving. The problem solver is expected to make use of some skills and
techniques while solving problems. More importantly, a good understand of problem solving
enables us to use computers in addressing several problems that you will be encountering in
the day-to-day discharge of your duties. This is because problems are at the center of what
many people do at work every day. Whether you're solving a problem for a client (internal or
external), supporting those who are solving problems, or discovering new problems to solve,
the problems you face can be large or small, simple or complex, and easy or difficult.
Problem solving is a crucial part of Computer Science and/or computing. It is required that
when you intend using any Computer and Information (CIT) tools or techniques, the
problems you intend using them for are important. 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. Problem-solving is necessary on every job. It is
important that students develop the skills to resolve problems and have the personal resilience
to meet the challenges and pressure that may be the result of a problem. Problem-solving
requires a variety of both analytical and creative thinking skills. Which are used depends on
the role in the organization and the problem.
We usually use the term computerisation to indicate the use of computer to develop software
in order to automate any routine human task efficiently. Computers, are used for solving
various day-to-day problems and thus problem solving is an essential skill that a, computer
science student should know. It is pertinent to mention that computers themselves cannot
solve a, problem. Precise step-by-step instructions should be given by us to solve the
problem. Thus, the success of a, computer in solving a problem depends on how correctly and
1
precisely we define the problem, design a solution, (algorithm) and implement the solution
(program) using, a programming language.
Some good examples of real life problems that can be attended to using problem solving
skills are: Diagnosing Illnesses, Identifying the Causes for Social Problems, Interpreting
Data to Determine the Scope of Problems, Pinpointing Behaviors Contributing to Marital
Distress, Recognizing Invalid Research Models .
2
PROBLEM SOLVING AND DECISION MAKING IN WORK PLACE
Problem-solving and decision-making do go pari-pasu. This is because, a problem solver is
providing a solution to a particular problem in his domain with a view to making useful
decision. Ask anyone in the workplace if these activities are part of their day and they answer
'Yes!' But how many of us have had training in problem-solving? We know it's a critical
element of our work, but do we know how to do it effectively?
People tend to do three things when faced with a problem: they get afraid or uncomfortable
and wish it would go away; they feel that they have to come up with an answer and it has to
be the right answer; and they look for someone to blame. Being faced with a problem
becomes a problem. And that's a problem because, in fact, there are always going to be
problems!
There are two important things to remember about problems and conflicts: they happen all
the time and they are opportunities to improve the system and the relationships. They are
actually providing us with information that we can use to fix what needs fixing and do a
better job. Looked at in this way, we can almost begin to welcome problems!
Developing an Algorithm
It is essential to device a solution before writing a program code for a given problem.
The solution is represented in natural language and is called an algorithm. We can
imagine an algorithm like a very well-written recipe for a dish with clearly defined
steps that if followed one will end up preparing the dish. We start with a tentative
solution plan and keep on refining the algorithm until the algorithm is able to capture
all the aspects of the desired solution. For a given problem more than one algorithm is
possible and we have to select the most suitable solution.
Coding
After finalizing the algorithm, we need to convert the algorithm into the format which
can be understood by the computer to generate the desired solution. Different high
level programming languages can be used for writing a program.
It is equally important to record the details of the coding procedures followed and
document the solution. This is helpful when revisiting the programs at a later stage.
Coding is explained in detail in section 1.8.
4
ALGORITHM
Algorithm is the 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 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 good algorithm is similar to 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 of Algorithm
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
The Algorithm:
• Step1: Start
• Step2: input the first num1.
• Step3: input the second num2.
• Step4: Sum = num1+num2
• Step5: Print Sum
• Step6: End
PROPERTIES OF AN ALGORITHM
An Algorithm must possess the following properties:
TYPES OF ALGORITHMS
Algorithms are classified according to what they are being used to achieve. The basic types of
computer science algorithms include:
• 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.
• Brute Force Algorithms – try all possible solutions until a satisfactory solution is
found.
• Randomized Algorithms – use a random number at least once during the
computation to find a solution to the problem.
• Greedy Algorithms – find an optimal solution at the local level with the intent of
finding an optimal solution for the whole problem.
• 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.
• 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.
• 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.
6
IMPORTANCE OF ALGORITHM
The algorithmic thinking is vital in many different fields. Some of the benefits are:
• Allows students to breakdown problems and conceptualize solution in distinct steps
• Being able to understand and implement an algorithm necessitates students to exercise
planned assessment and reasoning abilities.
• By using an algorithm, decision making becomes a more rational process.
• Makes Problem Solving Process more efficient and consistent
7
FLOWCHART
WHAT IS A FLOWCHART?
A flowchart is a visual representation of an algorithm. A flowchart is a diagram made up of
boxes diamonds and other shapes, connected by arrows. Each shape represents a step of the
solution process and the arrow represents the order or link among the steps.
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 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 program.
Example, 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 * Y * Z
Display the Sum, Average and Product.
8
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
General Rules for flowcharting The basic guidelines for drawing a flowchart with the
above symbols include:
• In drawing a proper flowchart, all necessary requirements should be listed out in
logical order.
• The flowchart should be neat, clear and easy to follow. There should not be any room
for ambiguity in understanding the flowchart.
• All boxes of the flowchart are connected with Arrows. (Not lines)
• 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.
• 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.
• 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.
• Connectors are used to connect breaks in the flowchart. Examples are:
9
From one page to another page.
From the bottom of the page to the top of the same page.
An upward flow of more than 3 symbols
• Subroutines and Interrupt programs have their own and independent flowcharts.
• All flow charts start with a Terminal or Predefined Process (for interrupt programs or
subroutines) symbol.
• 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.
10
Example1: 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
TYPES 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.
11
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 substeps involved in the
process and also reveals the delays that occur when the materials required are not available in
the inventory.
12
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:
• 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.
• Effective Analysis: A flowchart of a problem can be used for effective analysis of the
problem.
• 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.
• Efficient Program Maintenance: A developed and operational program is easier to
maintain with the help of a flowchart.
• 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.
• Proper Debugging: The flowchart helps in debugging process.
PSEUDOCODE
A Pseudocode (pronounced Soo-doh-kohd) is another, way of representing an algorithm. It is
considered as a, non-formal language that helps programmers to write, algorithm. It is a
detailed description of instructions, that a computer must follow in a particular order. It is,
intended for human reading and cannot be executed, directly by the computer. No specific
standard for writing, a pseudocode exists. The word “pseudo” means “not, real,” so
“pseudocode” means “not real code”. Following, are some of the frequently used keywords
while writing, pseudocode:
• INPUT
• COMPUTE
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE,
Example: Write an Algorithm to display the, sum of two numbers entered by user, using both
Pseudocode and Flowchart.
Pseudocode for the sum of two numbers will be:
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
The flowchart for this algorithms is given
13
Example… Write an algorithm to calculate area and perimeter of a rectangle
using both pseudocode and flowchart.
Figur
e 1.5
BENEFITS OF PSEUDOCODE
Before writing codes in a high level language a pseudocode of a program helps in
representing the basic functionality of the intended program. By writing the code first in a
human readable language the programmer safeguards against leaving out any important step.
Besides for non-programmers actual programs are difficult to read and understand but
pseudocode helps them to review the steps to confirm that the proposed implementation is
going to achieve the desire output.
14
DECOMPOSITION
Breaking a complex problem into smaller, more manageable sub-problems. Example:
Dividing a software project into modules like user interface, database, and logic. Part of
being a computer scientist is breaking down a big problem into the smaller problems that
make it up. If you can break down a big problem into smaller problems, then you can give
them to a computer to solve. For example, if I gave you a cake and asked you to back me
another one, you might struggle. But if you watched me making the cake and worked out the
ingredients, then you’d stand a much better chance of replicating it. If you can look at a
problem and work out the main steps of that problem, then you’ll stand a much better chance
of solving it.
Look at an example, the equation to work out the roots of a quadratic equation:
−b ± √ b2−4 ac
x=
2a
On first look, it might appear a little scary, but if we decompose it, we should stand a better
chance of solving it:
1. b2
2. 4ac
3. b - 4ac
4. √ b2−4 ac
5. -b+¿ √ b2−4 ac
6. 2a
−b ± √ b2−4 ac
7. x=
2a
8. Repeat for -b−¿ √ b2−4 ac
By noting the steps down to solve a problem, we can often recognize patterns, and by giving
a list of steps, we are one step closer to creating an algorithm.
PATTERN RECOGNITION
Identifying recurring patterns or similarities within a problem to leverage existing solutions
or approaches. Often breaking down a problem into its components is a little harder than
taking apart an algorithm, we are often given a set of raw data and then are asked to find the
pattern behind it:
This is pretty easy with number sets, the above pattern An=n+3. But pattern recognition
might also involve recognizing shapes, sounds or images. If your camera highlights faces
when you point it at some friends, then it is recognizing the pattern of a face in a picture.
15
If your phone tells you the weather when you ask it “What is the weather like in Wukari?”,
then it has recognized the word “weather” and that “Wukari” is a city in Taraba State.
Linking them together, pattern recognition is the computing behind why you are given
tailored adverts when you log into your mail account or social network, they have recognized
the pattern of what someone like you wants to buy. Pattern recognition might predict the
weather, but the prediction may not always be perfect.
ABSTRACTION
Abstraction involves focusing on the essential details of a problem while ignoring irrelevant
or unnecessary information. It simplifies complexity by creating a general representation of
the problem, making it easier to understand and solve.
Steps in Abstraction
1. Understand the Problem:
o Analyze the overall system to determine what’s relevant.
o Example: Creating a navigation app focuses on routes and locations, ignoring
road textures.
2. Identify Essential Details:
o Select key features that are critical to solving the problem.
o Example: For a banking app, account balance and transaction history are
relevant, while user background isn't.
3. Remove Unnecessary Information:
o Exclude non-essential details to reduce complexity.
o Example: When programming a chess game, the aesthetic of pieces is
irrelevant compared to their moves.
4. Create a General Representation:
o Develop a simplified model or algorithm to work with.
o Example: Using variables to represent inputs and outputs in a program.
ALGORITHM DESIGN
Designing a step-by-step procedure (algorithm) that clearly outlines the steps required to
solve a problem. Once we have our patterns and abstractions, we can start to write the steps
that a computer can use to solve the problem. We do this by creating Algorithms. Algorithms
are not computer code, but are independent instructions that could be turned into compute
code. We often write these independent instructions as pseudo code. Examples of algorithms
could be to describe orbit of the moon, the steps involved in setting up a new online shopping
account or the sequences of tasks involved for a robot to build a new car.
16