B3062/3/1
Introduction To The Concept and Basic Instruction Of Programming C++
UNIT 3
Introduction To The Concept And Basic
Instruction Of Programming C++
OBJECTIVES
General Objective : To introduce techniques used in program design.
Specific Objectives : At the end of the unit you will be able to :
state the techniques of program designing tools
briefly explain the techniques of designing program by
different designing tools
use algorithm and flowchart to develop computer program
B3062/3/2
Introduction To The Concept and Basic Instruction Of Programming C++
INPUT
A recipe provides good and simple example of a plan.
The ingredients and the amounts are determined by what
is to be baked. That is the output determines the input
and processing. The recipe or plan reduces the number of
mistakes you might make. Many programmers
particularly beginners frequently try to write without first
making a careful plan. Step-by step process will enable
you to use your time efficiently and help you design error
free program that produced the desired output
3.0 Introduction The Technique To Develope C++ Programming
The first step in writing instructions to carry out a task is to determine what the
output should be – that is, what the task should produce. The second step is to
identify the data, or input, necessary to obtain the output. The last step is to
determine how to process the input to obtain the desired output. The general process
of writing program is to analyze the problem, design and plan the solution, coding
B3062/3/3
Introduction To The Concept and Basic Instruction Of Programming C++
which is to translate the algorithm into a programming language and finally testing
and debugging.
In this unit we shall discuss the logical sequence of precise steps that solve the
problem known as algorithm and three popular programming tools or techniques
used to develop the logic plan: flowcharts, pseudo code and structure chart or input-
process-output chart.
3.0.1 Algorithm
The techniques of selecting program statements and their logical sequencing
which emphasize the translating of each step of program outline into one or
equivalent C++ instruction is known by algorithm. Every detail, including obvious
steps , should appear in algorithm.
You use algorithm everyday to make decisions or perform tasks. For example, a 30%
discount sales at the supermarket, you must be able to write program that calculate
and display the price of item after discount according to the price entered by
consumer.
i. Request price of item (Input)
ii. Value of discount = rate of discount * price (Process)
iii. Price after discount = price - value of discount (Process)
iv. Display of discount price (Output)
3.0.2 Flowcharts
Graphic representation of algorithm is known as flowchart which consist of
geometric symbols. These symbols are connected by line of arrows called flowlines
which indicate the direction of flow of processes or activities. The shape of the
symbol indicates the type of operation that is to occur. Flowchart should flow from
the top of the page to the bottom. The symbols used in flowchart are standardized.
B3062/3/4
Introduction To The Concept and Basic Instruction Of Programming C++
Symbol Name Meaning
Flowline Used to connect symbols
and indicate the flow of
logic
Used to represent the
Terminal beginning (start) or the end
(End) of a task
Used for input and output
Input/output operations, such as reading
and printing. The data to be
read or printed are describe
inside
Used for arithmetic and
Processing data-manipulation
operations. The instruction
are listed inside the symbol
Used for any logic or
Decision comparison operations.
Unlike the input/output and
processing symbols, which
have one entry and one exit
flowline, the decision
symbol has one entry and
two exit paths. The path
chosen depends on whether
the answer to a question is
“yes” or “no”
Used to join different
Connector flowline
Used to indicate that the
Offpage connector flow chart continues to a
second page
B3062/3/5
Introduction To The Concept and Basic Instruction Of Programming C++
Used to represent a group
Predefined Process of statements that perform
one processing task.
Used to provide additional
Annotation information about another
flowchart symbol
The main advantage of using a flowchart to plan a task is that it provides a pictorial
representation of the task, which makes the logic easier to follow. We can clearly see
each and every step and how it is connected to the next step. For example lets look at the
discount sales program.
Start
Read price input
value
Calculate discount value = price *
process
discount rate
Price of item after discount
= price - discount value process
Display price after discount output
End
Figure 3.2 Flowchart for the discount sale
(An Introduction To Programming Using Visual Basic 6.0- David I. Schneider),
B3062/3/6
Introduction To The Concept and Basic Instruction Of Programming C++
The major disadvantage with flowcharts is that when a program is very large, the
flowcharts may continue for many pages, making them difficult to follow and modify.
3.0.3 Pseudocode
Pseudocode is an abbreviated version of actual computer code. The
geometric symbols used in flowcharts are replaced by English-like statements that
outline the process. Pseudocode looks more like computer code than does flowchart.
It allows programmer to focus on the steps required to solve a problem rather than on
how to use computer language.
The programmer can describe the algorithm in computer language form
without being restricted by the rules of the computer language. When the
pseudocode is completed it can be easily translated into any computer language.
The following is pseudocode for the price of discount sales item problem:
Program: determine the price of discount item on sale.
Read price (Input)
Set the value of discount = rate of discount * price entered (Process)
price of discount item = price of item - value of discount (Process)
Display the price after discount (Output)
Pseudocode has several advantages. It is compact and probably will not
extend for many pages as flowcharts commonly do. Also the plan looks like the code
to be written and preferred by many programmers.
3.0.4 Structure Chart
Structure chart is also known as input-process-output or hierarchy chart
shows the overall program structure. It refers to planning diagrams similar to a
B3062/3/7
Introduction To The Concept and Basic Instruction Of Programming C++
company’s organization chart. Structure chart depicts the organization of program
but omit the specific processing logic. They describe what each part, or module, of
the program does and they show how the modules relate to each other. Each module
may be subdivided into succession of sub-modules that branch out under it. The
charts are read from top to bottom and left to right. Figures 3.3 shows an example of
a structure chart. This price of discount item problem was solved by a series of
instructions to read data, perform calculations, and display results. Each steps was in
sequence; that is we moved from one line to the next without skipping over any
lines.
The main benefit of structure charts is in the initial planning program. We
break down the major parts of a program so we can see what must be done in
general. From this point, we can then refine each module into more detailed plans
using flowcharts.
Price of discount item program
Calculate price
Read price Display discount price
discount value = price * rate of discount discount price = price – discount value
Figure 3.3: Structure Chart
(An Introduction To Programming Using Visual Basic 6.0- David I. Schneider),
B3062/3/8
Introduction To The Concept and Basic Instruction Of Programming C++
Activity 3A
TEST YOUR UNDERSTANDING BEFORE YOU CONTINUE WITH THE
NEXT INPUT…!
Question 3.1
Draw the standard flowcharts symbol for
i. input/output
ii. processing
iii. decision
Question 3.2
State the 3 most popular programming tools
Question 3.3
What is an algorithm?
B3062/3/9
Introduction To The Concept and Basic Instruction Of Programming C++
Feedback To Activity 3A
Question 3.1
i. input/output
ii. processing
iii. decision
Question 3.2
1. Pseudocode
2. flowchart
3. structure chart
Question 3A.3
Algorithm is the techniques of selecting program statements and their logical
sequencing which emphasize the translating of each step of program outline into one
or equivalent computer language instruction.
B3062/3/10
Introduction To The Concept and Basic Instruction Of Programming C++
INPUT
3.1 Using Algorithm and Flowchart in programming
Class average Algorithm
Problem: Calculate and report the grade-point average for a class.
Discussion: The average grade equals the sum of all grades divided by
number of students. We need a loop to read and then add (accumulate) the
grades for each student in the class. Inside the loop, we also need to total
(count) the number of students in the class.
Input: Students grades
Processing: Find the sum of the grades; count the number of students;
calculate average grades = sum of grades / number of students.
Output: Average grade
B3062/3/11
Introduction To The Concept and Basic Instruction Of Programming C++
3.1.2. Class Average Flowchart
Start
counter and
Initialize sum start at 0
counter and
sum to 0
Are
There more No
Data?
Yes
read next
Get next
grade
grade
add 1 to
Increment counter
counter
accumulate
Add grade sum of grades
to sum
find the average
Set average to
sum/counter
Display display the
average answer
End
B3062/3/12
Introduction To The Concept and Basic Instruction Of Programming C++
Activity 3B
TEST YOUR UNDERSTANDING BEFORE YOU CONTINUE WITH THE
NEXT INPUT…!
Question 3.1
You are to plan a program to sum two integer entered by user and display the result
of the summation by using the flowchart technique, from the following pseudocode;
1. Print text “ Enter first number “
2. Read first number
3. Print text “ Enter second number “
4. Read second number
5. Calculate sum of two number
6. Print the sum of two number
B3062/3/13
Introduction To The Concept and Basic Instruction Of Programming C++
Feedback To Activity 3B
Question 3.1
Start
Print text “ Enter
first number “
Read first number
Print text “ Enter
second number “
Calculate sum of
two number
Display the sum of two
number
End
B3062/3/14
Introduction To The Concept and Basic Instruction Of Programming C++
KEY FACTS
1. The algorithm should be tested at the flowchart stage before being coded
into a program.
2. Pseudocode, flowcharts and structure charts are universal problem
solving tools that can be used to construct program in any computer
language
3. Flowcharts are used to provide visualization of the flow of certain
programming tasks
B3062/3/15
Introduction To The Concept and Basic Instruction Of Programming C++
1.
SELF-ASSESSMENT
You are approaching success. Try the question in this self-assessment section and
check your answers with those given in the Feedback on Self-Assessment given on
the next page. If you face any problems, discuss it with your lecturer. Good luck.
Question 3-1
Every employee who earns more than RM 50,000.00 per annum is required to pay
2.5 % of their nett yearly income. Write a complete program algorithm which will
calculate and display all the employees income tax return. Also, show the flowchart
for the program.
B3062/3/16
Introduction To The Concept and Basic Instruction Of Programming C++
Feedback To Self-Assessment
Have you tried the question????? If “YES”, check your answer now:
Question 3-1
Problem: Calculate and display income tax return
Discussion: The program must read the yearly income entered by user and select
income more than RM 50,000.00 to be multiplied by 2.5 %. Income less than
RM50,000.00 will be multiplied by 0. The result will be displayed..
Input: Employee yearly income
Processing: Select the value of income; more than 50000.00 calculate the income
tax return = yearly income * 0.025. Less than 50000.00 calculate the income tax
return = yearly income * 0.00.
Output: Income tax return
B3062/3/17
Introduction To The Concept and Basic Instruction Of Programming C++
Start
Print text “ Enter
yearly income “
Read yearly income
More than Yes
No
50000.00?
Calculate income Calculate income
tax return = tax return = yearly
yearly income * 0 income * 0.025
Display income tax return
End
CONGRATULATIONS!!!!…..
May success be with you
always….