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

Table of Contents

1. Introduction to problem-solving in programming.................................................................. 2


2. Concepts and Properties of Algorithm ................................................................................... 3
3. Problem Solving Process ....................................................................................................... 4
4. Roles of Algorithm in Problem Solving Process ..................................................................... 4
5. Ways of Expressing Algorithm ............................................................................................... 5
5.1 Flow chart .............................................................................................................................. 5
5.2 Pseudocode ........................................................................................................................... 8
6. Implementation Strategies in Problem Solving ................................................................... 12
7. Program Objects ................................................................................................................. 12

Problem Solving with Basic Programming Language NOT FOR SALE Page | 1
1. Introduction to problem-solving in programming
Problem-solving is a critical skill in programming. It involves the ability to analyze a problem,
break it down into smaller components, and create a solution using programming concepts and
techniques. Problem-solving in programming requires a combination of technical knowledge,
creativity, and critical thinking.

The first step in problem-solving is to understand the problem. This may involve reading a
problem statement, analyzing data, or identifying requirements. Once the problem is
understood, the next step is to break it down into smaller components or sub-problems. This
helps to simplify the problem and makes it easier to solve. The next step is to create a solution
using programming concepts and techniques. This may involve writing code, creating
algorithms, or designing data structures. The solution should be tested and refined until it
meets the requirements of the problem. Hence, effective problem-solving in programming
requires a deep understanding of programming languages, data structures, algorithms, and
software design principles. It also requires strong analytical and critical thinking skills, as well
as the ability to work collaboratively and communicate effectively.

In computer science, problem solving refers to the process of finding solutions to problems
using algorithms and programming (i.e. codes). It involves analyzing a problem, identifying
the requirements and constraints, and developing a logical plan of action to solve it.

Generally, the following are the basic steps of problem solving.

Problem solving can be broken down into several steps, including:


1. Understanding the problem: This involves defining the problem, identifying the
inputs and outputs, and understanding the constraints and requirements.
2. Breaking down the problem: Once the problem is understood, it can be broken down
into smaller sub-problems or tasks. This makes it easier to solve and manage.
3. Developing a plan: A plan is developed based on the sub-problems identified, and the
available resources and constraints. The plan should be logical, clear and concise.
4. Implementing the plan: This is where the actual programming is done to solve the
problem. The code should be efficient, maintainable and well-structured.
5. Testing and refining: Once the code is written, it is tested to ensure that it produces
the expected output. If errors are found, they are fixed and the code is refined until it
meets the requirements.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 2
By following a structured approach to problem-solving, programmers can create effective
solutions that meet the requirements of the problem. Overall, problem solving in computer
science involves using logical thinking and algorithmic approaches to identify, analyze and
solve complex problems efficiently and effectively.

2. Concepts and Properties of Algorithm


An algorithm is a precise sequence of instructions or steps that defines a set of operations that
must be performed in a specific order to solve a problem or accomplish a task. It is a step-by-
step procedure that outlines how to accomplish a particular goal. Simply put, an algorithm is a
recipe for solving a problem. In programming, algorithms are essential as they provide a
systematic approach to problem-solving, which is necessary to develop efficient and effective
software.

Some important concepts related to algorithms in programming include:


1. Input: An algorithm takes some input data or information as an input to solve the
problem. The input can be anything from user input (such as text, numbers, images, or
sound) and data stored in a database.
2. Output: An algorithm produces result of processing as an output based on the input
and the instructions given. The output can also be anything from simple text, numbers,
images, or sound and complex data structure.
3. Variables: In programming, variables are used to store data that can be modified or
used in calculations. They can be used to store input, intermediate results, or output.
4. Control structures: Control structures such as loops, conditionals and functions are
used to determine the flow of an algorithm. They can be used to make decisions based
on certain conditions or to repeat certain steps until a condition is met.
5. Efficiency: The efficiency of an algorithm is a measure of how well it performs in terms
of time and space. An algorithm that uses less time and space is considered more
efficient than an algorithm that uses more.
6. Pseudo-code: Pseudo-code is a way of expressing an algorithm in plain English that is
easy to understand. It is used to outline the basic steps of an algorithm before writing
the actual code.
7. Debugging: Debugging is the process of finding and fixing errors in an algorithm.
Debugging is an important skill for programmers, as errors in algorithms can cause
unexpected behavior or crashes.
8. Recursion: Recursion is a programming technique where a function calls itself to
accomplish a task. It is often used in algorithms that involve repetitive tasks or complex
data structures.
9. Complexity: The complexity of an algorithm refers to how much time and space it
takes to solve a problem. Algorithms can have different levels of complexity, ranging
from simple to complex.
10. Optimization: Optimization involves making an algorithm more efficient by reducing
its complexity or improving its performance. This is an important aspect of
programming as it can help to improve the overall quality of the software.

Properties of Algorithm
An algorithm is a set of steps or instructions that are used to solve a problem or perform a
task. Here are some of the properties of algorithms:
1. Clear and Unambiguous: An algorithm must be clear and unambiguous, which means
that it should be easy to understand and there should be no room for confusion or
misinterpretation.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 3
2.Finiteness: An algorithm must terminate after a finite number of steps. It should not
go on indefinitely without producing a result or run indefinitely, which can lead to
resource depletion and system crashes.
3. Input: An algorithm must have zero or more inputs that are given to it before it
begins. The inputs are used by the algorithm to produce the desired output.
4. Output: An algorithm must produce one or more outputs that are the result of the
inputs and the steps performed by the algorithm.
5. Effectiveness: An algorithm must be effective, which means that it must solve the
problem or perform the task it was designed for. It should not produce incorrect or
incomplete results.
6. Generality: An algorithm must be general, which means that it should be applicable to
a wide range of inputs, not just specific cases.
7. Feasibility: An algorithm must be feasible, which means that it should be possible to
implement the algorithm using the available resources, such as time and memory.
8. Optimality: An algorithm should be optimal, which means that it should be designed to
produce the best result with the available resources. An optimal algorithm should have
the least possible time complexity and space complexity.
9. Methodical: An algorithm should be well-organized, with clear logical steps that enable
easy debugging and maintenance.
10. Modularity: An algorithm can be split into smaller, reusable modules, which can be
used in different contexts or applications.
These properties are essential for designing efficient algorithms that can solve problems in
various domains, including computer science, mathematics, engineering, and more.

3. Problem Solving Process


The problem-solving process is the sequence of steps that a programmer takes to solve a
problem with computer programming. It typically involves the following stages:
a. Understanding the problem: The first step is to carefully read and understand the
problem statement, including any constraints or requirements.
b. Breaking down the problem: Next, the problem is broken down into smaller sub-
problems that can be more easily solved. This may involve identifying patterns,
algorithms, and data structures.
c. Developing a plan: Based on the sub-problems identified, the programmer develops a
plan for solving the problem. This may involve pseudocode or a high-level description of
the steps that will be taken.
d. Implementing the plan: The next step is to translate the plan into actual code using
a programming language. This may involve testing and debugging as the code is
written.
e. Testing and refining: Once the code is complete, it is tested to ensure that it
functions as intended. Any issues that arise during testing are addressed, and the code
may be refined further to improve performance or usability.
f. Documenting: Finally, the programmer documents the code so that others can
understand how it works and make modifications if necessary.
Overall, the problem-solving process requires creativity, critical thinking, and attention to
detail, as well as a strong understanding of programming concepts and techniques.

4. Roles of Algorithm in Problem Solving Process


Algorithms play a crucial role in the problem-solving process. Algorithms are critical tools in
the problem-solving process. They can help define the problem, generate and optimize
solutions, implement the solution, automate tasks, and test and validate solutions. Here are
some ways algorithms can help in problem-solving:

Problem Solving with Basic Programming Language NOT FOR SALE Page | 4
1. Defining the problem: Before solving a problem, it's essential to define the problem
precisely. Algorithms can help define the problem by breaking it down into smaller,
more manageable parts, allowing for a clearer understanding of what needs to be
solved.
2. Generating solutions: Algorithms can be used to generate solutions to a problem.
They can take input data, apply a series of rules and calculations, and generate an
output that provides a solution.
3. Optimizing solutions: Once a solution has been generated, algorithms can be used to
optimize the solution by tweaking the inputs or adjusting the rules and calculations.
This can lead to a better outcome than the initial solution.
4. Implementation: Algorithms can be used to implement a solution to a problem. They
can be used to write code, build models, or create workflows that solve the problem in
a systematic and efficient way.
5. Automation: Algorithms can automate repetitive tasks, freeing up time for humans to
focus on more complex problem-solving activities.
6. Testing and validation: Algorithms can be used to test and validate solutions to a
problem. By running simulations or applying a set of inputs to the solution, algorithms
can identify potential problems and help refine the solution.

5. Ways of Expressing Algorithm


In programming, algorithms are used to create software, design computer systems, and
analyze data. They could be expressed in different ways including flowcharts and pseudocode
before actual coding in a particular programming language.

5.1 Flow chart


a. Definition
A flowchart is a graphical representation of an algorithm, process, or workflow. It uses symbols
and arrows to illustrate the flow of a program or system. Flowcharts help to visualize the flow
of a program or process, making it easier to understand and identify potential issues. By using
these concepts, you can create a clear and easy-to-understand flowchart that will help you to
develop and execute your program or process more effectively.

b. Properties of flow chart


A flowchart is a visual representation of a process or algorithm using symbols and arrows.
Here are some properties of a flowchart:
1. Clarity: A flowchart must be clear and easy to understand. The symbols and arrows used
should be easily recognizable, and the flowchart should be organized in a logical manner.
2. Consistency: A flowchart must be consistent. The symbols and arrows used should be the
same throughout the flowchart, and the flow lines should follow a consistent direction.
3. Accuracy: A flowchart must accurately represent the process or algorithm being
described. It should not contain any errors or omissions.
4. Simplicity: A flowchart should be simple and concise. It should not contain unnecessary
details or symbols that may confuse the reader.
5. Completeness: A flowchart must be complete. It should include all the necessary steps
and decision points required to complete the process or algorithm.
6. Ease of modification: A flowchart should be easy to modify if changes are required to the
process or algorithm. It should be easy to add, remove, or modify steps without affecting
the overall structure of the flowchart.
7. Ease of use:** A flowchart should be easy to use. It should be accessible to a wide range
of users, regardless of their technical expertise.
By keeping these properties in mind, you can create an effective and efficient flowchart that
accurately represents the process or algorithm being described.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 5
c. Advantages of Flowcharts
1. The flowchart shows the logic of a problem displayed in pictorial fashion which
felicitates easier checking of an algorithm.
2. The Flowchart is good means of communication to other users. It is also a compact
means of recording an algorithm solution to a problem.
3. The flowchart allows the problem solver to break the problem into parts. These parts
can be connected to make master chart.
4. The flowchart is a permanent record of the solution which can be consulted at a later
time.

d. Symbols used in Flow-Charts


The symbols that we make use while drawing flowcharts as given below are as per conventions
followed by International Standard Organization (ISO).

a. Oval: Rectangle with rounded sides is used to indicate


either START/ STOP of the program. ..

b. Input and output indicators: Parallelograms are used


to represent input and output operations. Statements like
INPUT, READ and PRINT are represented in these
Parallelograms.

c. Process Indicators: Rectangle is used to indicate any set


of processing operation such as for storing arithmetic
operations.

d. Decision Makers: The diamond is used for indicating the step


of decision making and therefore known as decision box. Decision
boxes are used to test the conditions or ask questions and
depending upon the answers, the appropriate actions are taken
by the computer. The decision box symbol is shown below.

e. Flow Lines: Flow lines indicate the direction being followed in the
flowchart. In a Flowchart, every line must have an arrow on it to
indicate the direction. The arrows may be in any direction

f. On-Page connectors: Circles are used to


join the different parts of a flowchart and these
circles are called on-page connectors. The uses
of these connectors give a neat shape to the
flowcharts. In a complicated problem, a flowchart may run in to several pages. The parts
of the flowchart on different pages are to be joined with each other. The parts to be joined
are indicated by the circle.

g. Off-page connectors: This connector represents a break in the path


of flowchart which is too large to fit on a single page. It is similar to on-
page connector. The connector symbol marks where the algorithm ends
on the first page and where it continues on the second.

e. Simple Problems using Flow Chart


Example 1: Draw the Flowchart to find roots of quadratic equation ax 2+ bx + c = 0. The
coefficients a, b, c are the input data.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 6
Example 2: Draw a flowchart to find out the biggest of the three unequal positive numbers.

Example 3: Draw a flowchart for adding the integers from 1 to 100 and to print the sum.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 7
Example 4: Develop a flowchart to illustrate how to make a land phone telephone call

Flowchart for Telephone call

Example 5: ABC company plans to give a 6% year-end bonus to each of its employees
earning N6,000 or more per month , and a fixed N250bonus to the remaining employees. Draw
a flowchart for calculating the bonus for an employee

5.2 Pseudocode
Pseudocode is a high-level, informal language that is used to describe the logic of a computer
program. It is not a specific programming language, but rather a way of writing code that can

Problem Solving with Basic Programming Language NOT FOR SALE Page | 8
be easily understood by both humans and computers. Pseudocode is a valuable tool for
planning and designing computer programs. It allows programmers to focus on the logic and
structure of the program without getting bogged down in the details of a specific programming
language. By using these concepts, you can create effective and efficient pseudocode that
accurately represents the logic of your program.
Here are some key concepts of pseudocode:
1. Statements: Pseudocode is made up of statements that describe the steps of the
program. These statements are similar to those in a programming language, but they
are written in a more natural language.
2. Variables: Pseudocode uses variables to represent data that is used in the program.
Variables can be assigned values and used in calculations and other operations.
3. Expressions: Pseudocode uses expressions to represent calculations and other
operations. These expressions can include variables, constants, and operators.
4. Control Structures: Pseudocode uses control structures such as if-then-else
statements, loops, and functions to control the flow of the program.
5. Comments: Pseudocode includes comments to explain the purpose of certain
statements or to provide additional information about the program.
6. Readable: Pseudocode is designed to be easily readable by humans. It uses natural
language and avoids complex syntax and programming conventions.
7. Translatable: Pseudocode can be translated into actual code in a programming
language. This makes it a useful tool for planning and designing programs before they
are actually coded.

Properties of Pseudocode
Here are some properties of pseudocode:
1. Clarity: Pseudocode must be clear and understandable. It should be written in a way
that can be easily interpreted by anyone who reads it.
2. Simplicity: Pseudocode must be simple and easy to understand. It should not contain
unnecessary details or complex syntax.
3. Flexibility: Pseudocode must be flexible. It should allow for changes and modifications
to be made easily without affecting the overall structure of the program.
4. Readability: Pseudocode must be readable. It should be well-formatted and structured
in a way that is easy to follow.
5. Modularity: Pseudocode must be modular. It should be broken down into smaller,
more manageable parts that can be easily combined to create the final program.
6. Accuracy: Pseudocode must be accurate. It should accurately represent the logic of
the program and the steps that need to be taken to achieve the desired outcome.
By keeping these properties in mind, you can create effective and efficient pseudocode that
accurately represents the logic of your program and is easy to understand and modify.

Comparison between flow chart, pseudocode and algorithm


Flowcharts, pseudocode, and algorithms are all tools used in programming to plan and design
computer programs. Here are the main differences between the three:
1. Flowcharts: Flowcharts use symbols and diagrams to represent the flow of a program or
process. They are visual representations of the steps involved in a program and are
useful for identifying potential issues and improving the overall structure of the
program.
2. Pseudocode: Pseudocode is a high-level description of a program written in natural
language. It is used to plan and design a program and is often used as a first step
before writing actual code. Pseudocode is easy to read and allows programmers to
focus on the logic and structure of the program without getting bogged down in the
details of a specific programming language.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 9
3. Algorithm: An algorithm is a step-by-step description of a process. It is a set of
instructions that can be followed to solve a particular problem or perform a specific
task. Algorithms are often used in programming to solve complex problems and are the
foundation of computer programming.

In summary, flowcharts are visual representations of a program, pseudocode is a high-level


description of a program written in natural language, and algorithms are a set of instructions
that can be followed to solve a particular problem or perform a specific task. All three are
important tools in programming and are used to plan and design computer programs.

Example 6: A simple pseudocode to find the sum of two numbers


1. Get input values for a and b
2. Add a and b together
3. Store the result in a variable called sum
4. Output the value of sum
This pseudocode could be translated into code in a programming language like Python as
follows:

Example 7: A simple algorithm to compute the area of a triangle:


1. Start
2. Input the base of the triangle (b)
3. Input the height of the triangle (h)
4. Calculate the area of the triangle using the formula: area = 0.5 * b * h
5. Output the area of the triangle
6. Stop

This algorithm takes the base and height of the triangle as input and calculates the area using
the formula A = 0.5 * b * h. The result is then outputted to the user. This algorithm assumes
that the input values are valid and that the base and height are both measured in the same
unit (e.g. meters, inches, etc.).

Note that this algorithm can be implemented in any programming language by using the
appropriate syntax for input, output, and arithmetic operations.

Example 8: A simple flowchart to compute the area of a triangle:


Start
├─ Input base of triangle (b)
├─ Input height of triangle (h)
├─ Calculate area using formula: area = 0.5 * b * h
├─ Output area
End

The flowchart starts with the "Start" symbol and then proceeds to the next step, which is to
input the base of the triangle (b). From there, it proceeds to the next step to input the height
of the triangle (h). The flowchart then calculates the area of the triangle using the formula:
area = 0.5 * b * h. The result is outputted to the user and the flowchart ends with the "End"
symbol.

This flowchart is a visual representation of the algorithm described in the previous answer. It
shows the flow of data and the steps involved in computing the area of a triangle. Note that
this flowchart can be implemented in any programming language by using the appropriate
syntax for input, output, and arithmetic operations.

Example 9: A simple pseudocode to compute the area of a triangle:


Begin

Problem Solving with Basic Programming Language NOT FOR SALE Page | 10
Input base of triangle (b)
Input height of triangle (h)
area = 0.5 * b * h
Output area
End

This pseudocode starts with the "Begin" keyword and then proceeds to the next step to input
the base of the triangle (b). From there, it proceeds to the next step to input the height of the
triangle (h). The pseudocode then calculates the area of the triangle using the formula: area =
0.5 * b * h. The result is outputted to the user and the pseudocode ends with the "End"
keyword.

This pseudocode is a high-level representation of the algorithm described in the previous


answers. It is written in natural language and does not contain any specific programming
syntax. Note that this pseudocode can be easily translated into any programming language by
using the appropriate syntax for input, output, and arithmetic operations.

Example 10: Algorithm to find the roots of a quadratic equation:


1. Start
2. Input the values of a, b, and c from the user
3. Calculate the discriminant (d) using the formula: d = b^2 - 4ac
4. If d is negative, display a message saying "Roots are imaginary"
5. If d is zero, calculate the root using the formula: root = -b / 2a
6. If d is positive, calculate the roots using the formula: root1 = (-b + sqrt(d)) / 2a
and root2 = (-b - sqrt(d)) / 2a
7. Display the roots of the quadratic equation
8. Stop

This algorithm takes the values of a, b, and c as input and calculates the roots of the quadratic
equation using the discriminant formula, which is d = b^2 - 4ac. If the discriminant is
negative, the roots are imaginary and a message is displayed to the user. If the discriminant is
zero, there is only one real root and it is calculated using the formula -b/2a. If the discriminant
is positive, there are two real roots and they are calculated using the quadratic formula. The
roots are then displayed to the user.
Note: This algorithm can be implemented in any programming language by using the
appropriate syntax for input, output, and arithmetic operations.

Example 11: Flow chart to find the roots of a quadratic equation


Start
Input coefficients a, b and c
Calculate discriminant, d = b^2 - 4ac
If d < 0 then
Display "Roots are imaginary"
Else if d = 0 then
Calculate root, r = -b/2a
Display "Root is ", r
Else
Calculate roots, r1 = (-b + sqrt(d)) / 2a and r2 = (-b - sqrt(d)) / 2a
Display "Roots are ", r1, " and ", r2
End if
Stop

This flowchart starts with the "Start" symbol and then proceeds to the next step to input the
coefficients a, b, and c of the quadratic equation. From there, it calculates the discriminant

Problem Solving with Basic Programming Language NOT FOR SALE Page | 11
using the formula d = b^2 - 4ac. The flowchart then checks if the discriminant is negative,
zero, or positive using an "If" statement.

If the discriminant is negative, the flowchart displays a message "Roots are imaginary". If the
discriminant is zero, the flowchart calculates the root using the formula r = -b/2a and displays
"Root is" followed by the value of the root. If the discriminant is positive, the flowchart
calculates the roots using the quadratic formula and displays "Roots are" followed by the
values of the roots.

This flowchart is a visual representation of the algorithm described in the previous answer. It
shows the flow of data and the steps involved in finding the roots of a quadratic equation. Note
that this flowchart can be implemented in any programming language by using the appropriate
syntax for input, output, and arithmetic operations.

6. Implementation Strategies in Problem Solving


Programming is all about problem-solving, and there are several strategies that programmers
use to implement solutions to problems. Here are some common implementation strategies:
1. Divide and Conquer: This strategy involves breaking a large problem down into smaller,
more manageable subproblems. Each subproblem is then solved individually, and the
solutions are combined to form a solution to the larger problem.
2. Top-Down Design: This approach involves breaking a problem down into smaller and
smaller parts until each part is simple enough to be solved easily. The process starts
with the overall problem and works its way down to the details of how to solve each
part.
3. Bottom-Up Design: This strategy involves solving individual parts of a problem first and
then combining them to form a solution to the larger problem. It is the opposite of top-
down design.
4. Iterative Development: This approach involves writing code in small chunks, testing
each chunk, and then incorporating it into the larger program. This strategy helps catch
errors early in the development process and allows for easier debugging.
5. Test-Driven Development: This strategy involves writing tests for a piece of code before
writing the code itself. This helps ensure that the code works as expected and reduces
the likelihood of introducing bugs.
6. Object-Oriented Programming: This approach involves designing a program by
identifying the objects that are part of the problem domain and defining how they
interact with each other. This strategy helps create more modular and reusable code.
These implementation strategies can be used individually or in combination depending on the
problem being solved and the programming language being used.

7. Program Objects
In computer science, a program object refers to a self-contained unit of code that encapsulates
data and behavior. It is an instance of a program or module that can be executed within a
larger program or system. Programming objects involves creating software objects that
interact with one another to perform specific tasks or solve problems. These objects are
defined by classes, which are blueprints for creating instances of objects with specific
properties and behaviors. In object-oriented programming, objects can communicate with one
another through methods, which are functions that belong to a particular object. These
methods can access and manipulate the properties of the object, as well as communicate with
other objects.

Program objects can be created using various programming paradigms such as procedural
programming, object-oriented programming, or functional programming. In object-oriented
programming, program objects are typically instances of classes, which define the behavior

Problem Solving with Basic Programming Language NOT FOR SALE Page | 12
and attributes of the object. Program objects can communicate with each other and exchange
data through methods, functions, and interfaces. They can also be used to implement
abstraction and encapsulation, which are important concepts in software engineering that
enable modular design and code reuse.

In object-oriented programming (OOP), a program object refers to an instance of a class. A


class is a blueprint or template that defines the properties and behavior of objects, and an
object is a specific instance of a class that has its own set of values for the properties defined
by the class. For example, if you have a class called "Person" that has properties like "name",
"age", and "gender", you can create program objects (instances) of this class such as "John
Smith", "Jane Doe", etc. Overall, program objects are a fundamental concept in computer
science and are used extensively in software development to create complex systems and
applications.

Problem Solving with Basic Programming Language NOT FOR SALE Page | 13

You might also like