You are on page 1of 7

CHAPTER FOUR

Problem solving using computers


Introduction
No matter what field of work you choose, you may have to solve
problems. Many of these can be solved quickly and easily. Still others
require considerable planning and forethought (precaution) if the
solution is to be appropriate and efficient. Problem in this sense is one
that can be solved using computers, some examples,
Requirements for:
 Handling and manipulating employee data
 Solving mathematical equations based on formula
 Handling and manipulation of information about students etc.
A problem may be real problem (existing problem), or anticipated
(potential) problem. The reasons (initiation) for studying (identifying) a
given problems may be
 Directives (in organizations)
 Opportunities (to do things better)
 Or other things.

4.1. Problem solving


Creating a program is no different because a program is a solution
developed to solve a particular problem. As such, writing a program is
almost the last step in a process of first determining what the problem
is and the method that will be used to solve the problem. In solving a
problem, we may get various solutions.
One technique used by professional software developers for
understanding the problem that is being solved and for creating an
effective and appropriate software solution is called the software
development procedure. The procedure consists of three overlapping
phases
 Development and Design
 Documentation
 Maintenance

Phase I: Development and Design


The development and analysis phase consists of four different
overlapping phases, which includes:
 analyzing a problem
 develop a solution
 coding the solution
 testing

I. analyzing the problem


Before starting any part of the solution, we have to be introduced
with the problem. First, we must understand thoroughly what the
problem is. This should lead us to complete specifications of the
problem to be addressed.

Au-SOEIT-Dep’t of IT Page 1 of 7
 What part of the problem is going to be solved?
 What input data is required to solve the problem?
 What output data (result) is expected?
 What procedures are needed to achieve the result?

There are various methods to find these specifications, like interview,


observations, or for simpler problems in written form, read the
requirements carefully.
II. Developing the Solution
Programming is all about solving problems. This step involves the
following tasks:
 Divide the original problem into a number of sub problems.
These sub- problems, being necessarily smaller than the
original problem, are easier to solve and their solution will be
the components of our final solution. This method, by which a
complex problem is divided into smaller and easier sub-
problems is called “divide and conquer”
 Associating each task with a precise method to accomplish it,
which consists of a sequence of well defined steps which, when
carried out, perform the corresponding task. This sequence of
steps is called and Algorithm.
Algorithm is a sequence of steps that describes how the data are
to be processed to produce the desired outputs. Algorithms are usually
developed in pseudo code or using flowchart.
This stage is sketching the step-by-step activities that lead to the
solution. This is formulating a problem in terms of the steps to its
solution.

III. Coding the program


Algorithms cannot be executed (performed) directly by the
computer. They must first be translated into a kind of data that the
computer can understand. This step consists of translating the
algorithm into a computer program using a programming language
and the process is referred to as coding.
It is expressing the solutions (in the previous step) in one of the
various programming language.
There are many high-level programming languages like BASIC,
COBOL, Pascal, FORTRAN, C, C++, VB, etc. To get our program work,
we write it based on the syntax rules of the programming language.

IV. Testing the program


It is very rare that a program can be written correctly the first
time. Most programmers get used to the idea that there are errors in
their newly written programs, these errors are detected during testing
the program, and appropriate revision must be made and the tests
return. Testing provide a way to ensure whether a program provide
the intended solution to the problem.

Au-SOEIT-Dep’t of IT Page 2 of 7
Figure below lists the relative amount of effort that is typically expended on
each of these four development and design steps in large commercial programming
projects.
Step Effort (%)
Analyzing the solution 10
Developing the solution 20
Coding the solution 20
Testing the solution 50

In general, the process of coding and testing is called implementation.

Phase II: Documentation


Documentation is compiling related documents throughout the
lifetime of the program development. Documentation requires
collecting critical documents during the analysis, design, coding, and
testing.
There are five documents for every program solution:
 Program description
 Algorithm development and changes
 Well-commented program listing
 Sample test runs
 User’s manual

Phase III: Maintenance


This phase is concerned with the ongoing correction of problems,
revisions to meet changing needs and the addition of new features.
Maintenance is often the major effort, and the longest lasting of the
three phases. While development may take days or months,
maintenance may continue for years or decades.

Algorithms and flowcharts


Programming
The computer is directed by a program, i.e., a sequence of
instructions that determine the operations to be carried out by the
machine. Human or the user writes programs. Computer
programming is the process of program design and implementation by
the use of the computer. The user must specify the operations to be
performed. Natural languages like English, used for human
communication are not fit for programming because of their ambiguity
and lack of precision. Therefore program can be written in machine
language, assembly languages or high-level languages.
A compiler is used to convert the program source code entered
in HLL to a machine code. Each statement in a high level language
can be converted into a number of machine code instructions.

Au-SOEIT-Dep’t of IT Page 3 of 7
Design and Implementation of Algorithms:

An algorithm is a finite set of instruction that specify a sequence


of operations to be carried out in order to solve a specific problem or
class of problems. It is just a tool for solving a problem. All the tasks
that can be carried out by a computer can be stated as algorithms.
Once an algorithm is designed, it is coded in a programming language
and computer executes the program. Algorithms may be presented on
several levels of detail. It is difficult to use terms of machine code
instructions or high level language because the details would obscure
the essence of the procedure and due to difficulty to present long
programs in simple form. Therefore algorithm designers must start
with a much more concise representation

A. Pseudo code
Pseudo code is an artificial and informal language that helps
programmers to develop algorithms. Pseudo code is a language used
to describe the manipulations to be performed on data. This language
is a simple mixture of natural language and mathematical notations
and it is independent of programming language. Pseudo code has
some ways to represent sequence, decision and repetition in
algorithms.
Example: 1) Pseudo code to add two numbers.
Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: sum  n1 + n2
Step 4: Print sum
Step 5: Stop
Example: 2) pseudo code to find largest number from two numbers.
Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: If n1 > n2 then
Big  n1
else
Big  n2
Step 4: Print Big
Step 5: Stop
Example: 3) pseudo code to find largest number from three numbers.
Step 1: start
Step 2: Read three numbers n1, n2 and n3.
Step 3: If n1 > n2 and n1 > n3 then
Big  n1
Else
If n2 > n1 and n2 > n3 then
Big  n2
else
Big  n3
Step 4: Print Big
Step 5: Stop.

Au-SOEIT-Dep’t of IT Page 4 of 7
Loops
Some times there is a situation in which it is necessary to
execute a group of statements repeatedly until some condition is
satisfied. This situation is called a loop. Loop is a sequence of
instruction, which is repeated until some specific condition occurs.
The general execution flow structure of loops looks like the following.
[Initialization]

[test] false [stop]

true

[Compute]

[Increment/Decrement]

The above simple structure tells that execution continues as far as the
test holds true.
Example 4) Pseudo code to find sum of N positive integer numbers.
Step 1: start
Step 2: Read N
Step 3: Sum  0,
Step 4: Count  0
Step 5: Read Num
Step 6: SumSum + Num
Step 7: count  count +1
Step 8: If Count < N then goto step 4
Step 9: Print Sum
Step 10: Stop
Assignment:
1) Write an algorithm to find the roots of quadratic equation
ax 2  bx  c  0 . using the formula x=  b  ( b 2  4ac ) / 2a . Depending on
the values of D Where D  b 2  4ac
2) Write an algorithm to check whether the given number is even or
odd and display the suitable message.

B. Flow charts:
Due to the detail required of them, programming languages are not
convenient tools for initial algorithm design. The means of notation
widely used for algorithm is a flowchart. Flowchart is a two-
dimensional representation of an algorithm; the predefined graphic
symbols of a flowchart are used to indicate the various operations and
the flow of control. The most significant advantage of flowcharts is a
clear presentation of the flow of control in the algorithm, i.e. the
sequence in which operations are performed.

Au-SOEIT-Dep’t of IT Page 5 of 7
A basic set of established flowchart symbols is:
Decision
Processing Input/output

START & STOP

Connector
Annotation
Flow lines
The symbols have the following meanings:
Processing: one or more computational tasks are to be performed
sequentially
Input/Output: Data are to be read into the computer memory from
an input device or data are to be passed from the memory to an
output device. (Parallelogram)
Decision: two alternative execution paths are possible. The path to be
followed is selected during the execution by testing whether or not the
condition specified within the outline is fulfilled. (Rhombus)
Terminals: appears either at the beginning of a flowchart (and
contains the word “start”) or at its conclusion (and contains “stop”).
Annotation: contains comments that simplify the understanding of
the algorithm or description of data.
Connector: makes it possible to separate a flowchart into parts.
Identical cross reference symbols are placed in this outline where the
flow line is interrupted and where it resumes.
Flow lines: indicates the outline that is to be entered next.
Flowcharts allow the reader to follow the logic of the algorithm more
easily than would a linear description in English.
1. A flowchart to find factorial 2. A Flowchart to find
of a positive integer n largest of two numbers.
START
Start

Input n
Read A,
B.
F=1

Is
No
Display F A>B ?
n>
0? Yes

END Display Display


F= F*n is B is
Largest Largest
Flowchart versus pseudo code
n = n -1 Stop

Au-SOEIT-Dep’t of IT Page 6 of 7
Since flowcharts are inconvenient to revise, they have fall out of
favour by programmers. Nowadays, the use of pseudo code has gained
increasing acceptance.
Only after an algorithm has been selected and the programmer
understands the steps required can the algorithm be written using
computer-language statements. The writing of an algorithm using
computer-language statements is called coding the algorithm, which is
the third step in our program development process.

Exercise:
Algorithms:
1. Write an algorithm to find the smallest number from three
numbers.
2. Write an algorithm to find the sum of first N even numbers.
3. Write an algorithm to generate Fibonacci series.(a series which
goes like 1,1,2,3,5,8,13,…)
4. Write an algorithm to find the sum of digits of given number.
(eg. If the given number is 251, the sum of digits is 2+5+1=8)
Flowcharts:
1. Draw a flowchart to find sum of N positive numbers.
2. Draw a flowchart to find the biggest among N numbers.
3. Draw a flowchart to find the Factorial of a given number.
Programming paradigms
A programming paradigm is a fundamental style of computer
programming. The following are examples of programming paradigms:
1. Imperative Paradigm/Procedural: The 'first do this, next do that' is a
short phrase which really in a nutshell describes the spirit of the imperative paradigm.
The basic idea is the command, which has a measurable effect on the program state.
The phrase also reflects that the order to the commands is important. 'First do that,
then do this' would be different from 'first do this, then do that'.
2. Functional Paradigm: Functional programming is in many respects
a simpler and cleaner programming paradigm than the imperative
one. The reason is that the paradigm originates from a purely
mathematical discipline: the theory of functions.
3. Logic Paradigm: The logic paradigm is dramatically different from
the other three main programming paradigms. The logic paradigm fits
extremely well when applied in problem domains that deal with the
extraction of knowledge from basic facts and relations. The logical
paradigm seems less natural in the more general areas of
computation.
4. Object Oriented Paradigm: The object-oriented paradigm has
gained great popularity in the recent decade. The primary and most
direct reason is undoubtedly the strong support of encapsulation and
the logical grouping of program aspects. These properties are very
important when programs become larger and larger.

Au-SOEIT-Dep’t of IT Page 7 of 7

You might also like