1.Evaluate the following: a) 18 - 3 + 6 b) 30 % 12 + 56 - 35 / 5 * 8 c) 14 - 4 %
ID: 3853947 • Letter: 1
Question
1.Evaluate the following:
a) 18 - 3 + 6
b) 30 % 12 + 56 - 35 / 5 * 8
c) 14 - 4 % 11
d) 18.0 + 5.0 * 3.0 / 6.0
2. Given int x = 3; int y = 18; double z = 3.5; and double w = 2.0; evaluate each of the following statements, if possible. If the expression is illegal clarify the reason why.
a) (x + y) % x
b) x % y - w
c) x % (y + z)
d) (y + z) / w
3.Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason and provide the correct variable declaration.
a) double conversion = 2.54;
b) char grade = 'B+';
c) double 28.5 = num;
d) string message = 'First C++ course ';
e) int age = 19 years;
f) float i, y, decimal;
g) double fact 1;
h) string itIsOver = "It is over!"
4.Suppose x, y, z, and w are int variables. What value is assigned to each of these variables after the last statement executes?x = 2; y = 11;z = y - 3 * x;x = z + y;y = x + 5 * z;w = x - y + 2 * z;x = y + w - x;//now x = , y = , z = , w =
5.Suppose x, y, and z are int variables and w and t are double variables. What value is assigned to each of these variables after the last statement executes?x = 22;y = 15;x = x - y / 4 - 2;z = x % 3;w = 27 / 3 + 6.5 * 2;t = x / 4.0 + 15 % 3 - 3.5;
//now x = , y = , z = , w = , t =
6.Write C++ statements that accomplish the following:a) Output the newline character.b) Output the tab character.c) Output double quotation mark.
7.Give meaningful identifiers for the following variables:a) A variable to store the first name of a student.b) A variable to store the discounted price of an item.c) A variable to store the number of juice bottles.d) A variable to store the number of miles traveled.e) A variable to store the highest test score.
8.Write C++ statements to do the following:a) Declare int variable num1 and num2.b) Prompt the user to input two numbers.c) Assign the first input number to num1 and the second input number to num2d) Output num1, num2, and 2 times (num1 minus num2). Your output must identify each variable name followed by its value, then the expression for 2 times (num1 minus num2) followed by its value.
9.Suppose a, b, and c are int variables and it each case the program begins with a = 5, b = 6, and c is undefined. What value is assigned to each variable after each statement executes? If a variable is undefined at a particular statement, report UND(undefined).
Case 1: a = (b++) + 3; //a = , b = , c =
Case 2: c = 2 * a + (++b); //a = , b = , c =
Case 3: b = 2 * (++c) - (a++); //a = , b = , c =
10.What is printed by the following program? Suppose the input is as follows:
Miller34340
#include <iostream>#include <string>
using namespace std;
const int PRIME_NUM = 11;
int main () {
const int SECRET = 17;string name;int id;int num;int mysteryNum;cout << "Enter last name: ";cin >> name;cout << endl;cout << "Enter a two digit number: ";cin >> num;cout << endl;id = 100 * num + SECRET;cout << "Enter a positive integer less than 1000: ";cin >> num;cout << endl;mysteryNum = num * PRIME_NUM - 3 * SECRET;cout << "Name: " << name << endl;cout << "Id: " << id << endl;cout << "Mystery number: " << mysteryNum << endl;
return 0;
}
Explanation / Answer
Here are the solutions for the first 2 questions...
1. The order of precedence of arithmetic operators is:
%, /, * comes first which are left associative which means will be executed from left to right.
+, - comes next which are also left associative.
a. 18 - 3 + 6 = 15 + 6 = 21.
b. 30 % 12 + 56 - 35 / 5 * 8 = 6 + 56 - 35 / 5 * 8 = 6 + 56 - 7 * 8 = 6 + 56 - 56 = 62 - 56 = 6.
c. 14 - 4 % 11 = 14 - 4 = 10.
d. 18.0 + 5.0 * 3.0 / 6.0 = 18.0 + 15.0 / 6.0 = 18.0 + 2.5 = 20.5
2. Given int x = 3; int y = 18; double z = 3.5; and double w = 2.0;
Evaluate each of the following statements, if possible.
If the expression is illegal clarify the reason why.
a) (x + y) % x = (3 + 18) % 3 = 21 % 3 = 0.
b) x % y - w = 3 % 18 - 2.0 = 3 - 2.0 = 1.0.
c) x % (y + z) = 3 % (18 + 3.5) = 3 % 21.5. Here % cannot be applied on floating point number, so is illegal.
d) (y + z) / w = (18 + 3.5) / 2.0 = 21.5 / 2.0 = 10.75.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.