The following pseudocode is intended to count the number of digits in the positi
ID: 3745927 • Letter: T
Question
The following pseudocode is intended to count the number of digits in the positive integer n:
count = 1
temp = n
while temp > 10
Increment count
Divide temp by 10.0
Trace the pseudocode for n =123, n = 100, and n=3. What errors did you find, and how do you fix the code?
Explanation / Answer
count = 1 temp = n while temp > 10 Increment count Divide temp by 10.0 Tracing: ----------- 1) n = 123 count = 1 temp = 123 temp > 10 (true) count = 2 temp = 12 temp > 10(true) count = 3 temp = 1 temp > 10(false) so, count is 3. Correct 2) n = 100 count = 1 temp = 100 temp > 10 (true) count = 2 temp = 10 temp > 10(false) so, count is 2. InCorrect. since there are 3 digits. 3) n = 3 count = 1 temp = 3 temp > 10(false) so, count is 1. Correct. Fixed code: ------------ count = 1 temp = n while temp >= 10 Increment count Divide temp by 10.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.