0% found this document useful (0 votes)
3 views9 pages

Chapter Two Programming

Chapter 2 introduces problem solving and algorithms in programming, emphasizing the importance of a structured approach through six software development steps: problem definition, algorithm design, coding, testing, implementation, and maintenance. It discusses the characteristics of good algorithms and the use of flowcharts and pseudo-code for representing algorithms. The chapter also provides examples and exercises to illustrate how to write pseudo-code and draw flowcharts for various programming tasks.

Uploaded by

euaelab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Chapter Two Programming

Chapter 2 introduces problem solving and algorithms in programming, emphasizing the importance of a structured approach through six software development steps: problem definition, algorithm design, coding, testing, implementation, and maintenance. It discusses the characteristics of good algorithms and the use of flowcharts and pseudo-code for representing algorithms. The chapter also provides examples and exercises to illustrate how to write pseudo-code and draw flowcharts for various programming tasks.

Uploaded by

euaelab
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER 2

INTRODUCTION TO PROBLEM SOLVING AND ALGORITHM

2.1 INTRODUCTION TO PROBLEM SOLVING

Writing Computer Programs


Similar to language learning, there is no better teacher than experience to learn how to write
acomputer program. Good organization is a key to writing good programs. Informally, you
can organize your program writing by first thinking, then writing your thought with your
language and then by translating the instruction in to a target programming language such as
C++. Formally you can use the following 6 Software Development Steps to write good
computer programs.
 Problem Definition (Analysis)
 Algorithm Design
 Coding
 Testing
 Implementation
 Maintenance
Analysis
Requires thorough understanding of the problem at hand and analysis of the data and
proceduresneeded to achieve the desired result. This step focuses on what must be done rather
how to do [Link] is what input are needed, what outputs are expected and what must be done to
produce theoutput.
Algorithm design
An algorithm is a finite set of steps which if followed accomplishes a partial task.
Modernapproaches to programming make use of top-down programming for complicated
problems. Thetechniques suggest that the problem be broken down in to simpler and manageable
tasks in astep-by-step fashion, until we eventually end up with subtasks that can easily be
[Link] task finally be expressed in terms of an [Link] is used to
express the ideas of a program, and is called pseudo-code. Often,programmers will design their
programs in pseudocode, and then use this to write their actualcode.

Hope University College 1


Testing: To check if it is correct, i.e. if it does what it is supposed to do. Testing
includesdebuggingwhich is the process of removing program bugs or errors. Errors in a program
are called bugs.
Implementation: Installing the Program in the real business environment
Maintenance:Maintenance is the process of enhancing the program to include new features as
necessary. Itmay involve all steps from requirements analysis to testing due to new user
requirements,undiscovered bugs, etc.
Characteristics of good algorithm
 Unambiguous: It should be expressed neatly and easy to understand manner. It should
not be vague.
 Precisely defined:Each step in the algorithm needs to be written clearly and exactly in a
simple possible manner.
 Finite and effective: This means an algorithm required to have a starting point and an
ending point, and it should solve the problem. An algorithm should not have infinite
number of steps.
 Easy to modify: An algorithm should be designed in such a way that alterability can be
done easily both at the time of including additional features in the existing one and at the
time of fixing errors.
 Efficient: The way we design algorithm needs to consider minimum utilization of
computer resources such as CPU time it consumes and memory storage space it requires.
Efficient algorithm is the one that utilizes smaller memory space and smaller processor
time at the period of execution.
2.2 INTRODUCTION TO FLOW CHART AND PSEUDO CODE

An algorithm can be described in many forms such as prose(text narrative), diagrams (pictures
and flowcharts), or pseudo-code (a high-level abstraction of code). Regardless of the method
chosen to represent the algorithm, another reader should be able to:
 Read your algorithm,
 Determine what problem you are attempting to solve, and
 Identify problems in your procedure if they exist.
Pseudo-Code: English (or any human language) is often used to describe or narrate the
algorithm.

Hope University College 2


Flow Chart: is a diagram consisting of symbols together with arrows connecting one symbol to
another. program flowchart shows the operations and logical decisions of a computer program

Basic Symbols of Flow Charts

Terminal represent beginning and end of a program

Flow line shows the direction of logical flow. The direction of the arrow
can be Up, Down, Left, Right

I/O shows Input data that the program accept from user and display
Outputs of the program or results.

Processing which shows arithmetic expression and data assignment to


represent data manipulation.

Decision represent logic comparison operations that leads you different


paths on the flow chart.

Intra-page connector uses to connect portions of the same flow chart


A

drawn on the same page without drawing a flow line on the same page.

Inter-page connector: same as Inter-page connector but this connector is


used to connect portions of a single flow chart drawn in different pages.

Preparation: contains data or notations to be used latter in the flow chart

2.3 HOW TO WRITE A PSEUDO CODE AND DRAW A FLOW CHART FOR A
GIVEN ALGORITHM

Example I: Write the Pseudo Code and Draw the flowchart for program that computes the area
of a rectangle.
Solution:
1. What is the problem?
 Ans: Computing Area of a rectangle

Hope University College 3


2. What is required to compute area of a rectangle?
 Ans: From mathematics class we know that in order to compute area of a
rectangle we need to be given with its width and length
3. How do we compute area of a rectangle?
 Ans: Area=Length *Width (where * is product in computer notation)
The above is an example how you solve a problem using computers. We can summarize the
whole process as
 You have to define the problem. If you can’t understand the problem you can’t
solve it. There is a saying that sates “a problem understood is half solved”.
 Then you need to know the inputs of your program
 Finally you have to think of how the inputs should be manipulated in order to
produce the out put
4. Here is how we state the algorithm using pseudo-code and flow chart

Remark
Useful guidelines for drawing program flowcharts:
 Every program flowchart begins with the oval symbol containing START/
BEGIN end with STOP or END.
 Crossing flow lines should be avoided
 You can use operators like + for addition, * for multiplication, - for

subtraction, / for division and % for modulo( remainder operator) that is


it returns the remainder of the two operands when divided.
Example: 3%2=1, 6%3=0, 9%4=1, 11%4= 3 etc.

Hope University College 4


START
Algorithm description:
1. Start
READ: Width, Length
2. Read Length of rectangle
3. Read Width of rectangle
Area = Length * Width
4. Area is the product of Length and
Width
5. Print or Display Area WRITE, Area

6. Stop/End of the program


STOP

Exercises 1: Write the pseudo-code and draw the flow chart of the following programs
a. A program that computes the Area of a Circle.
b. A program that computes the Circumference of a circle.
c. A program that computes the Area of Triangle.
d. A program that converts Weight in Kilogram into Gram
e. A program that converts Length in meters into centimeters
f. A program that converts temperature in degree temperature in 0C to 0F.
g. A program that reads a number and compute its inverse
h. A program that accept two integers from user and compute their sum and average.
i. A program that reads the age of a student and converts it into months.
j. A program that accepts number of days from user and calculate i) no of hours in the days
ii) number of minutes in the days and iii) number of seconds in the days.

Example II: A certain company plans to give a 10 % salary bonus to each of its employees at
the end of every year. If an employee has been working 20 or more years at the company, he is
to get additional birr 100. Write an algorithm description and Draw a flow chart.
Solution
In order to write an algorithm for the above problem we have to thoroughly read the question try
to solve it by answering the following guiding questions.

1. What is the problem?

Hope University College 5


Answer: Calculate the bonus that a company plans to provide to its employee
2. What is required to calculate the bonus?
Answer: From the question we can see that bonus is calculated based on salary of the
employee taking his/her years of experience in to consideration. Hence SALARY and
YEARS of Experience are inputs that the program needs to be given
3. How Do we Calculate the Bonus?
Answer: We can see from the plan of the company as stated in the question that two
options exists for the calculation
 If years of experience of the employee is below 20 years the bonus calculation
will be using the formula BONUS=10% * Employee Salary
 Otherwise if Employee’s Years of experience is greater than 20 then the formula
to compute bonus will be BONUS=(10% *Employee Salary) + 100
4. Here is the pseudo code and flow chart description of the algorithm

Algorithm description
1. Start START
2. Read employee’s
salary and years of service
READ: SAL, YEAR
3. Calculate the employee bonus.
4. If year of service is greater
BONUS = 0.1*SAL
than or equal to 20 then
increase his bonus by 100.
5. Print the bonus. YEAR>=20 Ye BONUS = BONUS + 100
s
6. End of the algorithm
No

WRITE: BONUS

STOP

Hope University College 6


Exercises 2: Write the pseudo-code and draw the flow chart of the following programs
a. A program that accept two integers and display the largest one.
b. A program that accepts two integers X and Y and displays either X equals to Y, X is less
than Y or X is greater than Y by comparing their values.
c. A program that determines whether the integer it reads is POSITIVE, NEGATIVE or ZERO
d. A program that determines whether a number given is ODD or Even (hint use modulo
operator)
e. A program that determines a given integer is multiple of 3 or not.
f. A program that accepts AGE of citizens and displays CHILD if the age is less than 15,
YOUNG if the age is less than 30 and ADULT otherwise
g. A program that solves a quadratic equation ax2 +bx +c=0
h. Calculate the net salary of an employee given her Gross salary and her employment type.

Gross Salary Income Tax Bonus


Employment Type
Permanent Contractual
0-150 0 500
151-600 10% 550

2 months’
salary
600-1200 15% 600
1200-2600 20% 700
2600-5000 30% 800
>5000 35% 1000

Pension= 10% of G. Salary only for permanent staff


Net Salary= Gross Salary –Income Tax –Pension + Bonus

Example III: Write the Pseudo code and draw the flow chart for a program that displays
all-natural numbers less than 10. ( i.e 1,2,3,4,5,6,7,8,9)

Hope University College 7


Solution
Start AlgorithmDescription

1. Start
2. Declare a counter c and assign it
with initial value 1
3. Check if C< 10 and if itis true
then
C=1 i. display the value of c
ii. increment the value of c
by 1
iii. go to step 3 to check c<10
and repeat the above
processes until c>=10
4. If C>=10 then it is
C<10 Yes Display C C=C+1
End of the program.

No

Stop

Hope University College 8


Exercises 3: Write the pseudo-code and draw the flow chart of the following programs
a. A program that displays only evennatural numbers lessthan or equal to 20.
b. A program that computes the sum of all-natural numbers between 1 and 100 .
c. A program that displays only odd natural numbers less than or equal to 20.
d. A program that computes the sum of all integers between 1 and N where N is an integer
greater than 1 and given from the user at the time of execution.
e. A program that computes N!( Factorial of N= N*(n-1)*(n-2)……3*2*1)
f. A program that accepts n integers from user and calculate
i. Their sum
ii. Their average

2.4 Translating algorithms to programming languages

The algorithm described and depicted by both pseudo code and flowchart is independent of any
programming language. This means that once you design the algorithm you can convert it to
any programming language you like and know. The process of converting the algorithm to a
programming language of your choice only requires you to translate all blocks of the flow
charts into their equivalent syntactical forms of the programming language. As you have seen so
far in the examples above the constructs of an algorithm are input, output, processing, decisions
and loops. All programming languages provide you with programming statements to express
these constructs in to the required programming language format. For instance, C++have the
following statements

 cin>>, input statement


 cout<< , output statements
 Arithmetic, Assignment, logical and Boolean operators for processing
 if…. else, switch, etc. are conditional statements
 for ( ; ;), do ..while, and while are loop statement etc

These statements enable you to easily convert the algorithm expressed in flow chart into c++
programming code. Starting from chapter three of these module you will learn the syntax of
these statements so that you can be able to write C++ programs.

Hope University College 9

You might also like