could you explain how to answer this question programming C , EXPLAIN step by st
ID: 3561925 • Letter: C
Question
could you explain how to answer this question programming C , EXPLAIN step by step.
For the following pseudo-code or set of instructions , give the values of a, v, s, and p as you walk through the instructions. In other words, for each variable: a, v, s, and p, list its values per the example as they change throughout the execution of the instructions. You must list all values of each variable, not just the final values of the variables.
a is 0 % line 1
v is the 845 % line 2
s is 0 % line 3
p is 2 % line 4
r is 0 % line 5
while (v is greater than 0) do % line 6
multiply a by 10 % line 7
divide v by 10 using integer division1 and put the result into s % line 8
multiply s by 10, and put the result into p % line 9
subtract p from v and put the result into r % line 10
add r to a % line 11
Explanation / Answer
line 1 : a =0
line 2: a=0 v =845
line 3 : a= 0 v=845 s = 0
line 4 : a =0 v =845 s=0 p =2
line 5; a =0 v =845 s =0 p = 2 r=0
pass 1: ( since v > 0) condition satisfied
line 6 : a =0 v =845 s =0 p = 2 r=0
line 7: a =0 v =845 s =0 p = 2 r=0 ( since a =0 multiply by 10 still gives 0)
line 8: a =0 v =845 s =84 p = 2 r=0 ( s= v/10)
line 9: a =0 v =845 s =84 p = 840 r=0 ( p = s*10)
line 10 : a =0 v =845 s =84 p = 840 r =5 ( r = v-p)
line 11 : a =5 v =845 s =84 p = 840 r =5 ( a=a+r)
line 12 : a =5 v =84 s =84 p = 840 r =5 ( v=s)
pass 2:
line 6 :a =5 v =84 s =84 p = 840 r =5
line 7: a =50 v =84 s =84 p = 840 r =5 ( since a=a*10)
line 8: a =50 v =84 s =8 p = 840 r =5 ( s= v/10)
line 9: a =50 v =84 s =8 p = 80 r =5 ( p = s*10)
line 10 :a =50 v =84 s =8 p = 80 r =4 ( r = v-p)
line 11 : a =54 v =84 s =8 p = 80 r =4 ( a=a+r)
line 12 : a =54 v =8 s =8 p = 80 r =4 ( v=s)
pass 3:
line 6 : a =54 v =8 s =8 p = 80 r =4
line 7: a =540 v =8 s =8 p = 80 r =4 (a =a*10)
line 8: a =540 v =8 s =0 p = 80 r =4 ( s= v/10)
line 9: a =540 v =8 s =0 p = 0 r =4 ( p = s*10)
line 10 :a =540 v =8 s =0 p = 0 r =8 ( r = v-p)
line 11 : a =548 v =8 s =0 p = 0 r =8 ( a=a+r)
line 12 : a =548 v =0 s =0 p = 0 r =8 ( v=s)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.