You are on page 1of 44

INTRODUCTION TO C

LANGUAGE

MODULE II
ALGORITHM AND FLOWCHART

LEARNING OBJECTIVES
At the end of this chapter, the student should
be able to: INTRODUCTION

 learn the different tools used in problem After an overview of


solving programming, this chapter
 identify flowchart symbols covers algorithm and
 formulate algorithm for certain problems flowcharting concepts. The
 construct flowchart for certain problems chapter begins with a discussion
 formulate correct program logic using of algorithm and the different
algorithm and flowchart methods of specifying algorithm.
 learn the techniques in solving business
The student will learn how to
and scientific application problems by
employing flowcharting methods and formulate correct program logic
algorithms using different methods of
algorithm.

The chapter then covers


LESSO TOPIC
flowchart, flowchart symbols,
N guidelines, and basic control
1 Algorithm structures. Application problems
employing algorithms and
2 Flowchart flowchart methods are provided.

1|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

LESSON 1
ALGORITHM

A. Algorithm Defined

It is a set of instructions of how to carry out a process. An algorithm lists


the steps that must be followed to complete the process and to reach the
solution.
A finite set of an instruction that specifies a sequence of operation to be
carried out in order to solve a specific problem.
An unambiguous procedure specifying a finite number of steps to be
taken.

B. Properties of Algorithm

1. Finiteness - there is an exact number of steps to be taken and has an end.

2. Absence of Ambiguity - means that every instruction is precisely described and


clearly specified.

3. Sequence of Execution – instructions are performed from top to bottom.

4. Input and Output - defined the unknowns of the problem is specified and with the
expected outcome.

5. Effectiveness- the solution prescribed is guaranteed to give a correct answer and


that the specified process is faithfully carried out.

6. Scope Definition - applies to a specific problem or class of problem.

2|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

C. Methods of Specifying Algorithm

1. Pseudocode

Specifies the steps of algorithm using essentially natural language of


superimposed control structure.
Pseudo means “pretend” or “false” Pseudo Code is pretend or false
computer code; generic English-like terms that are somewhat like
computer code.
It is not as standardized as flowcharts, and does not facilitate the breaking
down of problems as well as a flowchart does.

2. Flowchart - a traditional graphical tool with standardized symbols. Show the


sequence of steps in an algorithm.

D. Examples

1. Write an algorithm to determine a student’s final grade and indicate whether it is


passing or failing. The final grade is calculated as the average of four marks.

Pseudocode:

Input a set of 4 marks

Calculate their average by summing and dividing by 4

if average is below 75

Print “FAILED”

else

Print “PASSED”

3|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Detailed Algorithm

Step 1: Input M1,M2,M3,M4

Step 2: GRADE = (M1+M2+M3+M4)/4

Step 3: if (GRADE < 50) then

Print “FAILED”

else

Print “PASSED”

endif

2. Write an algorithm to convert the length in feet to centimeter.

Pseudocode:

Input the length in feet (Lft)

Calculate the length in cm (Lcm) by multiplying LFT with 30

Print length in cm (LCM)

Detailed Algorithm

Step 1: Input Lft

Step 2: Lcm = Lft x 30

Step 3: Print Lcm

4|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

CONCEPT REVIEW

I. Fill in the blanks Fill in the blanks with the correct answer.

1. _____________ specifies the steps of algorithm using essentially natural


language of superimposed control structure.

2._______________ is a finite set of an instruction that specifies a sequence of


operation to be carried out in order to solve a specific problem.

3. Pseudo” means ____________.

4. ______________ is a traditional graphical tool with standardized symbols.

II. Enumeration Enumerate the following:

A. Properties of Algorithm

1.

2.

3.

4.

5.

6.

B. Methods of Specifying Algorithm

1.

2.

5|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

1. Write an algorithm that will compute the average of five input quizzes, and then
display the result.

2. Write an algorithm that displays 3 inputted numbers in descending order.

6|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

3. Write an algorithm to find and print the largest of three numbers.

4. Write an algorithm that will prompt for and receive 18 examination scores from a
mathematics test, compute the class average, and display all the scores and the
class average.

7|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

LESSON 2
FLOWCHART

A. Flowchart Defined

It is a visual representation of an algorithm


It uses easy-to-understand symbols to represent actions on data and the
flow of data
It consists of a sequence of instructions linked together by arrows to show
the order in which the instructions must be carried out.
A flowchart is basically the plan to be followed when the program is
written. It acts like a road map for a programmer and guides him in
proceeding from the starting point to the final point while writing a
computer program.
The process of drawing a flowchart for an algorithm is referred to as
flowcharting.

B. Flowchart Symbols

Only a few symbols are needed to indicate the necessary operations in a flowchart.
These symbols have been standardized by the American National Standards
Institute (ANSI).

1. Processing

A processing symbol is used in a flowchart to represent arithmetic and data


movement instructions. Thus, all arithmetic processes such as adding, subtracting,
multiplying and dividing are shown by a processing symbol. The logical process of
moving data from one location of the main memory to another is also denoted by this
symbol.

8|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

2. Flow lines

Flow lines with arrowheads are used to indicate the flow of operation, that is, the
exact sequence in which the instructions are to be executed. The normal flow of
flowchart is from top to bottom and left to right. Arrowheads are required only when
the normal top to bottom flow is not to be followed.

3. Decision

The decision symbol is used in a flowchart to indicate a point at which a decision has
to be made and a branch to one of two or more alternative points is possible.

4. Connector

If a flowchart becomes very long, the flow lines start crisscrossing at many places
that causes confusion and reduces the clarity of the flowchart. Moreover, there are
instances when a flowchart becomes too long to fit in a single page and the use of
flow lines becomes impossible. Thus, whenever a flowchart becomes too complex
that the number and direction of flow lines is confusing or it spreads over more than
one page, it is useful to utilize the connector symbol as a substitute for flow lines.
This symbol represents an entry from, or an exit to another part of the flowchart. A
connector symbol is represented by a circle and a letter or digit is placed within the
circle to indicate the link. A pair of identically labeled connector symbols is
commonly used to indicate a continued flow when the use of a line is confusing.

9|Page
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Name Symbols Representation

Terminator (terminal) Start or end of the


program

Preparation Signifies the preparation


of data

Data (input/output) Input or Output operation

Process Computational steps or


processing function of a
program

Decision Decision making and


branching

On-page Connector Connector or joining of


two parts of a program

Off-page Connector Designates entry or exit


from one page

Indicates the flow of


Flow lines operation

10 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

C. Flowcharting Guidelines

 The following are some guidelines in flowcharting:

a. In drawing a proper flowchart, all necessary requirements should be listed out


in logical order.
b. The flowchart should be clear, neat and easy to follow. There should not be
any room for ambiguity in understanding the flowchart.
c. The usual direction of the flow of a procedure or system is from left to right or
top to bottom.
d. Only one flow line should come out from a process symbol.

  or       

e. Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.

             

f. Only one flow line is used in conjunction with terminal symbol.

                

h. If the flowchart becomes complex, it is better to use connector symbols to


reduce the number of flow lines. Avoid the intersection of flow lines if you
want to make it more effective and better way of communication.
i. Ensure that the flowchart has a logical start and finish.
11 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

j. It is useful to test the validity of the flowchart by passing through it with a


simple test data.

D. Advantages and Limitations of using Flowcharts

The benefits of flowcharts are as follows:

 Communication: Flowcharts are better way of communicating the logic of a


system to all concerned.

 Effective analysis: With the help of flowchart, problem can be analyzed in


more effective way.

 Proper documentation: Program flowcharts serve as a good program


documentation, which is needed for various purposes.

 Efficient Coding: The flowcharts act as a guide or blueprint during the


systems analysis and program development phase.

 Proper Debugging: The flowchart helps in debugging process.

 Efficient Program Maintenance: The maintenance of operating program


becomes easy with the help of flowchart. It helps the programmer to put
efforts more efficiently on that part

The limitations of flowcharts are as follows:

 Complex logic: Sometimes, the program logic is quite complicated. In that


case, flowchart becomes complex and clumsy.

 Alterations and Modifications: If alterations are required the flowchart may


require re-drawing completely.

12 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

 Reproduction: As the flowchart symbols cannot be typed, reproduction of


flowchart becomes a problem.

 The essentials of what is done can easily be lost in the technical details of
how it is done.

E. Basic Control Structures

1. Sequence – process is executed from one to another in a straightforward


manner.

Examples:

1. Draw a flowchart that will accept and display a number. Write its equivalent
pseudocode.

13 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

BEGIN
Algorithm

2. Draw a flowchartRead A will compute and display the sum and product of two
that
numbers. Write its equivalent pseudocode. Step 1. Read in the value of A

BEGIN Algorithm
Step 2. Print the value of A.
Print A

S=0
P=0 Step 1. Initialize sum(S) and
END
product(P) into 0.
P=0
Step 2. Read in the values of A
Read A, B
and B.

Step 3. Compute sum(S) by adding


S = A+B
A and B then compute
P= A*B product(P) by multiplying A
and B.
P= A*B
Print S, P Step 4. Print the value of S and P.

END

14 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

3. Draw a flowchart that will read the two sides of a rectangle calculate and print its
area. Write its equivalent pseudocode. Formula: Area = Length x Width.

BEGIN Algorithm

Step 1. Initialize Area(A) into 0.


A=0
Step 2. Input the width(W) and
length(L) of a rectangle.

Input Step 3. Calculate the Area(A) by


W, L multiplying W and L.

Step 4. Print the value of A.

A=LxW

Print
END
A

END

15 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

2. Selection - Once the condition is evaluated, the control flows into one of two
paths. Once the conditional execution is finished, the flows rejoin before leaving the
structure. As an alternative, the "Question" could be replaced with a Boolean
expression and the branches labelled "TRUE" and "FALSE".

Operators Commonly Used in Flowcharting

A. Arithmetic Operators

Operators Meaning

+ Addition

- Subtraction

* Multiplication

/ Division

16 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

B. Relational Operators

Operators Meaning

= Equal

> Greater than

< Less than

<> Not equal

> Greater than or Equal to

< Less than or Equal to

C. Logical Operators

Operator Meaning

&& AND

II OR

! NOT

Examples:

1. Draw a flowchart that will input values for x and y. Compare two values inputted
and print which of the values is higher including the remark “Higher”. Write its
equivalent pseudocode.

Algorithm

Step 1. Read in the values of x and y.

Step 2. Test if x is greater than y.

Step 3. IfWork
x is greater than y, x is higher. However, if x is 17 | P a g e
Instructional Book
less than y, y is higher.
INTRODUCTION TO C
LANGUAGE

2. Draw a flowchart that will determine the student’s final grade and indicate weather
BEGIN
it is passing or failing. The final grade is calculated as the average of four marks.

Input
Algorithm x, y

Step 1. Initialize Grade into 0.


x>y T
Step 2. Input a set of 4 marks (M1, M2,
x>y M3,x,M4).
Print “Higher”

Step 3. Calculate their average(Grade) by summing and


dividing by
F 4.
Step 4. If average(Grade) is below 75
Print y, “Higher” END
Print “FAIL”

else 18 | P a g e
Instructional Work Book
Print “PASS”
INTRODUCTION TO C
LANGUAGE

BEGIN

Grade=0

Input
M1,M2,M3,M4

Grade=(M1+M2+M3+M4)/4

F IS GRADE<75 T

Print Print
“Passed” ”Failed”

3. JLB Manufacturing CompanyEND plans to give a year-end bonus to each of its


employee. Draw a flowchart will compute the bonus of an employee. Consider
the following conditions: If the employee’s monthly salary is less than 10,000
pesos, the bonus is 25% of the salary. If the employee’s monthly salary is greater
than 10,000 pesos, the bonus is 3,000 pesos. Print the name and the
corresponding bonus for each employee. Write the equivalent pseudocode.

19 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Algorithm

Step 1. Initialize bonus to 0.

Step 2. Read in employee’s name and salary.

Step 3. Test if employee’s salary is less than 10,000.

Step 4. If salary < 10,000 then

Bonus = salary * 25 %

Else

Bonus = 3,000

Step 5. Print the employee’s name and bonus.

BEGIN

bonus=0

Read Name,
Salary

Salary F
< 10,000 Bonus = 3,000

T
Bonus = 0.50*Salary

Print Name,
Bonus

END

20 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

4. Construct a flowchart that will accept the evaluation score of a faculty and
determine its equivalent remarks. Print the name of the faculty and the remarks
obtained. Write the equivalent pseudocode. Remarks are based on the following
criteria:

4.50 – 5.00 Outstanding

4.00 – 4.49 Very Satisfactory

3.50 – 3.99 Satisfactory

3.00 – 3.49 Needs Improvement

2.99 – below Poor

Algorithm

Step 1. Initialize Rem into space or blanks.

Step 2. Read in the values of Name and Score.

Step 3. Test the score if it is greater than or equal to 4.50.

Step 4. If the score is greater than or equal to 4.50, Rem is “Outstanding”.


However, if the score is less than 4.50, do step 5.

Step 5. Test the score if it is greater than or equal to 4.00.

Step 6. If the score is greater than or equal to 4.00, Rem is “Very Satisfactory”.
However, if the score is less than 4.00, do step 7.

Step 7. Test the score if it is greater than or equal to 3.50.

Step 8. If the score is greater than or equal to 3.50, Rem is “Satisfactory”.


However, if the score is less than 3.50, do step 9.

Step 9. Test the score if it is greater than or equal to 3.00.

Step 10. If the score is greater than or equal to 3.00, Rem is “Needs
Improvement”. However, if the score is less than 3.00, Rem is “Poor”.

Step 11. Print the name and Rem.

21 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

BEGIN

Rem = “ “

Read Name,
Score

Score T
> 4.50 Rem=”Outstanding” A

Score T
> 4.00 Rem=”Very Satisfactory” A

Score T
> 3.50 Rem=”Satisfactory” A

22 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Score T
> 3.00 Rem=”Needs Improvement” A

F
Rem=”Poor”

A
Print Name,
Rem

END

3. Repetition - Computers are particularly well suited to applications in which


operations are repeated many times. If the same task is repeated over and over
again a loop can be used to reduce program size and complexity.

23 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Loops and Counter

Looping

Used when it is desired to make the same calculation of more than one set of
data. It consists of repeating a program, or a section of program and substituting
new data for each repetition.

Counter

Is a setup in a program loop to keep track of a number of times the program


segment is repeated. The program can then be terminated after the completion
of a predetermined number of passes.

Steps in Loop Control

Initialization

The value of counter is initially set equal to zero or one.

24 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Test for limit condition

Before the logic flow gets out of a loop, a loop terminating condition must first
be satisfied.

Incrementation

Often each loop is executed, 1 is added to the counter. Thus counter reflects
the number of times the operation has been performed.

Examples:

1. Construct a flowchart that will count from 1 to 10 and print each number counted
using the looping structure. Write the equivalent pseudocode.

Algorithm

Step 1. Initialize the value of N to 0.

Step 2. Test if N is less than 10.

Step 3. If N is less than 10, add 1 to the value of N, print the value then
go back to step 2. However, if N is greater than 10, stop
processing.
25 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

BEGIN
2. The initial value of the radius of a circle is equal to 1 unit and each succeeding
radius is 1 unit greater than the value before it. Draw a flowchart to compute the
area of a circle starting with radius=1 up to radius = 5, then print each radius and the
corresponding area of a circle. N=0

Formula: area = pi x r2

N<10 F
END

Algorithm T

Step 1. Initialize the value of radius(r)


N=N to
+11 and the value of pi to 3.1416.

Step 2. Compute the area by multiplying pi to the square of r.

Step 3. Print theWork of r and thePrint


value Book area.N 26 | P a g e
Instructional
Step 4. Increment the value of r by 1.
INTRODUCTION TO C
LANGUAGE

BEGIN

Pi=3.1416
r=1

area=pi * r *r

Print r, area

r=r+1

F
T
r<5 END

3. Write an algorithm and draw a flowchart to calculate 2 4 using a loop approach?

27 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Algorithm
Step 1:Base = 2, Power = 4, Counter =1, Product=2
Step 2:While Counter < Power
Repeat Steps 2- 5
Step 3: Product = Product * Base
Step 4: Counter = Counter +1
Step 5: Print Product

BEGIN

Base=2
Power=4
Product=2
Counter=1

Is Counter < Power N

Y
Print
Product =Product * Base Product

Counter= Counter + 1
END

NAME: ______________________________ SCORE: ________

28 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

Course/Year/Section : _____________ ____ Date : __________

CONCEPT REVIEW

I. Fill in the blanks. Fill in the blanks with the correct answer.

1. Flowchart is a visual representation of a(n) _______________________.


2. ______________________ structure provides process execution first before
the decision is tested.
3. Only one _________________ should come out from a process symbol.
4. ___________________ is a finite set of an instruction that specifies a
sequence of operation to be carried out in order to solve a specific problem.
5. _____________ signifies the preparation of data.

II. Identify the following symbols.

1.

2.

3.

4.

5.

29 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

1. Draw a flowchart that computes the average of three input quizzes, and then
display the result.

30 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

2. Draw a flowchart that converts the input Fahrenheit degree into its Celsius degree
equivalent. Use the formula: C= (5/9)*F-32. Print the computed value of Celsius.

31 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

32 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

4. Draw a flowchart that converts the input Celsius degree into its equivalent
Fahrenheit degree. Use the formula: F= (9/5) * C+32. Print the computed value of
Fahrenheit.

33 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

5. Draw a flowchart that will compute for the midterm grade of a student. The
midterm grade is equal to one-third of the minor exam and two-thirds of the midterm
exam. Print the midterm grade.

34 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

6. Read two records in a computer. The first record will contain unit price and the
second record will contain quantity. Draw a flowchart that will compute and display
the amount by multiplying unit price and quantity.

35 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

7. Draw a flowchart that displays 3 inputted numbers in ascending order.

36 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

8. Draw a flowchart that will determine and display the largest number among 3
numbers being inputted.

37 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

9. Construct a flowchart that asks for the amount purchased by the customer. If the
customer purchases more than 2,000 then a 5% discount is given. Display the net
amount to be paid by the customer.

38 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

10. Draw a flowchart that will generate the sum and product of 20 input numbers.

39 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

11. Draw a flowchart that will input 10 integers. Output the number of negative
values and the sum of positive numbers.

40 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

12. Draw a flowchart that accepts a number in kilowatts then display the equivalent
number in watts. Hint: 1 watt = 0.0001 kilowatt.

41 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

13. Given two numbers A and B, draw a flowchart to determine the difference
between A and B and the value of X. If A – B is negative, X= A + B; if A – B is zero,
X = 2A + 2B; and if A – B is positive, X = A * B. Print the values of A, B and X.

42 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

NAME: ______________________________ SCORE: ________


Course/Year/Section : _____________ ____ Date : __________

SKILLS REVIEW

14. Write an algorithm and draw a flowchart to a) read an employee name (NAME),
overtime hours worked (OVERTIME), hours absent (ABSENT) and b) determine the
bonus payment (PAYMENT). Display the employee name and bonus payment.

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
>40 hours $50

>30 but  40 hours $40

>20 but  30 hours $30

>10 but  20 hours $20

 10 hours $10

43 | P a g e
Instructional Work Book
INTRODUCTION TO C
LANGUAGE

44 | P a g e
Instructional Work Book

You might also like