Walk through the following segments of code giving the value

Walk through the following segments of code giving the values of all the variables and the output:

1)

int x, y, z;

x = 7; cout << x << “ and y” << endl;

y = x / 5;

z = y % 3 * 2;

cout << x << z << y << endl;

2)

int a, b, c;

c = 9;

a = c – 3;

b = a + c; cout << b << c + 4 << endl;

c = c – 2; cout << a << b – 2 << endl;

b = b + 4;

a = a % 5;

cout << a << “ “ << b << “ “ << c << endl;

3)

int m, n;

m = 8;

n = m + 3;

string w = “why?”;

cout << m << “ “ << n << endl;

cout << w << endl;

m = n – 3 * 3;

cout << n << “ “ << m << endl;

cout << “why not?” << endl;

Solution

Walk through the following segments of code giving the values of all the variables and the output:
1)
int x, y, z; //declaring 3 variable x,y and z
x = 7; cout << x << “ and y” << endl;//initialising the variable x to 7
y = x / 5; // dividing the variable x by 5 leaves the output as 1.4, and the integer value of 1.4 i.e. 1 is stored in variable y
z = y % 3 * 2;// % means modulus y(value 2) % 3 means 1 divided by 3 leaves a remainder 1, which is multiplied by 2 to give 2 and stores in variable z
cout << x << z << y << endl;// printing x(value=7), z(value=2) & y(value=1)
/********output*************/
7 and y   
721
/********output ends*************/

2)
int a, b, c; //declaring 3 variable a,b and c
c = 9; //initialising the variable c to 9
a = c - 3; // subtracting 3 from c(value=9) which is 6 and storing it in a
b = a + c; // adding a(value=6) and c(value=9) which is 15 and storing it to variable b
cout << b << c + 4 << endl; // printing b(value=15) and c+4 i.e. 13
c = c - 2; // subtracting 2 from c i.e. 9-2=7
cout << a << b - 2 << endl;// printing a(value=6) and b-2 i.e 13
b = b + 4;// adding 4 to b i.e. 15+4=19
a = a % 5;// 6 % 5=1(remainder)
cout << a << \" \" << b << \" \" << c << endl; //printing a(value=1), b(value=19) && c(value=7)

/********output*************/
1513
613   
1 19 7

/********output ends*************/

3)
int m, n;//declaring variable m and n
m = 8;//initialising m to 8
n = m + 3;// adding 3 to m i.e. 8+3=11 and store it to n
string w = \"why?\"; //string variable and initialise to \"why\"
cout << m << \" \" << n << endl;//printing m(value=8) and n(value=11)
cout << w << endl;//printing string w(value=\"why\")
m = n - 3 * 3;// m=11-3*3 i.e. 11-9=2
cout << n << \" \" << m << endl;//printing n(value=11) and m(value=2)
cout << \"why not?\" << endl;//printing string \"why not?\"

/********output*************/
8 11
why?
11 2
why not?

/********output ends*************/

Note: each line is explained as comment for the output. Please feel free to ask question. God bless you!

Walk through the following segments of code giving the values of all the variables and the output: 1) int x, y, z; x = 7; cout << x << “ and y” <
Walk through the following segments of code giving the values of all the variables and the output: 1) int x, y, z; x = 7; cout << x << “ and y” <

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site