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

Week8 Group1

Uploaded by

Hữu Định
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

Week8 Group1

Uploaded by

Hữu Định
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

TRƯỜNG ĐẠI HỌC BÁCH KHOA – ĐẠI HỌC ĐÀ NẴNG

KHOA CƠ KHÍ – GIAO THÔNG

Problem Solving: Hand-Tracing


Application: Processing Sentinel Values

GVHD: TS. Nguyễn Văn Minh


SVTH: Quách Hữu Định
Đặng Thế Hòa
Trịnh Phan Quyền
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


Hand-tracing is a simulation of code execution in which you step through instructions and
track the values of the variables
In Programming Tip 3.2, you learned about the method of hand-tracing. When you hand-
trace code or pseudocode, you write the names of the variables on a sheet of paper,
mentally execute each step of the code, and update the variables.
It is best to have the code written or printed on a sheet of paper. Use a marker, such as a
paper clip, to mark the current line. Whenever a variable changes, cross out the old value
and write the new value below. When a program produces output, also write down the
output in another column.
Consider this example. What value is displayed?
n = 1729
total = 0
while n > 0 :
digit = n % 10
total = total + digit
n = n // 10
print(total)
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


There are three variables: n, total, and digit.

The first two variables are initialized with 1729 and 0 before
the loop is entered.
n = 1729
total = 0
while n > 0 :
digit = n % 10
total = total + digit
n = n // 10
print(total)
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


Because n is greater than zero, enter the loop. The variable digit is set to 9. The variable
total is set to 0 + 9 = 9.
n = 1729
total = 0
while n > 0 :
digit = n % 10
total = total + digit
n = n // 10
print(total)

Finally, n becomes 172.


PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


Cross out the old values and write the new ones under the old ones.
n = 1729
total = 0
while n > 0 :
digit = n % 10
total = total + digit
n = n // 10
print(total)

Now check the loop condition again


n = 1729
total = 0
while n > 0 :
digit = n % 10
total = total + digit
n = n // 10
print(total)
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


Because n is still greater than zero, repeat the
loop. Now digit becomes 2, total is set to
9 + 2 = 11, and n is set to 17.

Repeat the loop once again, setting digit to 7,


total to 11 + 7 = 18, and n to 1.
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


Enter the loop for one last time. Now digit is set
to 1, total to 19, and n becomes zero.

n = 1729
total = 0
while n > 0 : Because n equals zero,
digit = n % 10 this condition is not
total = total + digit true.
n = n // 10
print(total)
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


The condition n > 0 is now false. Continue with the statement after the loop.
n = 1729
total = 0
while n > 0 :
digit = n % 10
total = total + digit
n = n // 10
print(total)

This statement is an output statement. The value that is output is the value of total, which
is 19.
Of course, you can get the same answer by just running the code. However, hand-tracing
can give you insight that you would not get if you simply ran the code. Consider again what
happens in each iteration:
• We extract the last digit of n.
• We add that digit to total.
• We strip the digit off of n.
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.2 Problem Solving: Hand-Tracing


Hand-tracing can help you understand how an unfamiliar algorithm works.
In other words, the loop computes the sum of the digits in n. You now know what the loop
does for any value of n, not just the one in the example.
Hand-tracing can show errors in code or pseudocode.
Hand-tracing does not just help you understand code that works correctly. It is a powerful
technique for finding errors in your code. When a program behaves in a way that you don’t
expect, get out a sheet of paper and track the values of the variables as you mentally step
through the code.
You don’t need a working program to do hand-tracing. You can hand-trace pseudocode. In
fact, it is an excellent idea to hand-trace your pseudocode before you go to the trouble of
translating it into actual code, to confirm that it works correctly.
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.3 Application: Processing Sentinel Values

• In this section, you will learn how to write loops that


read and process a sequence of input values.
• Whenever you read a sequence of inputs, you need to
have some method of indicating the end of the
sequence. Sometimes you are lucky and no input value
can be zero. Then you can prompt the user to keep
entering numbers, or 0 to finish the sequence. If zero is
allowed but negative numbers are not, you can use –1
to indicate termination.
• Such a value, which is not an actual input, but serves as
a signal for termination, is called a sentinel.
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.3 Application: Processing Sentinel Values


• Let’s put this technique to work in a program that computes the average of a set of
salary values. In our sample program, we will use any negative value as the sentinel. An
employee would surely not work for a negative salary, but there may be volunteers who
work for free
• Inside the loop, we read an input. If the input is non-negative, we process it. In order to
compute the average, we need the total sum of all salaries, and the number of inputs.

while . . . :
salary = float(input("Enter a salary or -1 to finish: "))
if salary >= 0.0 :
total = total + salary
count = count + 1
• Any negative number can end the loop, but we prompt for a sentinel of –1 so that the
user need not ponder which negative number to enter. Note that we stay in the loop
while the sentinel value is not detected.

while salary >= 0.0 :



PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.3 Application: Processing Sentinel Values


There is just one problem: When the loop is entered for the first time, no data value has
been read. We must make sure to initialize salary with a value that will satisfy the while
loop condition so that the loop will be executed at least once.

salary = 0.0 # Any non-negative value will do.

After the loop has finished, we compute and print the average.
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.3 Application: Processing Sentinel Values


Some programmers don’t like the “trick” of initializing the input variable with a value other
than a sentinel. Although it solves the problem, it requires the use of an if statement in the
body of the loop to test for the sentinel value. Another approach is to use two input
statements, one before the loop to obtain the first value and another at the bottom of the
loop to read additional values:
salary = float(input("Enter a salary or -1 to finish: "))
while salary >= 0.0 :
total = total + salary
count = count + 1 salary = float(input("Enter a salary or -1 to finish: "))

If the first value entered by the user is the sentinel, then the body of the loop is never
executed. Otherwise, the value is processed just as it was in the earlier version of the loop.
The input operation before the loop is known as the priming read, because it prepares or
initializes the loop variable.
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.3 Application: Processing Sentinel Values


The input operation at the bottom of the loop is used to obtain the next input. It is known
as the modification read, because it modifies the loop variable inside the loop. Note that
this is the last statement to be executed before the next iteration of the loop. If the user
enters the sentinel value, then the loop terminates. Otherwise, the loop continues,
processing the input.
Special Topic 4.1 shows a third approach for processing sentinel values that uses a Boolean
variable.
Now consider the case in which any number (positive, negative, or zero) can be an
acceptable input. In such a situation, you must use a sentinel that is not a number (such as
the letter Q).
Because the input function obtains data from the user and returns it as a string, you can
examine the string to see if the user entered the letter Q before converting the string to a
numeric value for use in the calculations:
inputStr = input("Enter a value or Q to quit: ")
while inputStr != "Q" :
value = float(inputStr)
Process value.
inputStr = input("Enter a value or Q to quit: ")
PBL 5: THIẾT KẾ HỆ THỐNG ĐIỀU KHIỂN

4.3 Application: Processing Sentinel Values


Note that the conversion to a floating-point value is performed as the first statement within
the loop. By including it as the first statement, it handles the input string for both the
priming read and the modification read.
Finally, consider the case where you prompt for multiple strings, for example, a sequence
of names. We still need a sentinel to flag the end of the data extraction. Using a string such
as Q is not such a good idea because that might be a valid input. You can use the empty
string instead. When a user presses the Enter key without pressing any other keys, the
input function returns the empty string:

name = input("Enter a name or press the Enter key to quit: ")


while name != "" :
Process name.
inputStr = input("Enter a name or press the Enter key to quit: ")
THANK YOU FOR
YOUR ATTENTION

You might also like