CSC201 Lect1
CSC201 Lect1
INTRODUCTION
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 precisely we
define the problem, design a solution (algorithm) and implement the solution (program) using
a programming language. 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.
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.
1. Understanding the problem: Carefully read the problem to understand what needs
to be accomplished. Assess your background skills in the area under consideration
to know if you have to acquire more skills in other to solve the problem. Visualize
the final-result and use it as a guide in determining the validity of your solution and
the result that will be obtained. Identify the known and the unknown (variables).
Check the data and the conditions that needs to be satisfied. Check to know if the
condition is sufficient to determine the unknown. Split the problem into parts and
write down the parts.
2. Device a plan: Check to see if there is a link between the data and the unknown
(variable), if there is no link then try using auxiliary problem. Check if a similar
problem exists so you can adapt the solution instead of reinventing the wheel. After
all, the outcome of this phase is the plan of the solution.
3. Carrying out the plan: In carrying out the plan, you have to go through the steps
to know if they are correct, ascertain if there is any missing link. Check if the
correctness of the plan can be proved. If the plan does not work correctly, discard
it and try another plan.
4. Looking Back: This is the moment of reflection. This is the phase that you can
crosscheck what you have done to arrive at the present stage. In this phase, you can
ascertain what worked and what did not work. This is the time your argument and
results are verified for correctness and consistency. In this phase, you can check if
the solution can be applied in other problems. At this point, you can also ascertain
if you can still get the same result using different methods.
CONCEPTS OF ALGORITHM
In our day-to-day life we perform activities by following certain sequence of steps. Problem
solving goes through a sequence of steps which involves the abstraction and decomposition of
large amount of problem into smaller manageable subtasks that can be handled independently.
An algorithm is important in the planning and execution of a problem solution since it specifies
the sequence of steps needed to carry out the instructions that will lead to the overall result. An
algorithm can be defined as a sequential set of instructions that are followed in other to
solve a problem using a finite amount of data in a finite amount of time. More than one
algorithm can be written for a particular problem (see figure 1.0) but the choice of selecting a
particular algorithm in place of the other is dependent on the following factors: reliability,
accuracy, ease of modification, and time required for the execution when converted to a high-
level language.
Input: This represent the data that is to be sent for processing in the system. Most
algorithms provide some variables and the description of the data that the variable will
handle.
Processing: This is the action performed on data in other to produce the desired result.
Such actions like computation, selection, iteration, etc.
Output: This is the result produced after the data has been processed. When the
algorithm has been converted to programs, the data is usually displayed through the
output device, for example the monitor.
Properties of Algorithm
a. Input/output: The algorithm should be able to produce output value(s) when given
a set of specified input value(s). There should be a relationship between the input
and the output produced by the algorithm.
b. Finiteness: An algorithm should be able to terminate after a finite number of
instructions must have been processed. That means it does not have to take forever
to process a set of instructions.
c. Definiteness: The algorithm should be developed in a form that each step will be
precise and easy to understand.
d. Effectiveness: This means that the algorithm should be in a form that will be easy
to convert to a programming statement in a finite amount of time.
e. Generality: The algorithm should be able to produce the same output when given
different type of valid data (i.e. data taking from a specified domain).
Pseudo Code: This type of expression has a form that is closely related to a
programming language though there is no restriction on the syntax. It is not
ambiguous like the natural language.
Example 1.2: Write an algorithm to calculate the area of a triangle (area =
½ * base * height)
Solution:
Step 1: Start
Step 2: Read base and height
Step 3: Product = base x height
Step 4: Area = product/2
Step 5: Print Area
Step 6: Stop
Solution:
Table 1.0. Shapes or symbols to draw flow charts
Flowchart Symbol Function Description
Start/End Also called “Terminator” symbol. It indicates where
the flow starts and ends.
Top-Down Design
The most common problem solving approach that is used among humans is divide and conquer,
which means dividing a large problem into smaller parts that can be solved independently. This
is the concept used in Top-Down Design.
Top-down design, which is also called functional decomposition, is a problem-solving
methodology that involves splitting a large problem into smaller manageable parts that can be
solved independently and integrated to form the overall solution. The problems in a top-down
design are arranged in a hierarchical tree structure (structured chart), consisting of problems or
sub problems called modules.
A module is a self-contained set of steps needed for the solution of a problem. The modules
are arranged in a hierarchy from top to the bottom in a structure chart. The module at the top
is the main problem while the once at the bottom is a sub division of the problem. The modules
are numbered according to their position in the hierarchy; those in the same level are given the
same number. The numbering starts from top, which is usually zero, down to the bottom, which
can be any finite number, depending on the problem.
The reason for the division of the problems into modules, parts, or segments is to reduce the
complexity of the problem by sub dividing them into smaller manageable independent parts.
In the context of computer science, the responsibility of the module depends on the requirement
of the problem. Some of the module(s) could be responsible for data input, some may perform
processing of data, this may involve; mathematical, logical or relational operation, while some
may be responsible for the output.
As an example, let us refer to figure 2.1. The figure is a hierarchical tree structure (structured
chart) that consist of three levels (level 0, 1, 2, and 3), the top level or level 0 describes the
function of the overall problem. It is the abstract step because the problems at this level can
further be decomposed into smaller sub problems in the levels that follows. The last module at
the bottom (i.e. the module at level 3), that cannot be decomposed any further is called the
concrete step. Refer to case study II to see a detailed example of top-down design
methodology.
The following are the summary of the methodology used in top-down design.
Analyze the Problem - Your main goal at this point is to understand the
problem. Try to solve the problem by hand first. Identify all the variables (may
be data or any other information) you are going to work with. Specify the output
format and all the assumptions you are going to make about the problem
solution. The output of this phase is an algorithm or a solution plan.
Write the main module - Restate the problem in English or pseudo code form
and put it as the main module in a structured chart. Decompose the module into
sub modules and put them in the levels below the parent or top module; that is
if the module has too many functions or if the sentence is too long. Put the
whole modules in a logical sequence if there is need for that and defer the detail
until you get to the lowest module. Give each module a concise and meaningful
name.
Write the Remaining Modules - Continue breaking down the module until
you have gotten to the lowest level where you will provide all the necessary
details. This level is called the concrete step. That is the level you provide all
the necessary details. You can extend the number of level to any length of your
choice (depending on the problem) as you are not limited to any number of
modules or levels. However, the sequence should terminate after a finite
number of steps.
Re-Sequence and Revise as Necessary - Check if there is a need for change
or even to drop the solution if you discover that your plan is not solving the
problem. Try to refine the problem wherever necessary and try to be clear,
simple and direct.
Implementations strategies
Earlier we introduced general problem solving strategies using Polya’s approach. In this
section, we are going to limit our conversation to computer-based problem-solving strategy.
Problem solving is an activity undertaken in every programming or software development
environment, so it is an indispensable knowledge needed by every programmer or software
developer. Problem solving is not just limited to computer scientists, or programmers, it is also
used by business students, engineers and scientists though in a different way. In business
studies, students use systems approach, while in engineering and science, the engineers and
scientists use engineering and scientific method respectively, the programmers on the other
hand use software development approach, and that is going to be our topic of discussion in this
section. The software development methods are as follows:
a. Specify the problem requirements (Requirement Specification).
This involves a critical examination of the problem so as to get a better understanding
of the requirement needed to solve the problem. The problem is restated and every
unimportant aspect are eliminated at this point. This is to help in presenting the problem
in a clear and unambiguous form.
b. Analyze the problem (Analysis)
The identification of the input variables (i.e. the data) needed for the implementation of
the problem is done at this point. The expected output (i.e. the result) that is to be
obtained after the execution of the input data and the formats (example a table with
heading, rows and columns) of presenting the result are also identified. The use of
formulas or other methods as a means of expressing the relationship that exists with the
variables in the problem is strongly encouraged. It is important to note that if the first
step and this second step is not carried out properly, it will lead to error or failure in the
solution. See example 2.1 for explanation of this phase.
Example 2.1:
Given the three sides and height of a triangle, calculate in centimeter the area and
perimeter of the triangle.
Problem Input
Three sides (side 1, side 2, side 3) of the triangle (in centimeter)
Height (h) of the triangle (in centimeter)
Problem Output
Area of the triangle
Perimeter of the triangle
Formula to identify the relationship between the input and output
Once we have been able to determine the input and output of the problem, then we
can establish a relationship between the input and output variables using
formula(s).
Let the base = side3;
Perimeter = side1+side2+side3;
Area = ½ * base * h;
When solving a problem we extract those variables and relationship that are of
interest to us. The process of modelling a problem by extracting relevant
information is known as abstraction.
c. Design the algorithm to solve the problem (Design).
This phase involves developing the sequence of steps called an algorithm that will be
followed in other to solve a given problem, and after all, a verification will be made to
know if the steps taken was able to solve the problem. It is usually advisable that top-
down design (or divide and conquer) approach be adopted in carrying out this task of
algorithm design and development. Using top-down design implies that the overall
solution of the problem will be realized by unifying all the steps followed and solutions
obtained from the sub modules. Some steps taken at each module might need to be
expanded so that the required details will be provided to adequately, solve the problem.
This approach of expanding the steps by providing additional detailed steps to a
particular step is called stepwise refinement. Let us examine how a lecturer develop
his lecture note, he takes up a topic, break it down into sub topics (or course outline),
then for each of the sub topics he fills in some additional details that will enhance the
understanding of the course. This process of providing some additional details into a
sub topic or course outline is an example of stepwise refinement in top down design
process.
d. Implement the algorithm (Implementation).
After the algorithm has been developed, the next step, which is the implementation of
the algorithm, is to convert every step in the algorithm to an equivalent statement in a
programming language. One algorithm can be implemented using one or more
programming language(s).
e. Test and verify the completed program (Testing).
Testing and verification involves executing the program in the computer to know if it
is producing the desired result. Testing phase is a very tedious task, as the program has
to be tested given different circumstances and data.
f. Maintain and update the program (Maintenance).
After the program has been tested, certified and put into productive use, it will be made
to work under different hardware, operating system, geographic locations, security
conditions, etc. and will be expected to deliver its task effectively and uninterruptedly.
The program needs regular or periodic maintenance and update in case of any
malfunctioning or deviating from the originally intended task. This update will continue
for a period of five years or more depending on the organizational policies. During this
time, the original developers may have left the company thereby entrusting the
responsibility of the software maintenance on other people who may not have a good
understanding of the software. Thus, those people have to rely on the software
documentation for guidance. So, based on this, for the maintenance and update of a
program to be effective there is need for proper documentation at every point of
software development and maintenance. Maintenance and update is a means through
which software are released at interval to be installed in already existing software to
improve its functionality, correct faults, improve performance or address some security
challenges.
THE PROBLEM
You have been entrusted with the job of weather forecasting, and you have a device
that can only read temperature in Celsius and you are expected to report in Fahrenheit.
Write a program to convert every Celsius temperature reading recorded by your device
to Fahrenheit.
ANALYSIS
The first thing to do is to understand what is expected of you in the problem. The
problem says that you will be getting the Celsius temperature from your device, which
will be converted to Fahrenheit. Therefore, the input is Celsius and the output is
Fahrenheit. In this phase, we equally need to ascertain the relationship between the
input and the output. This can be established using the formula.
DATA REQUIREMENT
Required Input
o Constants
SCALERATE = 9.0/5.0;
TEMPSCALE = 32;
Required Output
Required Formulas
DESIGN
The next step will be to formulate the algorithm by listing the sequence of steps needed
to execute the problem. Such steps are listed below:
ALGORITHM
Step 1: Start
Step 2: Read Celsius Temperature
Step 3: Convert Celsius to Fahrenheit
Step 4: Print Fahrenheit
Step 5: Stop
To convey meaning and enhance better clarity, we have to refine some steps as follows:
o Step 1 refinement
Step 1: Read Celsius Temperature
1.1: Write “Enter Celsius Temperature”
1.2: Read Celsius temperature
Step 2 refinement
Step 2: Convert Celsius to Fahrenheit
2.1: Fahrenheit = (Celsius * 9/2) + 32
IMPLEMENTATION
The next step now is to convert every step of the algorithm to valid programming
statement using C++ language. To write the program, we need to state the kind of
input(data) to be collected, in this case we will use decimal numbers (i.e. float). We will
also identify the data in the memory (declare variable) with names Celsius and
Fahrenheit. Then we will proceed with the statement that will execute the algorithm
that does the conversion from Celsius to Fahrenheit.
Below shows the C++ program of the above algorithm. The program uses a comment
to explain the program statements. The detailed explanation of the program will be in
the next class.
/*
* C++ program to convert Celsius to fahrenheit
*/
#include<iostream> /* Definition for header file */
using namespace std;
int main (){ /* The program starts here */
Enter Celsius temperature value and press enter key when done >> 20
TESTING
Testing the program means verifying that the program will give the required output
when given a set of valid input data. You have to test the program by supplying it with
a set of valid input data and examining the data carefully to know if it corresponds with
the intended output. To be sure that the program is behaving well you have to test it
with variable input sets. In some cases, you might have to test how the program will
behave if given some unrelated input values. This has to be ascertained so that the
program will not crash when someone mistakenly put some unrelated input value(s).
ANALYSIS
From the problem, we can deduce that the solution will require two inputs - radius and
height of the cylinder, then one output will be required which is the volume. The input
values will be of float (fractional) type, since the constant pie is of decimal type, which
will change the integer variable to be of fractional type. The relationship that exist
between the variables (i.e. the input and output) will be listed subsequently.
DATA REQUIREMENT
Required Constant
PI 3.14 /* the value of the constant PI */
Required Input
Radius /* the radius of the cylinder */
Height /* the height of the cylinder */
Required Output
Volume /* the volume of the cylinder */
Required Formulas
Area = PI * Radius * Radius
Volume = Area * Height
DESIGN
IMPLEMENTATION
/*
* program to calculate the volume of a cylinder
*/
#include<iostream> /* Definition for header file */
using namespace std;
int main(){ /* The program starts here */
/* Perform variable declaration */
float radius;
float height;
float area;
float volume;
/* Read radius and height from keyboard */
cout<<”Enter the radius ";
cin >> radius;
/* Calculate volume */
area = 3.142 * radius * radius;
volume = area * height;
/* Print volume */
cout<<"The volume of the cylinder is " << volume;
return 0;
} /* the program finishes or terminates here */
TESTING
The same explanation given in case study I still holds in here so you can refer to testing
in case study I for a review.
Exercises
Algorithm questions
1. Write an algorithm to calculate simple interest
2. Write an algorithm to calculate area of triangle
3. Write an algorithm to find the largest of three numbers x, y, z
4. Write an algorithm to test if a given integer value is prime or not
Programming questions
5. Write a C program to calculate simple interest
6. Write a C program to calculate area of triangle
7. Write a C program to find the largest of three numbers x, y, z
8. Write a C program to test if a given integer value is prime or not