0% found this document useful (0 votes)
11 views

Chap1 Introductiontocomputing

This document introduces algorithms and how to describe them using flowcharts and pseudocode, including examples of algorithms to calculate pay and averages, and algorithms that use branching and looping. It explains the basic components of algorithms, flowcharts, and pseudocode, provides examples of each, and gives an exercise to write a flowchart and pseudocode to find the largest of three numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chap1 Introductiontocomputing

This document introduces algorithms and how to describe them using flowcharts and pseudocode, including examples of algorithms to calculate pay and averages, and algorithms that use branching and looping. It explains the basic components of algorithms, flowcharts, and pseudocode, provides examples of each, and gives an exercise to write a flowchart and pseudocode to find the largest of three numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 1

INTRODUCTION TO ALGORITHMS
AND FLOWCHARTS

Introduction to computer science 1


Chapter 1

§ Algorithms
§ Using Flowchart to describe Algorithm
§ Algorithm in pseudo-code
§ Branching in Algorithms
§ Loop in Algorithms

2
1. Algorithms

n An algorithm is defined as a step-by-step sequence


of instructions that describes how the data are to be
processed to produce the desired outputs.
n In essence, an algorithm answers the question:
“What method will you use to solve the problem?”.
n You can describe an algorithm by using flowchart
symbols. By that way, you obtain a flowchart.
n Flow chart is an outline of the basic structure or
logic of the program.
n Another way to describe an algorithm is using
pseudocode.

3
2. Using Flowchart to describe algorithm

Terminal

Input/output

Process Connector

Flowlines
Predefined process

Decision

Fig 1. Flowchart symbols 4


Example 2.1. Calculate the payment of an employee

Fig. 2. Start

Input Name,
Hours, Rate

Calculate Note: Name, Hours


Pay ¬ Hours ´ Rate and Pay are variables
in the program.

Dislay
Name, Pay

End

5
Example 2.2: Calculate the average of 3 numbers

Fig. 3.

6
Example 2.3: How to compute profit and loss.

Fig. 4.

7
3. Algorithms in pseudo-code

n You also can use English-like phrases to describe an


algorithm. In this case, the description is called
pseudocode.

n Example: The pseudocode for the program with the


flowchart in Fig. 2.
Input the three values into the variables Name, Hours,
Rate. Start

Calculate Pay = Hours ´ Rate.


Input Name,
Hours, Rate

Display Name and Pay. Calculate


Pay ¬ Hours ´ Rate

Dislay
Name, Pay

8
End
4. Branching in algorithms

If test-condition then S If test-condition then S1


endif else S2
endif
Example 2.1

Pseudo-code of the
program:
Input A
if A > 0 then
calculate B = sqrt(A)
print B
else
Print “ A is negative”
endif

10
Branching
Example 4.2: Solving
the quadratic
equation:
ax2 + bx +c = 0

Pseudo-code of the program:


Input a, b, c
Calculate del = b2 + 4.a.c
if del >0 then
x1 = (-b + sqrt(del)/(2.a)
x2 = (-b - sqrt(del)/(2.a)
print x1, x2
else if del =0 then
x = -b/(2.a)
else
print “No solution”
endif

11
5. Loops on Algorithms

n Many problems require repetition capability, in which


the same calculation or sequence of instructions is
repeated, over and over, using different sets of data.
n Loop is a very important concept in programming.

n Example 5.1. Write a program to do the task: Print a


list of the numbers from 4 to 9, next to each number,
print the square of the number.

12
Example 5.1 Note:
NUM ¬ NUM + 1 means
Start
old value of NUM + 1
NUM ¬ 4 becomes new value of NUM.

SQNUM ¬ NUM2

The algorithm can be described


Print
NUM, SQNUM
in pseudocode as follows:
NUM ¬ 4
NUM ¬ NUM + 1 do

No
SQNUM¬ NUM2
NUM> 9?
Print NUM, SQNUM
Yes
NUM ¬ NUM + 1
STOP while (NUM <= 9)

13
Example 5.2
The algorithm sums
all the even numbers
between 1 and 20
inclusive and then
displays then sum.

Programming Fundamentals 14
Pseudo-code of Example 5.2
sum = 0
count = 1
do
if count is even then
sum = sum + count
endif
count = count + 1
while count <= 20
Display sum

15
Exercise

Give the flowchart of


the program which
finds the largest
among three different
numbers. Write the
pseudo-code for the
flowchart.

16
Ans.
Input a,b,c
if a > b then
if a > c then
print a
endif
else
if b > c then
print b
else
print c
endif
endif
17

You might also like