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

Chapter 4

COMP 1601

Uploaded by

Shania Schultz
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)
7 views9 pages

Chapter 4

COMP 1601

Uploaded by

Shania Schultz
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
You are on page 1/ 9

4.

Flow Charts

It is useful to be able to visualize the flow of logic in a computer program. A diagramming tool known as
a flow chart is often used for this purpose. A flow chart uses a small set of symbols to show how each
logical step in a program leads to another logical step, from the starting point to the end, until the goals
of the program are achieved. In this chapter, we discuss the basic symbols that are used in a flow chart
and show they can be put together to derive the logic for a program. We will do this by constructing a
flow chart for the carpet program which was discussed in the previous chapter.

4.1 Flow Chart Symbols

A flow chart is a diagram which shows the flow of logic through a computer program. In this chapter, we
will use a small set of symbols to draw a flow chart. These are shown in Table 4.1.

1
Symbol Name Description

Start / End An oval is used to indicate the start or end


point of a program.

Arrow A line with an arrow is used to show the


flow of logic from one part of the program
to another.

Input / Output A parallelogram is used to indicate input


or output of the program (e.g., cin or
cout statements).

Process A rectangle represents a processing step


(e.g., assigning the value of an expression
to a variable).

Decision A diamond indicates a decision being


made.

Table 4.1: Flow Chart Symbols

4.2 Flow Chart for Calculating Amount of Carpet Required

In the previous chapter, we looked at a program which inputs the dimensions of a rectangular room and
calculates the amount of carpet required to cover the room. The flow of logic through this program can
be illustrated in the flow chart shown in Figure 4.1.

2
Start

input
width, length

area =
width x length

output
width, length,
area

End

Figure 4.1: Flow Chart for Carpeting Program

The logic of the program is contained between the “Start” and “End” symbols. The program first obtains
the values of the width and length of the room, which is input by the user via the keyboard. An
“Input/Output” symbol is used to depict this step. (Note that two or more input/output statements can
be combined in one “Input/Output” symbol to simplify the flow chart.) The area of the carpet is then
calculated; this is shown in the flow chart using the “Process” symbol. Finally, the width, length, and area
values are displayed on the monitor using an “Input/Output” symbol. As can be observed from Figure 4.1,
all the symbols are connected by arrows which show the sequence of programming steps required to
solve the given problem.

4.3 Flow Chart for Calculating Cost of Carpet

Suppose we wish to include an additional feature in the carpet program—we would like to calculate the
cost of the carpet required, given the price of the carpet. Three changes will have to be made to the
program and the corresponding flow chart:

3
(1) An input step will be required to obtain the price of the carpet from the user. Assume this is stored in
a floating point variable, priceCarpet. The value for this variable will be input using a cin statement:

cin >> priceCarpet;

A new “Input/Output” symbol for inputting the value of the price of the carpet from the user has to
be added to the flow chart.

(2) A new processing step will be needed to calculate the cost of the carpet, based on the amount of
carpet required and the price of the carpet. The cost can be calculated as follows:

cost = area * priceCarpet;

A new process symbol must be added to the flow chart to depict the calculation, after the area has
been calculated.

(3) An output step will be required to display the cost of the carpet. An “Input/Output” symbol to depict
the output of the cost of the carpet must be added to the flow chart.

Each of these three steps must be inserted at the appropriate place in the flow chart shown in Figure 4.1.
The revised flow chart is shown in Figure 4.2, with the new steps shaded.

4
Start

input
width, length

input
priceCarpet

area =
width x length

cost = area x
priceCarpet

output
width, length,
area

output
cost

End

Figure 4.2: Flow Chart for Calculating Cost of Carpet

5
The flow chart for calculating the cost of the carpet required to a cover a room consists of several symbols
connected via lines with arrows. The program is strictly sequential from the “Start” symbol to the “End”
symbol. This means that the programming steps are executed one after the other, starting from the
“Start” symbol. There is no repetition of programming steps; so, once a step is executed, it will never be
executed again. In the next chapter, we will show how to repeat one or more steps in a flow chart.

The revised program to calculate the cost of the carpet is given below:

#include <iostream>

using namespace std;

int main () {

int width;
int length;
int area;
float priceCarpet;
float cost;

cout << "Please enter the width and length of the room: ";
cin >> width >> length;

cout << "Please enter the price of the carpet (per square metre): ";
cin >> priceCarpet;

area = width * length;


cost = area * priceCarpet;

cout << "Width of the room: " << width << endl;
cout << "Length of the room: " << length << endl;
cout << "Amount of carpet required: " << area << endl;
cout << "Cost of carpet: " << cost << endl;

return 0;
}

4.4 Mapping Flow Chart Symbols to Programming Statements

Drawing a flow chart is one of the first steps that should be followed before attempting to write the actual
program. After the flow chart is drawn, the source code statements must be written in order to achieve
the functionality shown in the flow chart. It is therefore important to understand how symbols in a flow
chart map to actual programming statements. Figure 4.3 shows this mapping for the carpet program
which calculates the cost of the carpet required to cover a room.

6
Start

input cout << "Please enter the width and length of the room: ";
width, length cin >> width >> length;

input cout << "Please enter the price of the carpet (per square metre): ";
priceCarpet cin >> priceCarpet;

area = area = width * length;


width x length

cost = area x
cost = area * priceCarpet;
priceCarpet

output cout << "Width of the room: " << width << endl;
width, length, cout << "Length of the room: " << length << endl;
area cout << "Amount of carpet required: " << area << endl;

output cout << "Cost of carpet: " << cost << endl;
cost

End

Figure 4.3: Mapping Flow Chart Symbols to Programming Statements

7
4.5 Exercises

1. For each of the following flow charts, write the corresponding C++ program.

Start Start

input input
num1, num2 num1, num2

square = sum =
num1 * num2 num1 + num2

avg =
output sum / 2
num, square

End output
sum, avg

End

Flowchart 1 Flow chart 2

8
2. A program is required which must input a temperature in Fahrenheit from the user at the keyboard
and display the equivalent temperature in Centigrade on the monitor. After studying the problem for
a while, you came up with the following steps:
Step 1: Ask the user to enter a temperature in Fahrenheit
Step 2: Input the temperature typed at the keyboard
Step 3: Subtract 32 from the temperature
Step 4: Multiply the result in Step 3 by 5
Step 5: Divide the result in Step 4 by 9
Step 6: Display the result in Step 5 to the user

a. Draw a flow chart which solves the problem, showing the six steps above.
b. Write a C++ program which achieves the functionality shown in the flowchart.

3. A program is required that inputs a measurement in feet and displays the equivalent measurement in
metres. For example, if the measurement is 5.3 feet, the program should display 1.60 m.

a. Draw a flow chart that shows the steps required to solve the problem.
b. Write a C++ program which achieves the functionality shown in the flowchart.

You might also like