Chapter Four Problem Solving Using Computers
Chapter Four Problem Solving Using Computers
Chapter Four Problem Solving Using Computers
Chapter Four
After selecting the appropriate solution, algorithm is developed to depict the basic logic/procedure
of the selected solution. An algorithm is a finite set of well defined rules for the solution of a
problem in a finite number of steps. To design an algorithm for a specific problem, first we break
down the problem into simpler and manageable tasks. For one problem there may be a lot of
algorithms that help to solve the problem (Looking for Alternative Solutions), but the algorithm that
we select must be powerful, easy to maintain, and efficient ( it doesn’t take too much space and
time)- Selection of the best solution
The advantage of flowchart is it doesn’t depend on any particular programming language, so that it
can be used, to translate an algorithm to more than one programming language.
ALGORITHMS
Algorithms are one of the basic tools that are used to develop the problem solving logic.
Different Algorithms may accomplish the same task, with a different set of instructions, in more or
less the same time, space and efforts. Note that algorithms are not computer programs, as they
cannot be executed by a computer.
An algorithm must have the following properties:
FLOWCHART
Flowchart is a pictorial representation of algorithms in which the steps are drawn in the form of
different shapes of boxes and logical flow is indicated by interconnecting arrows. The boxes boxes
represent operations and the arrows represent the sequence in which the operations are
implemented.
Flowcharts are important for:
I. Make the Logic Clear: As it provides the pictorial representation, it makes the logic easier to
follow. The symbols are connected in such a way that they show the movement (flow) of
information through the system visibly.
II. Communication: Flowchart is better way of communicating the logic of a system to all
concerned. That is the diagrammatical representation of logic is easier to communicate to all
Read a, b
C= a + b
Print C
Stop
Ex 2:- Write an algorithm description and draw a flow chart to find the following sum.
Adama University, School of Engineering and Information Technology, Department of IT 6
Introduction to Computing
Sum = 1+2+3+4
Ex2:- Write an algorithm description and draw a flow chart to check whether a number is negative
or not algorithm description.
1) Read a number x
2) If x is less than zero write a message” negative”
Else write a message not “negative”
Start
Read x
Print message
Write not No Yes write negative
negative X<0
Stop
Finished
Test Stop
Computation
Incrimination
Ex: - Write the algorithmic description and draw a flow chart to find the following sum.
Sum = 1+2+3+…. + 50
1. Initialize sum to 0 Counter to 1 Start
Counter = Counter + 1
Assignments
Ex2: Write a flow chart to find sum of the first N positive numbers.
Ex 3: Write a flow chart to find n!
Exercise Find the following sum
Sum = 1 +2! +3! +…+n!
Ex4: Draw a flow chart that accepts a salary of an individual and calculate pension, income tax and
net salary and display the results.
Pension = 10%
I.T equals 0% for salary between 0-149
10% 150-599
20 % 600-1199
30 % above 1200
The flow chart is independent of programming language. Now at this stage, we translate
each step, described in the flow chart (Algorithm description) to equivalent instructions of target
programming languages.
Maintain
Implement
and
ation
Enhance
Document Analyze
ation Problem
Program
Development
Test &
Debug
Program
Cycle Task
Analysis
Algorithm
Coding Developm
ent
Algorithm
Testing
TYPES OF INSTRUCTION
A program is a set of instructions (steps) that a computer should follow to perform a given task. The
instructions should be written in a language (or can be translated to a language) that can be
understood by the computer. The language that is used to write instructions is called a
programming language. Depending on the function the instruction performed, instructions are
categorized as follows:
1) Input/ output Instructions: These instructions perform interfacing (communication) between the
user and the computer through the I/O peripherals Transfer data between peripherals and memory,
or between peripherals and accumulators.
2) Arithmetic and Logic Instructions:- These instructions perform arithmetic operations ( addition,
subtraction, multiplication and division) and logical comparison.
3) Branch (or control transfer) Instructions: - A branch instruction (also called a jump instruction)
alters the normal sequence of program execution and is used to create loops that allow the same
sequence of instructions to be executed many times. There are two types of branch instructions,
PROGRAMMING PARADIGMS
Programming involves many hardships in terms man power, time and money. Usually a programmer
is busier in maintaining an existing application rather than creating a new one. Hence, there are
different programming paradigms have been developed in order to minimize the programming
efforts. Programming paradigms refer to how program is written in order to solve a problem.
Broadly, programming can be classified in to two catagories.
o Unstructured Programming
o Structured Programming
Unstructured Programming:
This programming style refers to writing small and simple programs consisting of only one main
program. All the actions like inputs, outputs, and processing is done within one program only. This
type of programming is generally restricted for developing a small application but if the application
becomes large then it poses real difficulties in terms of clarity of the code, modifiability and ease of
use. Therefore, this approach is meant for simpler and small problems.
Structured Programming: If the problem gets lengthy, it becomes too complex and obscure and
after some time, even the programmer may find it difficult to understand his own program. Hence,
programming should be performed using an organized that is structured programming approach.
Using Structured programming, a program is broken down in to small independent tasks, each
having a single point of entry and exit.
Object Oriented Programming: The object oriented paradigm has gained great popularity in the
recent decades. 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.
We have seen that a computer system consists of different layers. It is possible to communicate
with the computer by writing a program at the different layers but basically there are three types of
programming languages:- Machine, low-level and high- level.
Machine language:- There is the only language that the computer understands directly zero. A
machine language is a set of machine instructions which consists of zeros and one’s. a machine
instruction contains two parts an operation code ( op code) and an address. The OP code tells the
microprocessor system what operation it should perform, add, transfer, compare, or move data to
output device, etc. The address identifies the location (memory, register) holding the required
operands that is, the data to be operated upon. The address part may contain one, two or more
addresses that is, there may be one (or single address, two (double) address, and three (or triple)
address instructions.
Assembly Language:- In machine language we have seen that the OP code and the address are
represented as a binary sequence but it is difficult for the programmer to write a big program using
binary sequence and it is difficult to debug an error from such program so instead of representing
the OP code and the adders as a binary sequence we can represent them in mnemonics ( terms). A
Assembly language is a programming language which uses mnemonics to write in program Low-level
languages are machine dependent.
High-level language:- We have seen that writing a program in low-level languages is easier and
simple compare to machine languages. But still machine has its own drawbacks in the sense that
are they are machine dependent. So we need another type of languages which are not machine
dependent and more flexible (human like languages). These languages are called high-level
languages third generation languages.
Advantages of high-level languages:-
Easier to learn and understand ( Look like human languages e.g. English)
Require less time to write and easier to debug errors.
Can be used on different machines with little modifications (platform/machine independent)-
portable
A program written in high-level or low-level language is called source code program and, the
translated machine code is called the object code program. Programs that translate a program
written in high level language and low-level language to machine code program are called
translators.
There are three types of translators; assembler, interpreter, and compiler.
Assembler: - A program that translate in source program written in a how-level language (assembly)
to machine- code.
Interpreter: - A translator that accepts one line of a source program at a time produces the
corresponding machine code instructions and executes them.
Compiler:-A program designed to translate a high-level language source program into a machine
code program. It translates the whole source program at once.
The translator not only translates the instructions into machine code but also it detects whether the
program fulfills the syntax of the programming language. A program passes through different stages
before it carries out its function. First the program will be translate to object code (compilation
time), then it will be loaded to the memory and finally it will be executed (run time) or carries out its
function.Source Assembler Object
program Or compiler Program
Adama University, School of Engineeringorand Information Technology, Department of IT 14
Interpreter
Introduction to Computing