question archive Write a single C++ statements to accomplish each of the following tasks: In one statement, assign the sum of the current value of x and y to z and postincrement the value of x
Subject:Computer SciencePrice:3.87 Bought7
Write a single C++ statements to accomplish each of the following tasks:
“Count is greater than 10.”.
(b)
SHOW THE STEPS.
(c) Write a program that prompt the user to enter a string. Encrypts a message where each of the character in the string will be replaced by the next 3 letters of the current letter. For example: if the user input a message “Hello”, then the encrypted message is “Khoor”.
Sample Output Run
Enter a String : My Best Friend
Encrypted message is : P|#Ehw#Iuhqg
Answer:
a. In one statement, assign the sum of the current value of x and y to z and post-increment the value of x:
int z = (x++) + y;
i. Determine whether the value of the variable count is greater than 10. If it is, print “Count is greater than 10.”:
int count = 20;
cout << ((count > 10) ? "Count greater than 10" : "Count less than 10");
iii. Pre-decrement the variable of x by 1, then subtract it from the variable total:
(total - --x);
(b)
Given a=3, b=2, and c=5
Pre-increment:
a = a + 1;
(++a) means, cout << a; will give "4".
Post-increment:
a=a;
a = a + 1;
(a++) means, cout << a; will give "3". If again, cout << a; - it will print 4.
i. R = ++a * c++ % b + (a * b) > 20:
= 4 * 5 % 2 + (4 * 2) > 20
= 4 * 5 % 2 + 8 > 20 [Paranthesis comes 1st]
= 20 % 2 + 8 > 20 [Multiplication, Division, Modulus are all of the same priority. So, we will traverse from left to right]
= 0 + 8 > 20
= 8 > 20 = 0 [Yes = 1 and No = 0]
Answer: 0
[After above operation, a = 4, b = 2, c = 6]
ii. R = b * c / a++ + ++a + ++b:
= 2 * 6 / 4 + 6 + 3 ["a" was 4, a++ gave 4 at that instant and changing "a" to 5, ++a gave 6 at that instant]
= 12 / 4 + 6 + 3
= 3 + 6 + 3 = 12
Answer: 12
[After above operation, a = 6, b = 3, c = 6]
iii. R = (a * b * (c + (9 * 3 / 3)))
= (6 * 3 * (6 + (9 * 3 / 3)))
= (6 * 3 * (6 + 9)) [9 * 3 / 3 = 27 / 3 = 9]
= (6 * 3 * 15) = 270
Answer: 270
[After above operation, a = 6, b = 3, c = 6]
iv. R = b % b + b * b – b / b
= 3 % 3 + 3 * 3 - 3 / 3
= 0 + 9 - 1 = 8
Answer: 8
[After above operation, a = 6, b = 3, c = 6]
v. R = c / b * 10 % 3 < 5
= 6 / 3 * 10 % 3 < 5
= 2 * 10 % 3 < 5
= 20 % 3 < 5
= 2 < 5, gives 1 [Yes = 1 and No = 0]
Answer: 1
c.
#include <iostream>
#include <string>
#include <cctype>
#include <stdlib.h>
using namespace std;
int main()
{
string str, resultString = "";
cout << "Enter a string: ";
getline(cin, str);
for(int i = 0; i < str.length(); i++)
{
if(str[i] == ' ')
str[i] = '#';
else
str[i] += 3;
}
cout << "\nEncrypted message is: " << str << endl;
return 0;
}