In: Computer Science
Trace the following program. This means you need to fill the table in which you show the output of each executed instruction as if you are the computer executing the program. Put your comments under the table.
#include<iostream>
using namespace std;
int main ()
{
cout.setf(ios :: fixed);
cout.setf(ios :: showpoint);
cout.precision(2);
int A=7.5, B=2.5, C; //statement 1
double X,Y;
char Letter;
C = A/B; //statement 2
X = 1.0*A/B; //statement 3
if (C<X)
Letter = 30*C; //statement 4
if (C=X)
Letter = 20*C; //statement 5
if (C>X)
Letter = 10*C; //statement 6
cout << "Letter = " << Letter+10 << endl; //statement 7
Letter = Letter + 10; //statement 8
cout << "Letter = " << Letter << endl; //statement 9
Y = 1.0*(A/B); //statement 10
X = Y/ (B+5) + 2.5; //statement 11
cout.precision(1);
cout << "X = " << X << endl; //statement 12
return 0;
}
Please find the dry run attached.
#include<iostream>
using namespace std;
int main ()
{
cout.setf(ios :: fixed);
cout.setf(ios :: showpoint);
cout.precision(2);
int A=7.5, B=2.5, C; //statement 1 , here value of A=7, B=2, C=Garbage value
double X,Y; // both will contain garbage value
char Letter; // garbage value
C = A/B; //statement 2, here A=7, B=2, 7/2 = 3.5 but 3 will be stored in C
X = 1.0*A/B; //statement 3, here A=7, B=2, X = 1.0*7/2 = 7.0/2 = 3.50
if (C<X) // here C=3, X=3.50, condition is true
Letter = 30*C; //statement 4, Letter=30*3 = 90, 90 is ascii of Z, So Letter = Z.
if (C=X) // here C=3, X=3.50, I guess this must be == , here C = X, C will store 3 and this condition is true
Letter = 20*C; //statement 5, Letter=20*3 = 60, 60 is ascii of <, So Letter = <.
if (C>X) // here C=3, X=3.50, condition is false
Letter = 10*C; //statement 6, we won't reach here
cout << "Letter = " << Letter+10 << endl; //statement 7, Letter + 10 = 60 + 10 = 70
Letter = Letter + 10; //statement 8, Letter = 70, 70 is ascii of F. So, Letter = F.
cout << "Letter = " << Letter << endl; //statement 9, Letter = F
Y = 1.0*(A/B); //statement 10, here A=7, B=2, Y = 1.0*(7/2) = 1.0*3 = 3.00
X = Y/ (B+5) + 2.5; //statement 11, here Y = 3.0, B = 2, X = 3.0/(2+5) + 2.5 = 3.0/7 + 2.5 = 0.428+2.5 = 2.928, 2.93 will be stored in X
cout.precision(1); // precision set to 1 digit
cout << "X = " << X << endl; //statement 12, X=2.9
return 0;
}