157k views
4 votes
1 // Lab 2 tryIt2A 2 #include 3 using namespace std; 4 5 int main() 6 { int x = 1, y = 3; 7 int X = 2, Y = 4; 8 9 cout << "tryIt 2A" <

User Quenton
by
3.6k points

1 Answer

1 vote

Answer:

Here is the complete program:

#include <iostream>

using namespace std;

int main()

{ int x = 1, y = 3;

int X = 2, Y = 4;

cout << "tryIt 2A" <<endl;

cout << x << y << endl;

cout << "x" << "y" << endl;

cout << X << " " << Y << endl;

cout << 2 * x + y << endl;

cout << 2 * X + Y << endl;

//cout << x + 2*y << endl;

cout << "x = ";

cout << x;

cout << " y = ";

cout << y;

return 0;

}

Step-by-step explanation:

I will explain the code line by line in the comment with each line of code and the output of each cout statement.

  • int x = 1, y = 3;

This statement assigns value 1 to integer variable x and 3 to int variable y

  • int X = 2, Y = 4;

This statement assigns value 2 to integer variable X and 4 to int variable Y As C++ is a case sensitive language so variable x and y are different from variables X and Y.

  • cout << "tryIt 2A" <<endl;

This statement has cout which is used to display output on the screen. So the output displayed by this cout statement is:

tryIt2A

  • cout << x << y << endl;

This statement will print the values stored in x and y variables. So output displayed by cout statement here is 1 and 3. As there is not space or next line specified in the statement so output displayed will look like this:

13

  • cout << "x" << "y" << endl;

This statement will display x and y but these are not the variable x and y. They are enclosed in double quotation marks so they are treated as strings not variables so the output displayed is:

xy

  • cout << X << " " << Y << endl;

This statement will print the values stored in X and Y variables. So output displayed by cout statement here is 2 and 4. As there is space " " specified in the statement so 2 and 4 are displayed with a space between them so the output displayed will look like this:

2 4

  • cout << 2 * x + y << endl;

This statement has an arithmetic operation in which 2 is multiplied by the values stored in variable x and then the result is added by value of y. So 2*1 = 2 and 2 + 3 = 5. So the result produced by this cout statement is:

5

  • cout << 2 * X + Y << endl;

This will work same as above cout statement but the only difference is that the values of capital X and Y variables are calculated here. So 2 * 2 = 4 and then 4 + 4 = 8. The result produced by this cout statement is:

8

  • //cout << x + 2*y << endl;

This is a comment because before this statement // is written which is used for single line comment. So compiler ignores comments and will not compile this statement.

  • cout << "x = ";

This will display "x = " as it is not variable but it is treated as a line to be displayed on the screen. So cout statement displays:

x =

  • cout << x;

This will print the value stored in x variable as there are no double quotes around x so it is a variable which contains value 1. In the above statement there is no endl so the output of this cout statement is displayed with the output of previous cout statement. So the following line is displayed on screen:

x = 1

  • cout << " y = ";

This will display "y = " as it is not variable but it is treated as a line to be displayed on the screen. In the above statement there is no endl so the output of this cout statement is displayed with the output of previous cout statement. So the following line is displayed on screen

x = 1 y =

  • cout << y;

This will print the value stored in y variable as there are no double quotes around y so it is a variable which contains value 3. In the above statement there is no endl so the output of this cout statement is displayed with the output of previous cout statement. So the following line is displayed on screen:

x = 1 y = 3

So the output of the entire program along with the program is attached as screenshot.

1 // Lab 2 tryIt2A 2 #include 3 using namespace std; 4 5 int main() 6 { int x = 1, y-example-1
User Grorel
by
4.1k points