COM 113 Lecture 2

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

FEDERAL COLLEGE OF AGRICULTURAL PRODUCE

TECHNOLOGY, KANO
DEPARTMENT OF COMPUTER SCIENCE

COURSE TITLE:
INTRODUCTION TO PROGRAMMING
COURSE CODE: COM113 LECTURE 2

UNIT :- 4 CREDIT UNIT


CONTACT: anasmsani22@fcapt.edu.ng

1 1
Lecture Overview

• Examples of flowchart
• Uses of flowchart
• Advantages and dis-advantages of flowchart

1 2
INTRODUCTION TO PROGRAMMING

• EXAMPLE 1
• Write an algorithm and draw a flowchart to convert the
length in feet to centimeter.
• SOLUTION (Pseudo code)
• Step 1: START
• Step 2: Input LFT
• Step 3: LCM = LFT * 30
• Step 4: Print LCM
• Step 5: STOP

1 3
INTRODUCTION TO PROGRAMMING

• flowchart

1 4
INTRODUCTION TO PROGRAMMING

• USES OF FLOWCHARTS
• 1 It gives us an opportunity to see the entire system as a
whole.
• 2 It makes us to examine all possible logical outcomes in
any process.
• 3 It provides a tool for communicating i.e. a flowchart
helps to explain the system to others.
• 4 To provide insight into alternative solutions.
• 5 It allows us to see what will happen if we change the
values of the variable in the system.
1 5
INTRODUCTION TO PROGRAMMING
• ADVANTAGES OF USING FLOWCHART
• 1. Flowcharts are visual aids for communicating the logic of a
system to all concerned.
• 2. Flowcharts are a means of documentation
• 3. Changes to the procedure are more easily catered for
(modification).
• 4. Flowchart can be understood by new staff coming to the
company
• 5. Flowcharts help to clarify the logic of a system i.e the overall
picture of the organization can be seen
• 6. A flowchart is a consistent system of recording. It brightens the
relationships between different parts of a system
1 6
INTRODUCTION TO PROGRAMMING

• DISADVANTAGES OF USING FLOWCHART


• 1. Where the logic of a problem is complex, the flowchart
quickly becomes clustered and lacks clarity.
• 2. If alterations are required the flowchart may require
redrawing completely.
• 3. As the flowchart symbols cannot be typed,
reproduction of flowchart is often a problem.

1 7
INTRODUCTION TO PROGRAMMING
• Example 2 Draw a flowchart to find the average of four numbers
stored in variables A, B, C, and D.

1 8
INTRODUCTION TO PROGRAMMING
• Example 3 Draw a flowchart to find the average of four numbers
stored in variables A, B, C, D, when the value of A is Zero no
average is to be done.

1 9
INTRODUCTION TO PROGRAMMING
• Example 4:Draw a flowchart that accept the age of an applicant and
determine if he eligible to vote or not, applicants are only eligible if their age is
18 years and above.

1 10
INTRODUCTION TO PROGRAMMING
• Example 5: Draw a flowchart that gives a customer a bonus of N50 when the
recharge amount is N200 and above, the customer doesn’t get any bonus if
amount recharge is below N200.

1 11
INTRODUCTION TO PROGRAMMING
• HIEREARCHY CHARTS
• A hierarchy chart is a diagram that graphically depicts the structure
of a program.
• It has boxes that represent each step in the program. The boxes are
connected in a way that illustrates their relationship to one
another. Figure below shows a hierarchy chart for the pay-
calculating program.
• A hierarchy chart begins with the overall task and then refines it
into smaller subtasks.

1 12
INTRODUCTION TO PROGRAMMING

1 13
INTRODUCTION TO PROGRAMMING
• EXAMPLE :Develop an algorithm using hierarchy chart to find the
average of four numbers stored in variables A, B, C and D.
• Solution:

1 14
INTRODUCTION TO PROGRAMMING
• EXAMPLE :Develop an algorithm using hierarchy chart to find the
average of four numbers stored in variables A, B, C and D.
• Solution:

1 15
INTRODUCTION TO PROGRAMMING
• EXAMPLE :Develop an algorithm using hierarchy chart to find the
average of four numbers stored in variables A, B, C and D.
• Solution:

1 16
INTRODUCTION TO PROGRAMMING
• BASIC CODING STRUCTURES
• All computer programs can be coded using three logic structures (or
programs) or combinations of these structures:
• 1. Simple sequence 2. Selection 3. Repetition
• The three structures are useful in a disciplined approach to
programming because:
• 1. The program is simplified. Only the three building blocks are used,
and there is a single point of entry into the structure and a single
point of exit.
• 2. The three coding structures allow a program to be read from top
to bottom making the logic of the program more visible for checking
and for maintenance.
1 17
INTRODUCTION TO PROGRAMMING
• SIMPLE SEQUENCE
• The simple-Sequence structure consists of one action followed by
another. In other words, the flow of control is first to perform
operation A and then to perform operation B. A simple sequence is
flowcharted as two process symbols connected by a flow line.
• In a computer program or an algorithm, sequence involves simple
steps which are to be executed one after the other. The steps are
executed in the same order in which they are written.
• PROCESS 1
PROCESS 2
………………………………..

• PROCESS N

1 18
INTRODUCTION TO PROGRAMMING
• In a flowchart, sequence is expressed as:

• (The arrowheads are optional if the flow is top-to-bottom.)

1 19
INTRODUCTION TO PROGRAMMING
• The selection structure consists of a test for condition followed by
two alternative paths for the program to follow.
• The program selects one of the program control paths depending
on the test of the condition.
• After performing one of two paths, the program control returns to
a single point.
• This pattern can be termed IF . ELSE because the logic can be
stated (for condition P and operations C and D): IF P (is true),
perform C; ELSE perform D.
• There are three (3) types of selection structures viz: single
alternative, double alternative, multiple alternative.

1 20
INTRODUCTION TO PROGRAMMING
• (a) Single alternative – This structure has the form:
• [IF condition, Then:]

• [End of IF structure]

• 1. IF condition THEN
• Process 1
• ENDIF

1 21
INTRODUCTION TO PROGRAMMING
• (b) Double alternative: This structure has the form
• i.e IF condition holds, then Process 1 executed; otherwise process
2 is executed.

1 22
INTRODUCTION TO PROGRAMMING
• (c) Multiple alternatives: This structure has the form:
IF Condition (1), then
[PROCESS A,]
Else if condition (2), then:
[PROCESS B]

1 23
INTRODUCTION TO PROGRAMMING
• ITERATION LOGIC (REPETITIVE FLOW)
• Repetition allows for a portion of an algorithm or computer
program to be done any number of times dependent on some
condition being met.
• An occurrence of repetition is usually known as a loop.
• An essential feature of repetition is that, each loop has a
termination condition to stop the repetition, or the obvious
outcome is that the loop never completes execution (an infinite
loop).
• The termination condition can be checked or tested at the
beginning or end of the loop, and is known as a pre-test or post-
test respectively.
1 24
INTRODUCTION TO PROGRAMMING
• REPETITION: PRE-TEST
• A pre-tested loop is so named because the condition has to be met
at the very beginning of the loop or the body of the loop is not
executed.
• WHILE condition is true
• Process (es)
• ENDWHILE

1 25
INTRODUCTION TO PROGRAMMING
• A post-tested loop executes the body of the loop before testing the
termination condition. This construct is often referred to as an
unguarded loop.
• REPEAT In a flowchart, post-test is expressed
as:
• Process
• UNTIL condition is true

1 26
INTRODUCTION TO PROGRAMMING
• For example let us take 10 sets of numbers each set containing three. The
problem is to get the biggest number in each set and print it.
• Algorithm
• Step 1: Read the total number of sets
• Step 2: Initialize the number of the set as N=1
• Step 3: Read three numbers of a set say A, B, C.
• Step 4: Compare A with B and choose the bigger.
• Step 5: Compare the bigger number with C and choose the biggest
• Step 6: Print the biggest number,
• Step 7: Increment the number of the set by 1
• (N=N+1)
• Step 8: Check whether we have exceeded 10. If not Go – To step 3. Otherwise
• Step 9: STOP
1 27
INTRODUCTION TO PROGRAMMING

• MODULAR PROGRAMMING: Modular Programming is the


process of breaking down a complex algorithm or program into
smaller parts or sub-programs.
• The goal of modular programming is to break up the program into
small parts that are more easily understood.
• The planning, coding and testing can be done on these small,
relatively simple units, rather than on one large, complex body of
code.
• MODULAR PROGRAM PLANNING
• There are several popular methods used for planning modular
programs. Pseudo code or flowcharts maybe used, with slight
modification for the subroutine, or hierarchy charts may be used.
1 28
INTRODUCTION TO PROGRAMMING
• PROGRAM EXAMPLE
• Using modular (subroutines) approach shows the program plan of
a computer program for calculating simple interest on a deposit by
a customer.
• SOLUTION Modular Pseudo code plan for the problem
• 1. Input data
• 1.1 Prompt and input rate, deposit amount, and number of years.
• 2. Calculations
• 2.1 Calculate interest = deposit * rate * years
• 2.2 Calculate ending balance = deposit + interest
• 3. Output
• 3.1 Print interest and ending balance

1 29
INTRODUCTION TO PROGRAMMING
• MODULAR FLOWCHART PLAN FOR THE PROBLEM
Start Input Data Calculations

Enter Rate Calculate


Input Data
Interest=Deposit
*Rate*Year

Enter Deposit
Calculations Calculate
Balance=Deposit
+Interest

Enter Year
Write output

Return

Stop Return
1 30
INTRODUCTION TO PROGRAMMING
• MODULAR FLOWCHART PLAN FOR THE PROBLEM

1 31
INTRODUCTION TO PROGRAMMING
• MODULAR HIERARCHY PLAN FOR THE PROBLEM

1 32

You might also like