Solution Manual for C++ Programming: From Problem Analysis to Program Design, 8th Edition

Preview Extract
Chapter 2 1. a. false; b. false; c. true; d. true; e. false; f. false; g. true; h. true; i. false; j. false; k. true; l. false 2. b, d, e, f 3. b, e 4. A keyword is a reserved word and is defined by the system. A keyword cannot be redefined in a program. A user-defined identifier can be redefined. 5. The identifiers quizNo1 and quizno1 are not the same. C++ is case sensitive. The fifth letter of quizNo1 is uppercase N while the fifth character of quizno1 is lowercase n. So these identifiers are different 6 . a. 22 b. 2 c. 14 d. 8 e. 7.00 f. 21 g. 20 h. 0.00 i. 15.50 7. a. 7 b. 5.50 c. -1.00 d. Not possible. Both the operands of the operator % must be integers. y + z is of type double. Both operands, y + z and x, of %V must be integers. e. 13.50 f. 1 g. Not possible. Both the operands of the operator % must be integers. Because the second operand, z, is a floating-point value, the expression is invalid. h. 3.00 8. a, b, c, e, i, j, and k are valid; d, f, and g are invalid because the left side of an expression must be a variable. h is invalid because the operands of the mod operator must be integers. 9. x = 9, y = 5, z = 3, w = -3 10. Variable declarations in Lines 1, 6, and 7 are correct. Variable declaration in Line 2 is incorrect. Now, B+ is a string, so it must be enclosed in double quotes mark. Also, grade is a char variable and a string cannot be assigned to a char variable. A correct declaration is: char grade = ‘B’; //Line 2 Variable declaration in Line 3 is incorrect because the left side of the assignment operator must be a variable, and the semicolon at the end of the statement is missing. A correct declaration is: double num = 28.5; //Line 3 The variable declaration in Line 4 is incorrect because strings are enclosed in double quotation marks. A correct declaration is: string message = “First C++ course”; //Line 4 The variable declaration in Line 5 is incorrect because the value assigned to age must be an int value. A correct declaration is: int age = 18; //Line 5 11. a and c are valid 12. a. int x, y; x = 25; y = 18; b. int temp = 10; char ch = ‘A’; c. x = x + 5; d. double payRate = 12.5; e. tempNum = firstNum; f. temp = x; x = y; y = temp; g. cout << x << " " << y << " " << x + 12 / y – 18 << endl; h. char grade = 'A'; i. int num1, num2, num3, num4; j. x = static_cast(z + 0.5); 13. a. 9.0 / 5 * C + 32 b. static_cast(‘+’) c. static_cast(x + 0.5) d. str = “C++ Programming is exciting” e. totalInches = 12 * feet + inches f. i++, ++i, or i = i + 1; g. v = 4 / 3 * (3.1416 * r * r *r); h. s = 2* (3.1416 * r * *r) + 2 * (3.1416 * r) * h; i. a + (b โ€“ c) / d * (e * f โ€“ g * h) j. (โ€“b + (b * b โ€“ 4 * a * c)) / (2 * a) 14. x = 1 y = 102 z = 15 w = 44 15. x = 101 y = 11 z = 104 w = 159.00 t = 81.50 16. a. x = 18, y = 5, z = 4 b. 5 * x – y = 85 c. Product of 18 and 4 is 72 d. x – y / z = 17 e. 18 square = 324 17. a. 1000 b. 42.50 c. 1.25 d. 11.00 e. 9 f. 88.25 g. -2.00 18. a. cout << endl; or cout << "n"; or cout << 'n'; b. cout << "t"; c. cout << """; 19. a and c are correct 20. a. char grade = '*'; b. double salesTax = 0.05; c. int numOfJuiceBottles = 0; d. double billingAmount = 0.0; e. double gpa = 0.0; 21. a. int num1; int num2; b. cout << "Enter two numbers separated by spaces." <> num1 >> num2; d. cout << "num1 = " << num1 << ", num2 = " << num2 << ", 2 * num1 โ€“ num2 = " << 2 * num1 โ€“ num2 << endl; 22. A correct answer is: #include #include using namespace std; const double DECIMAL = 5.50; const string blanks = ” “; double PAY_RATE = 10.75; int main() { int height, weight; double discount; double billingAmount; double bonus; int hoursWorked = 45; double price; height = 6; weight = 156; cout << height << " " << weight << endl; discount = (2 * height + weight) % 10; price = 49.99; // billingAmount = price * (1 – discount) – DECIMAL ; DECIMAL = 7.55; cout << price << blanks << "$" << billingAmount << endl; bonus = hoursWorked * PAY_RATE / 50; cout << "Bonus = " << bonus << endl; return 0; } 23. A correct answer is: #include using namespace std; const char STAR = ‘*’; const int PRIME = 71; int main() { int count, sum; double x; int newNum; //declare newNum count = 1; sum = count + PRIME; x = 25.67; // x = 25.67; newNum = count * 1 + 2; //newNum = count * ONE + 2; sum++; //(x + sum)++; sum = sum + count; //sum + count = sum; x = x + sum * count; // x = x + sum * COUNT; sum += 3; //sum += 3–; cout << " count = " << count << ", sum = " << sum << ", PRIME = " << PRIME << endl; return 0; } 24. A correct answer is: #include #include using namespace std; int main() { int num1, num2; string str1; cout <> str1; cout << endl; cout <> num1 >> num2; cout << endl; cout << str1 << " " << "num1 * num2 = " << num1 * num2 << endl; return 0; } 25. An identifier must be declared before it can be used. 26. b. 27. a. x += 5; b. x *= 2 * y c. totalPay += currentPay; d. z *= (x + 2); e. 28. a. y /= x + 5; x = x + 5 โ€“ z; b. y = y * (2 * x + 5 โ€“ z); c. w = w + 2 * z + 4; d. x = x โ€“ (z + y โ€“ t); e. sum = sum + num; 29. a b c a = (b++) + 3; 8 3 und c = 2 * a + (++b); 8 2 12 b = 2 * (++c) โ€“ (a++); 9 -3 11 30. a sum = static_cast(a + b + c); 6 b += c * a; c -= a; a *= 2 * b – c; 6 6 214 b c sum 3 16 16 16 2.2 2.2 -3.8 -3.8 11 11 11 11 31. (The user input is shaded.) firstNum = 62 Enter three numbers: 35 10.5 27 The numbers you entered are 35, 10.5, and 27 z = 33 Enter grade: B The letter that follows your grade is: C 32. (The user input is shaded.) Enter last name: Miller Enter a two digit integer: 34 Enter a decimal number: 62.5 Name: Miller Id: 34 Mystery number: -5.14286 33. #include #include using namespace std; const double X = 13.45; const int Y = 18; const char STAR = ‘*’; int main() { string employeeID; string department; int num; double salary; cout <> employeeID; cout << endl; cout <> department; cout << endl; cout <> num; cout << endl; salary = num * X; cout << "ID: " << employeeID << endl; cout << "Department " << department << endl; cout << "Star: " << STAR << endl; cout << "Wages: $" << salary << endl; cout << "X = " << X << endl; cout << "X + Y = " << X + Y << endl; return 0; } 34. The program requires four inputs in the following order: string decimal_number decimal_number integer

Document Preview (7 of 542 Pages)

User generated content is uploaded by users for the purposes of learning and should be used following SchloarOn's honor code & terms of service.
You are viewing preview pages of the document. Purchase to get full access instantly.

Shop by Category See All


Shopping Cart (0)

Your bag is empty

Don't miss out on great deals! Start shopping or Sign in to view products added.

Shop What's New Sign in