Question 3
Run the following code and explain, using your own words, what is happening with each
variable (w,x,y,z) and what is the difference between ++x and w++ (is there any
influence on the result between prefix or postfix ++ operator?). Provide a snippet of the
output showing the result.
int main()
{
int w = 20, x = 20;
int y = 5, z = 5;
y = y + (++x);
z = z + (w++);
cout << "x=" << x << " and y=" << y << endl;
cout << "w=" << w << " and z=" << z << endl;
system("pause");
return 0;
}
Provide a paragraph that answers question 3.
Provide 4 valid test cases for (w, x, y, z).
Test # |
Valid / Invalid Data |
Description of test |
Input Value |
Actual Output |
Test Pass / Fail |
1 |
Valid |
W |
Pass |
||
2 |
Valid |
X |
Pass |
||
3 |
Valid |
Y |
Pass |
||
4 |
Valid |
Z |
Pass |
The values of the above program after execution is
x = 21
y = 26
w = 21
z = 25
The difference between ++x and w++ is
For ++x the value is incremented first and assigns to the variable x..That's why y = y + (++x) here y = 26 because first y is equal to 5 plus x value is 21(pre increment).So 5 + 21 =26.
For w++ the value for w is assigned first and increments the value.That's why z = z + (w++); here z = 25 because the first z value is equal to 5 plus w value is 20(post increment).So 5 +20 = 25
Now the z value becomes incremented so the z value is 21.
The x value is also updated after incremented so the value of x is 21
Question 3 Run the following code and explain, using your own words, what is happening with...
QUESTION 1 What is the output of the following code snippet? int main() { bool attendance = false; string str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } cout << str << endl; return 0; } False True Unknown Maybe QUESTION 2 What is the output of the following code snippet? #include <iostream> #include <string> using...
Question 1 The following program asks the user for the current temperature (in Fahrenheit). Provide a total of 7 valid test cases (one for each message). Do not provide invalid test cases. Add a series of if statements (if with multiple alternatives i.e. if/else if/else) so that one of the following messages is printed based on the temperature value: Temperature (F) Message >90 “Go swimming.” <=90, >80 “Turn on air conditioning.” <=80, >70 “Do nothing.” <=70, >55 “Turn on heat.”...
,,1. Which of the following statments are valid C++ statements? A) cout << "Hello World" : B) cout << Hello World; C) cout << "Hello " << "World" ; D) cout << "Hello" << "World" ; 2. What is the difference between the literals 'A' and "A"? ,3. Read the input from user; Check for valid entries If input is invalid: print an error message; else: calculate the result and print to the console; exit; The code snippet above may...
What is the output of the following program? int main(void) { int x, y, z; x = 5; y= test(x); z= x + y; cout<< setw(4)<<x <<” + “<<setw(4)<<y<<”=” << setw(4)<<z<<endl; return 0; } int test (int x) { int w; w = x + 10; return (w); }
How can I fix that ? Can you please explain clearly ? main.cpp 2 3 using namespace 4. 5 int main() std; unsigned int x = 0b01101100; 8 9 10 12 13 14 15 16 17 18 19 20 21 unsigned int z = 0x12345678; unsigned int e 0x7; unsigned int w = 0x87654321; unsigned int =0x123; int 1; int 92; int Q3; int Q4; Q1=x&y; cout << bitset<8> (Q1) << endl; Q2=-(x| y); cout << bitset<8>(Q2) << endl; Q3=(z...
16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int main) int x-1,y 0,z-2; x-fun2(y,z); cout sx fun 1 (z); cout (#xtytz(endl; y-fun2(x,x); cout <exty+zscendl; system("pause"); void fun 1 (int a) int fun2(int a, int b) int static c2; return atx; 16 Points) Question 3 Write down the outputs of the following program into the provided table...
A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;
I know the right answers by using a compiler. However, can you please explain each step and how you get the correct answer. Thank you in advance. Output: E = 1 F = 7 G = 11 H = 14 W = 5 X = 6 Problem 7 #include <iostream> #include <cmath> using namespace std; void F1(int,int,int,int&); void F2(int,int&,int&,int&); int main ( void ) { int E=1,F=2,G=3,H=4,W=5,X=6; F1(E,F,G,H); F2(E,F,G,H); cout << "E = " << E <<...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...
What is the output of the following code? Is there a relationship between the variables x and y? If yes, state the relationship? What is the output? int x = 19683; int i; int y = 0; for (i = x; i >= 1; i = i / 3) y++; cout << "x = " << x << ", y = " << y << endl;