Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4.2 Problem Solving: Hand-Tracing Hand-tracing is a simulation of code execution

ID: 3745034 • Letter: 4

Question

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.6, 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? int n 1729; int sum = 0; while (n > ) int digit n % 10; sumsum digit; n n10; cout

Explanation / Answer

Answer are as follows

Answer 1.

the tracings are as follows

n count digit

1796 0

179 1 6

17 1 9

1 2 7

0 2 1

As n becomes 0, comes out of the loop

Answer 2. It computes all the digits in n that are 6 or 7.

Answer 3. Total 5 values are printed. they are 1,2,3,5,8

Answer 4. Output of r i c is

r is Fred

i = 4

c = d

Answer 5. The program Prints the every other character in string s and also counts the number of characters in s

Answer 6.

print "Enter the string"

get s

r =""

i=0

while i < length of s

c= ith chsracter of s

r = c + r

i++

print r

====================================================

Practice It Answer

Answer 1: 8 is the last value printed

Answer2:

int result = 1;

while(n>0)

{

int digit = n%10;

result = result *digit;

n = n/10;

}

cout<<result<<endl;

Answer 3 ,4 has same solution

int main()

{

int num, rev = 0;

int rem;

cout<<"Enter Number: ";

cin>>num;

while(num!=0)

{   

rem = num%10;

rev= rev*10 + rem;

num = num/10;

}

cout<<"Reverse: "<<rev;

}

Answer 5.

trace are :

n r

1 1

2 4

3 9

4

so at the end n = 4 and r = 9

Answer 6.

int n =1;

while(n<=3)

{

int r = n*n;

cout<<r;

if(n<3)

{

cout<<"," ;

}

n++;

}

Answer 7.

the Code is wrong, for the inputs that are less than ten, the loop condition should be temp>0;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote