Professional Documents
Culture Documents
START
Display message
How many
hours did you
work?
What is a Flowchart?
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
rounded rectangles
parallelograms
a rectangle
Rounded
Rectangle
Display message
How many
hours did you
work?
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Rounded
Rectangle
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
Parallelogram
START
Terminal
Display message
How many
hours did you
work?
Terminals
Read Hours
represented by rounded
rectangles
indicate a starting or
ending point
Display message
How much do
you get paid per
hour?
Read PayRate
START
Multiply Hours
by PayRate.
Store result in
GrossPay.
END
Terminal
Display
GrossPay
END
Input/Output Operations
represented by
parallelograms
indicate an input or output
operation
Display message
How many
hours did you
work?
4
Display message
How many
hours did you
work?
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Read Hours
Display
GrossPay
END
Input/Output
Operation
START
Display message
How many
hours did you
work?
Read Hours
Screen Output
How many
hours did
you work?
Step 1: An
Output
Operation
Display message
How much do
you get paid per
hour?
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Variable Contents:
Hours: ?
PayRate: ?
GrossPay: ?
Display
GrossPay
END
START
Step 2: An Input
Operation
(User types 40)
Display message
How many
hours did you
work?
Read Hours
Display message
How much do
you get paid per
hour?
Read PayRate
Variable Contents:
Hours: 40
PayRate: ?
GrossPay: ?
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
START
Display message
How many
hours did you
work?
Processes
Read Hours
represented by rectangles
indicates a process such as a
mathematical computation or
variable assignment
Display message
How much do
you get paid per
hour?
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Process
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
Connectors
z
z
The A connector
indicates that the second
flowchart segment begins
where the first segment
ends.
START
END
A
Modules
z
Read Input.
Call calc_pay
function.
Display results.
END
Sequence Structure
z
z
10
Decision Structure
z
NO
YES
11
Decision Structure
z
C++ Code
YES
x < y?
if (x < y)
a = x * 2;
else
Calculate a
as x plus y.
12
Calculate a
as x times 2.
a = x + y;
Decision Structure
z
C++ Code
YES
x < y?
if (x < y)
a = x * 2;
Calculate a
as x times 2.
13
Repetition Structure
z
z
14
Repetition Structure
z
C++ Code
while (x < y)
x++;
YES
x < y?
Add 1 to x
15
16
Display x
Add 1 to x
YES
x < y?
C++ Code
Display x
do
{
Flowchart
Add 1 to x
YES
x < y?
17
Case Structure
z
CASE
years_employed
1
bonus = 100
18
bonus = 200
bonus = 400
Other
bonus = 800
Combining Structures
z
YES
x > min?
Display x is
outside the limits.
NO
YES
x < max?
Display x is
outside the limits.
19
Seldom do we have a
program worked the first
time it is written
Need debugging
To find out and correct
the errors in a program
May need many times of
iteration for a large program
To reduce the number of
iteration, need a structural
method for program
development.
20
Display x is
within limits.
Program size
21
int
int main()
main()
{{
::
::
::
::
::
::
}}
22
int
int main()
main()
{{
::
}}
int
int function1()
function1()
{{
::
}}
int
int function2()
function2()
{{
::
}}
23
For
For users
users
26
For
For developers
developers
27
For example
Totally meaningless comment!
// The main function
int main()
{
cout << "Hello World!\n";
// cout Hello World
return 0;
// return a number 0
}
28
See
See the
the lines
lines for
for comments
comments are
are more
more than
than the
the codes
codes
29
How to comment?
At the beginning of each program, the following
comments are often required:
Background information of the program
(Objectives, version no., development date,
developer name, etc.)
Usage (e.g. command line options)
Additional resource required (e.g. if any other
projects/files are required for compiling this
program, e.g. header files)
30
How to comment?
At the beginning of each function, the following
information is needed
Objective of this function
Other functions that it will call
Passed parameters
Return parameters
Global variables that have been made use of.
31
How to comment?
Inside a function, the following information is
needed
The use of every local variable
Explanation of any tricky part that other
people may have difficulty in understanding.
32
Case study
Developing a program step-by-step
33
Step
Step 00 Prepare
Prepare
aa flowchart
flowchart
Start
D
To better
visualize the
problem, we
may develop a
flowchart for it
Skeleton
Menu
34
Yes
User chooses a?
No
User chooses b?
Yes
B
No
User chooses q?
Yes
No
End
D
A
35
Step
Step 11 construct
construct the
the
skeleton
skeleton
36
// A stub
// Input parameter: Nil
// Return parameter: Just a constant
bool menu()
{
return true; //Loop 1 time
}
37
40
Exercise 4.1
By following the steps as described above, you are
requested to develop the skeleton of a personnel
database program. The program should be a console
application that repeatedly asks users to do one of
the following in the main menu:
a. Enter/modify record
b. Show all records
q. Quit
44
45
Debugging a Program
46
Compile-time Debugging
Compile-time errors can often be spotted out by
the compiler
The compiler is able to detect the approximate
error location, but cannot be exact
Also, the error messages generated are
sometimes irrelevant
When a series of error messages are found, always
try to debug them starting from the first one
47
Run-time Debugging
49
#include <iostream>
using namespace std;
int main()
{ int a, b, c;
cout << "Enter 3 numbers: \n";
cin >> a;
cout << a << endl; // Checkpoint
cin >> b;
cout << b << endl; // Checkpoint
cin >> c;
cout << c << endl; // Checkpoint
if (c = (a+b))
{
cout << a << endl << b
<< endl << c; // Checkpoint
cout << "c = a+b\n";
}
return 0;
}
Step 1:
Build the program of
interest
Click on the lines of
code where you would
like to set breakpoints
Right-click Breakpoint
Insert Breakpoint
53
Breakpoints:
Breakpoints:
The
Themoments
momentsthat
that
you
youwould
wouldlike
liketo
to
examine
examinethe
thestatus
statusof
of
the
theresource
resourceallocated
allocated
to
tothe
theproject
project
Step 2:
Build it successfully.
Start the debugger
by clicking Debug
Start Debugging
This
Thisline
lineof
ofcode
codeisis
to
tobe
beexecuted
executed
Status
Statuswindow:
window:
Give
Givethe
thevalue
valueof
of
all
related
all related
variables
variables
54
Status
Statuswindow:
window:
One
Onecan
canalso
also
particularly
particularlywatch
watch
the
theresult
resultof
ofan
an
expression
expressionby
by
typing
typingititin.
in.
55
Stop
Stopdebugging
debugging
Step
Step Out:
Out:
Return
Returnto
tothe
the
calling
function
calling function
Continue
Continue
debugging
debugging
until
untilthe
thenext
next
breakpoint
breakpoint
Step
Step Over:
Over:
Execute
Executejust
just
the
thecurrent
current
line
line
56
Step
Step Into:
Into:Like
Like
Step
Step Over,
Over,but
butifif
the
thecurrent
currentstatement
statement
isisaafunction,
function,go
gointo
into
this
thisfunction
functionto
todebug
debug
Step 3: Step
over the codes
On
Onstepping
steppingover
over
each
eachline
lineof
ofcode,
code,the
the
current
values
of
the
current values of the
variables
variablesare
areshown
shown
57
58
Step 5: Fix
the error
Stop
Stop
Debugging.
Debugging.
Go
Goback
backto
tothe
the
source
sourceand
andfix
fix
the
theerror
error
59
Exercise 4.2
The Run-time Debugger
of Visual Studio can also
help us analyze the runtime behavior of our
program
For the program on the
right, estimate the values
of CurrentX before and
after the 6th, 9th, and 11th
lines are executed
Verify your solutions by
using the Run-time
Debugger
60
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{ int CurrentX;
int x = 50;
CurrentX = x;
{
int x = 100;
CurrentX = x;
}
CurrentX = x;
return 0;
}