Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
117 views16 pages

CSC 102 Lecture Note-1

Uploaded by

Collins Ifeanyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views16 pages

CSC 102 Lecture Note-1

Uploaded by

Collins Ifeanyi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INTRODUCTION TO PROBLEM SOLVING

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.

What are Problem-Solving Skills?


Problem solving skills may be grouped into soft skill and hard skill. Considered a soft skill (a
personal strength, as opposed to a hard skill that is learned through education or training), an
aptitude for creative and effective problem-solving is nonetheless one of the most valued
attributes employers seek in their job candidates. For example, a cable television technician
might be trying to resolve a customer problem with a weak signal. A teacher might need to
figure out how to improve the performance of her students on a writing proficiency test. A
store manager might be trying to reduce theft of merchandise. A computer specialist might be
looking for a way to speed up a slow program
Thus, problem solving is the, process of identifying a problem, developing an algorithm, for
the identified problem and finally implementing the, algorithm to develop a computer
program.
Problem Solving is the sequential process of analyzing information related to a given
situation and generating appropriate response options.
“Computer Science is a science of abstraction -creating the right model for a problem and
devising the appropriate merchandisable techniques to solve it.”
–A. Aho and J. Ullman, ,

Required Skills for a problem solver


Some of the required skills that a problem solver is expected to have include:
 Active Listening
 Data Gathering
 Data Analysis
 Fact Finding
 Historical Analysis
 Causal Analysis
 Process Analysis
 Needs Identification

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!

STEPS FOR PROBLEM SOLVING


Suppose while driving a vehicle starts making a strange noise. We might not know how to
solve the problem, right away. First we need to identify from where the The correctness of
the, output that a computer noise is coming? In case the problem cannot be solved gives
depends upon the by us then we need to take the vehicle to a mechanic. correctness of input.
The mechanic will analyse the problem to identify the provided. Source of the noise make a
plan about the work to be, done and finally repair the vehicle in order to remove the noise.
From the above example it is explicit that finding the solution to a problem might consist of
multiple steps.
When problems are straightforward and easy we can easily find the solution. But a complex
problem requires a methodical approach to find the right solution. In other words we have to
apply problem solving techniques. Problem solving begins with the precise identification of
the problem and ends with a complete working solution in terms of a program or software.
Key steps required for solving a problem using a computer are shown in Figure 1.1 and are
discussed in following subsections.

Figure 1.1 Steps for problem solving

Figure 1.1 Steps for problem solving

Analyzing the problem,


3
Algorithm A set of exact steps, which when followed solve the problem or accomplish
the required task.
It is important to clearly understand a problem before we begin to find the solution for
it. If we are not clear as to what is to be solved we may end up developing a program
which may not solve our purpose. Thus we need to read and analyse the problem
statement carefully in order to list the principal components of the problem and decide
the core functionalities that our solution should have. By analysing a problem we
would be able to figure out what are the inputs that our program should accept and the
outputs that it should produce.

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.

Testing and Debugging


The program created should be tested on various parameters. The program should
meet the requirements of the user. It must respond within the expected time. It should
generate correct output for all possible inputs. In the presence of syntactical errors no
output will be obtained. In case the output generated is incorrect then the program
should be checked for logical errors if any.
Software industry follows standardised testing methods like unit or component
testing integration testing system testing and acceptance testing while developing
complex applications. This is to ensure that the software meets all the business and
technical requirements and works as expected. The errors or defects found in the
testing phases are debugged or rectified and the program is again tested. This
continues till all the errors are removed from the program.
Once the software application has been developed tested and delivered to the user still
problems in terms of functioning can come up and need to be resolved from time to
time. The maintenance of the solution thus involves fixing the problems faced by the
user answering the queries of the user and even serving the request for addition or
modification of features.

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:

• 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.
• Definiteness: Each step of must be precisely defined. Actions to be carried out must
to be unambiguously specified for every activity.
• Input: An algorithm should have some inputs. The values are given to the algorithm
before it begins.
5
• Output: An algorithm has one or more outputs after completing a specified task
based on the inputs.
• Effectiveness: Algorithm operations should be basic and done exactly in a finite
amount of time by a person with pen/paper.

CHARACTERISTICS OF A GOOD ALGORITHM

• Activities in an algorithm must be clearly defined in other words for it to be


unambiguous.
• Precision — the steps are precisely stated or defined.
• Uniqueness — results of each step are uniquely defined and only depend on the input
and the result, of the preceding steps.
• Finiteness — the algorithm always stops after a finite number of steps
• Input — the algorithm receives some input
• Output — the algorithm produces some output.

COMMON ELEMENTS OF ALGORITHMS


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

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

WHY DO WE NEED AN ALGORITHM?


A programmer writes a program to instruct the computer, to do certain tasks as desired. The
computer then follows the steps written in the program code. Therefore the programmer first
prepares a roadmap of the program to be written before actually writing the code. Without a
roadmap the programmer may not be able to clearly visualise the instructions to be written
and may end up developing a program which may not work as expected.
Such a roadmap is nothing but the algorithm which is the building block of a computer
program. For example, searching using a search engine, finding a word in a document,
booking a taxi through an app, performing online banking, playing computer games all are
based on algorithms.
Writing an algorithm is mostly considered as a first step to programming. Once we have an
algorithm to solve a problem we can write the computer program for giving instructions to
the computer in high level language. If the algorithm is correct computer will run the program
correctly every time. So the purpose of using an algorithm is to increase the reliability,
accuracy, and efficiency of obtaining solutions.

IMPLEMENTING / EXPRESSING ALGORITHMS


Algorithms can be expressed in many notations including:
• Natural languages: tend to verbose, ambiguous and are rarely used for complex or
technical algorithms.
• Programming language: programming language are specifically designed to express
algorithms in a way that computers can understand and execute.
• 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.

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.

Flowchart for the above problem

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.

The figure shows basic flowchart process.


Recall what circle shape stands for “connector”. Here, it connects and extends
processes from start to end.

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

Flowchart for the above Algorithm will look like:

Example 2: Using a Flowchart find the Area of a circle of radius r

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.

Pseudocode for calculating area and perimeter of, a rectangle.


INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
PRINT Perim

The flowchart for this algorithm is given

Figure 1.5 Flowchart to


calculate area and
perimeter of a rectangle

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.

COMPUTATIONAL APPROACHES TO PROBLEM SOLVING


Computational approaches to problem solving in computer science refers to the method of
tackling problems by breaking them down into smaller, manageable parts, identifying
patterns, and designing step-by-step solutions that can be executed by a computer, essentially
using the principles of "computational thinking" to find solutions through systematic analysis
and abstraction.

Computational Thinking (CT) is a problem-solving process that involves a set of techniques


and approaches to break down complex problems into manageable parts, allowing for
systematic and logical solutions. It teaches you the things you need to think about before you
start writing a program. You need to know how to approach the problem, how to develop a
solution and what the computer is capable of. It forms the foundation of computer science.

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:

1, 4, 7, 10, 13, 16, 19, 22, 25, …

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

You might also like