Question

**C++** a. Write a program containing the following expressions. x should be an int variable. Add...

**C++** a. Write a program containing the following expressions. x should be an int variable. Add a statement after each one to print out the current value of x in the following format (of course, you will print the value of x, not the constant 4!): Problem 1. x has the value 4

1. x = 4;

2. x = 2;

3. x = 24 – 6 * 2; (show each computation)

4. x = -15 * 2 + 3; (show each computation)

5. x = 72 / 5; (why not 14.4?)

6. x = 72 % 5;

7. x = 5 * 2 / 6 + 15 % 4; (show each computation) On this paper write the answer printed and a small note for each problem indicating the value given and why the given value was computed. Refer to the actions of operators and precedence of operations in your answers when appropriate. b. Add the following statements to your program and once again print the value of x for each one:

8. x = x + 1;

9. x++;

10. x = x + 3;

11. x += 3;

12. x += x + 3;

13. x--; Write a small note for each problem above indicating the answer and why the given values were printed.

c. Add the following statements to your program (where z is an int variable). Print the value of z for each numbered problem.

14. z = x * x;

15. z = z % 10;

16. z = x * x; z = z / 10;

17. z = x * x; z = (z / 10) % 10; Write a small note for each problem indicating the answer and why the given values were printed.

d. Finally, declare a double variable y. Once again, add the following statements and print the value of y after each one.

18. y = 72 / 5;

19. y = 72 / 5.0; Write a small note for each problem indicating why the given values were printed.

Homework Answers

Answer #1

a.

#include <iostream>
using namespace std;

int main()
{
int x;
x = 4;
cout << "x has value " << x <<endl;
x = 2;
cout << "x has value " << x <<endl;
x = 24 - 6 * 2; // x=24-(6*2)=24-12=12
cout << "x has value " << x <<endl;
x = -15 * 2 + 3; //(-15*2)+3=-30+3=-27
cout << "x has value " << x <<endl;
x = 72 / 5; //because it is of int type it prints quotient of 75 divides 5
cout << "x has value " << x <<endl;
x = 72 % 5; //it prints the remainder of 75 divided by 5
cout << "x has value " << x <<endl;
x = 5 * 2 / 6 + 15 % 4;
//((5*2)/6)+(15%4)
//(10/6)+3
//1+3
//4
cout << "x has value " << x <<endl;
}
Output:

After executing the statements in 'a' the value stores in x is 4.

now we will execute the statements in 'b' by using the x value as 4.

b.

#include <iostream>
using namespace std;

int main()
{
int x;
x = 4;
cout << "x has value " << x <<endl;
x = x+1; // x increments by 1
cout << "x has value " << x <<endl;
x++; //post increment
//x has been incremented after the execution of this statement
//if we done k=x++; the k is equal to previous value of x
//x is incremented by 1
cout << "x has value " << x <<endl;
x = x + 3; // x increments by 3
cout << "x has value " << x <<endl;
x += 3; //this statement is equal to (x=x+3)
cout << "x has value " << x <<endl;
x += x + 3;//this statement is equal to x=x+(x+3)
//x=12+(12+3)
//x=12+15=27
cout << "x has value " << x <<endl;
x--; //x decrements by 1
cout << "x has value " << x <<endl;
}
Output:

After executing the statements in 'b' the value stores in x is 26.

now we will execute the statements in 'c' by using the x value as 26.

c.

#include <iostream>
using namespace std;

int main()
{
int x,z;
x = 26;
cout << "x has value " << x <<endl;
z=x*x; //z=26*26=676
cout << "z has value " << z <<endl;
z=z%10; //z=676%10 = last digit of number 67'6'=6.
cout << "z has value " << z <<endl;
cout << "x has value " << x <<endl;
z=x*x; //z=676
cout << "z has value " << z <<endl;
z=z/10; //z=676/10 = all digits of number except last one '67'6 = 67
cout << "z has value " << z <<endl;
cout << "x has value " << x <<endl;
z=x*x; //z= 676
cout << "z has value " << z <<endl;
z=(z/10)%10; //z=(676/10)%10=67%10=7
cout << "z has value " << z <<endl;
}

d.

#include <iostream>
using namespace std;

int main()
{
double y;
y = 72/5;
//Here 72,5 both are int data type
//it calculates the value of 72/5 that is
//quotient of 72 divides 5 = 14
cout << "y has value " << y <<endl;
y = 72/5.0;
//But here 72 is int and 5.0 is double
//so fractional calculation happens here
// and the value is 14.4.
cout << "y has value " << y <<endl;
}

If you have any queries, please comment below.

Please upvote , if you like this answer.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program to do the following: Declare and assign values to int variables x,...
Write a C++ program to do the following: Declare and assign values to int variables x, y Declare an int variable z; and store the sum of x and y in it Declare a pointer called pz and point it in the heap direction (using new) Store the z value in the heap location using the pz pointer Print the content of the pz pointer Print the pointer (the address)
A.6 ... static int x = 1; int y = x * 2; void t1() {...
A.6 ... static int x = 1; int y = x * 2; void t1() {                 y++;                 cout << "x: " << x << " | y: " << y << endl;                 y += 1;                 x -= -1; } void t2() {                 int* x = &y;                 cout << "x: " << x << " | y: " << y << endl; } void t3() {                 int y = x;                 static int x...
Write a C function to compute the following output (int) based on two input (int) variables:...
Write a C function to compute the following output (int) based on two input (int) variables: output = a*a + b*b - 2*a*b If the value of output is '0', then return -1 instead. Call the C function 4 times from main() with values of 'a' and 'b' as follows and print output values for each case. a = 3, b = 4 a = 1, b = 1 a = -2 b = 3 a = -4 b =...
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
1.    Given the following segment of code: (If there is nothing output, write None.) int x;...
1.    Given the following segment of code: (If there is nothing output, write None.) int x; int y; cin >> x; cin >> y; while (x > y) {     x -= 3;     cout << x << " "; } cout << endl;        a.    What are the output and final values of x and y when the input is 10 for x and 0 for y? [2, 2, 2]               Output                                                                                                                                                                                                    x = ______________                                                                                                                                                                                                   ...
Write a C++ program to compute the value of x * (x + 1) + y...
Write a C++ program to compute the value of x * (x + 1) + y * y + z * (z - 1) where x, y, and z are 3 floating point numbers entered by the user. Requirements: • You should have a function get3numbers() that asks the user to enter 3 numbers. • You should have a function computeExp() that computes the value of the expression. • Your main function should call the above 2 functions and get...
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that...
If and else statements: 1a.) Given the following declaration: int temperature; Write an if statement that prints the message "The number is valid" if the variable temperature is within the range of -50 trough 150 (both values excluded). 1b.) Given the following declarations int x; int y; Write an if statement that assigns 100 to x when y is equal to 0. 1c.) Given the following declaration: int grade; Write an if statement that prints the message "The number is...
A) write a program the computes nx and store the result into y You can use...
A) write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to opportunity retry the entry the user must be able to quit prior to entering...