Chapter Four Problem Solving Using Computers

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

Introduction to Computing

Chapter Four

Problem Solving Using Computers


Objective
 This chapter introduces the fundamentals of computer program which refers to the set of
instructions needed for performing a particular task.
 Introduces the life cycle of the program development and various programming tools like
Algorithms, flowcharts, and pseudo codes which are essential for making a computer program.
 Finally, the students will be acquainted with program documentation along with the
characteristics of a good program.
1.1 . Introduction
No matter what field of works you choose, you may have to solve problems. Many of these can be
solved quickly and easily. Still others require considerable planning and forethought 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.

1.2 Steps Program Development


Computer programming is the art of conceiving a problem in terms of the steps to its solution and
expressing those steps as instructions for a computer system to follow. We stress the two parts to
this definition.
 The first part focuses on defining the problem and logical procedures to follow in solving it.
(Task of system analyst)
 The second introduces the means by which programmers communicate those procedures
to the computer system so that it can be executed. (Work of programmers).
System analyst use system analysis and design tools, particularly flowchart and structure chart, to
define the problem in terms of the steps to its solution. The programmer use programming language
to communicate the logic of the solution to the computer. Programming language is a collection of
commands that direct the control of the computer system. Like a human-to-human language,
programming languages contains a number of rules of expression called syntax.
Adama University, School of Engineering and Information Technology, Department of IT 1
Introduction to Computing
Program is a logical set of instructions that tells the computer what steps to take to solve a
problem. Computer solves varieties of problems that can be expressed in a finite number of steps
leading to a precisely defined goal by writing different programs. A program is not needed only to
solve a problem but also it should be reliable (main trainable), portable and efficient. This is met by
program design using techniques of software engineering.

There are nine stages of program development:


A. ANALYS/DEFINE 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.
This stage concerns with a deep understanding of the problems,
 Which part of the problem is going to be solved (Scope)?
 What outputs are expected?
 What procedures needed to achieve the result?
 What input data are needed to the problem to get the output?
There are various methods to find these specifications, like interview, observations, or for
simpler problems in written form, read the requirements carefully.
B. TASK ANALYSIS
After analyzing the problem the developer needs to develop various Solutions to solve the given
problem. From these solutions, the optimum solution (by experimenting with all the solutions) is
chosen, which can solve the problem comfortably and economically.

C. USING PROGRAMMING TOOLS

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

Adama University, School of Engineering and Information Technology, Department of IT 2


Introduction to Computing
There are different ways of describing the algorithm but the most common ones are verbal form,
flow chart and using any of existing human languages (Pseudo code). A flow chart consists of an
ordered set of standard symbols (mostly, geometrical shapes) which represent operations, data flow
or equipment. There are two types of flow charts. System flowcharts (the overall logic of data
processing system). A program flow chart shows the operations and logical decisions of a computer
program.
The standard flowchart Symbols and their meanings are given below.
Symbol Name Function

Terminal Used to represent the start of the end of a program

Input/ Used to represent data input or data output from a


output computer
Processing Usually encloses operations or (command black) a group
of operations( a process)
Decision it usually contains a question within it there are typically
block two output paths: one if the answer to the question is yes
(true) , and the other if the answer is no ( false)
Flow line is used to indicate the direction of logical flow ( a path
from one operation to another
On-page is used for connecting two points in connector a flow
chart without drawing flow lines In one page.
Off page It is used an exit to or any entry from another part of the
connector flowchart on another page

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:

Adama University, School of Engineering and Information Technology, Department of IT 3


Introduction to Computing
 There must be no ambiguity in any instruction.
 There should not be any uncertainty about which instruction is to be executed next.
 The description of the algorithm must be finite. An algorithm cannot be open-ended.
 The execution of the algorithm should conclude after a finite number of steps.
 The Algorithm must be general enough to deal with any contingency.
Example
1. Write an algorithm to determine the largest number out of three numbers A, B and C.
Step1: Start
Step2: Read three numbers say A, B, C
Step3: Find the Largest number between A and B and store it in
MAX_AB
Step4: Find the Largest number between MAX_AB and C and store in
MAX
Step5: Display MAX
Step6: Stop
2. Write an algorithm to determine the Average of N numbers
Step1: Start
Step2: Initialize Total_Num,Num,Sum,and Average to zero
Step3: Read total number of integers for Average is found and
store it in Total_Num.
Step4: While numbers to be added are less than Total_Num, follow
these two sub tasks repeatedly
Read Number and store it in Num
Add Num to Sum (Sum= Sum + Num)
Step5: Calculate the Average by using the formula
Average=Sum/Total_Num
Step6: Display the Average
Step7: Stop

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

Adama University, School of Engineering and Information Technology, Department of IT 4


Introduction to Computing
the intersecting parties as compared to actual program code as the users may not be aware of
all the programming techniques and jargons.
III. Effective Analysis: with the help of flowchart, a problem can be analyzed in the effective way.
IV. Useful in Coding: flowcharts act as a guide or blueprint during the analysis and program
development phase. Once the flowcharts are ready, the programmers can plan the coding
process effectively as they know where to begin and where to end making sure that no steps
are omitted.
V. Appropriate Documentation: Flowchart serves as a good program documentation tool. Since
normally the programs are developed for novice users, they can take the help of the program
documentation to know what the program actually does and how to use the program.
PSEUDO CODE.
It is not a real programming code, but it models and may even look like programming code. It
is a generic way of describing of an algorithm without using any specific programming language
related notations. Pseudo code is an outline of a program written in a form that can be easily
converted into real programming statements. Pseudo code uses plain English statements rather
than symbols, to present the processes of a computer program.
Example
Write a Pseudo code to determine the largest of the three numbers say: A, B and C.
Read A, B and C
IF A is greater thab B THEN
IF A is greater than C THEN
DISPLAY A
ELSE
DISPLAY C
ENDIF
ELSE
IF B is greater than C THEN
DISPLAY B
ELSE
DISPLAY C
ENDIF
ENDIF
STOP

ADVANTAGES OF USING PSEUDO CODE

Adama University, School of Engineering and Information Technology, Department of IT 5


Introduction to Computing
 Since it is language independent, it can be used by most programmers It allows the
developers to express the design in plain natural language.
 It is easier to develop a program from a pseudo code than with a flowchart.
 It is easier to translate pseudo code in to a programming language a step which can be
accomplished by less experienced programmers.
 Unlike flowcharts, pseudo code is compact and does not tend to run over many pages. Its
simple structure and readability makes it easier to modify as well.
 Since they inclined more towards natural languages, pseudo codes are self documenting.
DISADVANTAGES OF PSEUDO CODE
 It does not provide visual representation of the program’s procedure/logic
 There are no accepted standards for writing pseudo codes. Different programmers use their
own style of writing Pseudo codes.
 Example 1:- Draw flow chart of an algorithm to add two numbers and display their result.
Algorithm description
 Read the rules of the two numbers (a and b)
 Assign the sum of A and B to C
Start
 Display the result ( c)
Now the flow chart is

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

Sometimes there are conditions in which it is necessary to execute a group of statements


repeatedly. Until some condition is satisfied. This condition is called a loop. Loop is a sequence of
instructions which is repeated until some specific condition occurs.
A loop normally consists of four parts. There are:-
Initialization: - Setting of variables of the computation to their initial values and setting the counter
for determining to exit from the loop.
Computation: - Processing
Test: - Every loop must have some way of exiting from it or else the program would endlessly
remain in a loop.
Increamentation: - Re initialization of the loop for the next loop.

Adama University, School of Engineering and Information Technology, Department of IT 7


Introduction to Computing
Initialization

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

2. If the counter is less than or equal to 50


 Add counter to sum Sum = 0
Counter =1
 Increase counter by 1
 Repeat step 2
Write sum
 Else Counter>50 Yes
 Exit
3. Write sum
Sum = Sum + counter Stop
N0

Counter = Counter + 1

Adama University, School of Engineering and Information Technology, Department of IT 8


Introduction to Computing

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

D. TESTING THE ALGORITHM FOR ACCURACY


Before converting the Algorithms into actual code, it should be checked for accuracy. The main
purpose of checking algorithm is to identify major logical errors at an early stage, because logical
errors are often difficult to detect and correct at later stages. The testing also ensures that the
algorithm is a ‘true’ one and it should work for both normal as well as unusual data. Test data needs
to be ‘walked through’ each step in the algorithm to verify that the instructions described in the
algorithm actually perform the required functions.
E. CODING THE SOLUTION
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.

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.

Adama University, School of Engineering and Information Technology, Department of IT 9


Introduction to Computing
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. For example if we want to write in C ++ programming language, each step will be described
by an equivalent C++ instruction ( Statement).
F. TESTING AND DEBUGING THE PROGRAM
It is not unusual for the initial program code to contain errors. The errors may be in the form of
logical errors (semantic errors) or incorrect use of programming language (grammatical errors are
also known as Syntax errors). A Program compiler and programmer designed test data machine tests
the code for syntax errors. The results obtained and compared with results calculated manually
from this test data. Depending on the complexity of the program, several rounds of testing may be
required.
G. DOCUMENTATION
Once the program is free from all the errors, it is the duty of the program developers to ensure that
the program is supported by suitable documentation. These documents should be supplied to the
program users. Documenting a program enables the user to operate the program correctly.
H. IMPLEMENTATION
This is the final stage where the program is debugged for error, tested and documented then placed
for actual work. The user is also provided with the essential documents so that he/she can
understand how the program works.

I. MAINTENANCE AND ENHANCEMENT


Some time it may be needed to modify the program so that it accommodates new things and it
becomes efficient, and this can be done through maintaining the program. The program should be
regularly enhanced by adding additional capabilities. This phase is also concerned with detecting
and fixing these errors, which were missed in testing phase. Since this step generates user
feedbacks, the programming cycle continues as the program is modified or reconstructed to meet
the changing needs.

Adama University, School of Engineering and Information Technology, Department of IT 10


Introduction to Computing

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,

Adama University, School of Engineering and Information Technology, Department of IT 11


Introduction to Computing
unconditional and conditional. An Unconditional branch always causes the program to jump to the
branch address. A conditional branch only if a specified condition is met.
4) Information Monument Instruction (storage/ retrieval/:- This group of instructions takes on the
task of moving information from one register (or storage location) to another. Every programming
language has its own sets of instruction and it has its own way of writing the 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.

Adama University, School of Engineering and Information Technology, Department of IT 12


Introduction to Computing

TYPES OF PROGRAMMING LANGUAGES

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

Adama University, School of Engineering and Information Technology, Department of IT 13


Introduction to Computing
Examples of High level languages include: FORTRAN, COBL, ALGOG 80, Basic, Scientific &
engineering applications.
 Fortran:- Formula in Business Data Processing Application
 COBOL-:-Common Business Oriented Language
 ALGOL 80:- ( Algorithmic Oriented Language)
 BASIC:- ( Beginners All-purpose symbolic Instruction code) simplest language developed for
solving numerical problem
 Pascal, L, Ada, and Modula-2:- Used in teaching programming language and the other is C, C++
There are also other languages which are still simplest and easier then high-level languages which
we called then fourth generation languages. These languages are application oriented languages.
 Ex:- Visual basic, C++
Translation and Execution
The only language that the computer understands is the machine language. Therefore any program
that is written in either low-level or high level language must be translated to machine code so that
it could be processed by the computer.

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

CHARACTERSTICS OF A GOOD PROGRAM


Every Computer requires appropriate instruction sets (programs) to perform the required task. The
quality of the processing depends up on the given instructions (incorrect or correct). A few
important characteristics that a computer programs should posses are:
1. Portability: refers to the ability of an application to run on different platforms (operating Systems)
without or with minimal changes. Due to rapid development in hardware and the software,
nowadays platform changes in common phenomena. Hence, if a program is developed for a
particular platform then the life span of the program is severely affected.
2. Readability: The program should be written in such a way that it makes other programmers or users
follow the logic of the program without much effort. If a program is written structurally, it even
helps the programmer himself to follow his own program in a better way if he is away from it for
some time.
3. Efficiency: Every program requires certain processing time and memory to process the instructions
and data. As you must have realized processing power and memory are the most precious resources
of a computer, a program should be laid out in such a manner that it utilizes the least amount of
memory and processing time.
4. Structural: to develop a program the task must be broken down into a number of subtasks. These
subtasks should be developed independently i.e each subtasks should perform the assigned job
without the help of any other subtasks.
5. Flexibility: A program should be flexible enough to handle most of the changes without having to
rewrite the entire program. They should be flexible for changes from time to time.
6. Generality: Apart from flexibility, the program should also be general. By generality we means, that
if a program is developed for a particular task then it should also be used for all similar tasks of the
same domain. For example, if a program is developed for a particular organization, then it should
suit all the other similar organizations.
7. Documentation:
Documentation: is one of the most important components of an application development. Even if a
program is developed following the best programming practices, it will be rendered useless if the
end user is not able to fully utilize the functionality of the application. A wel-documented

Adama University, School of Engineering and Information Technology, Department of IT 15


Introduction to Computing
application is also useful for programmers because even in the absence of the author, other
programmers can understand it.

Adama University, School of Engineering and Information Technology, Department of IT 16

You might also like