Docsity

C++ Programming Exercises and Answers: Chapter 2, Study notes of Engineering

Toggle sidebar
bg1
Chapter 2 C++ Syntax and Semantics, and the Program Development Process
Chapter 2
EXERCISE ANSWERS
Exam Preparation Exercises
2. a. XYZ Invalid—must end with 1, 2, or 3
b. 123 Invalid—must start with X, Y, or Z
c. Xl Valid
d. 23Y Invalid—must start with X, Y, or Z
e. XY12 Valid
f. Y2Y Invalid—must end with 1, 2, or 3
g. ZY2 Valid
h. XY23Xl Valid
5. False. Reserved words are just that—reserved for use by C++. They may not be used as variable
names.
6. False. C++ is a caseāˆ’sensitive language. The word main must be entirely in lowercase letters.
9. One character
10. Zero. The null string is an empty string.
12. True
13. The identifier computer is used to name something, such as a variable. In contrast, "computer"
is a literal string made up of the characters ā€˜c’, ā€˜o’, ā€˜m’, ā€˜p’, ā€˜u’, ā€˜t’, ā€˜e’, and ā€˜r’.
15. Below, the line numbers do not include blank lines.
Line 2: The ā€œ>ā€ is missing after <iostream.
After this line, two more lines are missing:
#include <string>
using namespace std;
Lines 3, 4, 5: ā€œconstantā€ should be ā€œconstā€.
ā€œ:ā€ should be ā€œ=ā€.
Each literal string should be surrounded by double quotes.
Line 5: The terminating ā€œ;ā€ is missing.
Line 6: ā€œint mainā€ should be ā€œint main()ā€.
Line 9: ā€œcharacterā€ should be ā€œcharā€.
Line 10: Martin, Luther, and King are undeclared identifiers. (The statement should be
name = FIRST + MID + LAST;)
Line 11: A string expression (MID) cannot be stored into a char variable (initial).
Line 12: A constant (LAST) cannot be assigned a new value.
Line 13: ā€œcountā€ should be ā€œcoutā€.
The literal string should be surrounded by double quotes, not single quotes.
Line 14: mid is an undeclared identifier. It should be MID.
The terminating ā€œ;ā€ is missing.
After line 15: The main function is missing the terminating ā€œ}ā€.
Programming Warmāˆ’up Exercises
1. cout << "student’s name";
4. cout << "Value of make: " << make << endl;
cout << "Value of model: " << model << endl;
cout << "Value of color: " << color << endl;
1
pf3

Partial preview of the text

Download C++ Programming Exercises and Answers: Chapter 2 and more Study notes Engineering in PDF only on Docsity!

Chapter 2

EXERCISE ANSWERS

Exam Preparation Exercises

  1. a. XYZ Invalid—must end with 1, 2, or 3 b. 123 Invalid—must start with X, Y, or Z c. Xl Valid d. 23Y Invalid—must start with X, Y, or Z e. XY12 Valid f. Y2Y Invalid—must end with 1, 2, or 3 g. ZY2 Valid h. XY23Xl Valid
  2. False. Reserved words are just that—reserved for use by C++. They may not be used as variable names.
  3. False. C++ is a caseāˆ’sensitive language. The word main must be entirely in lowercase letters.
  4. One character
  5. Zero. The null string is an empty string.
  6. True
  7. The identifier computer is used to name something, such as a variable. In contrast, "computer" is a literal string made up of the characters ā€˜c’, ā€˜o’, ā€˜m’, ā€˜p’, ā€˜u’, ā€˜t’, ā€˜e’, and ā€˜r’.
  8. Below, the line numbers do not include blank lines.

Line 2: The ā€œ>ā€ is missing after <iostream. After this line, two more lines are missing: #include using namespace std; Lines 3, 4, 5: ā€œconstantā€ should be ā€œconstā€. ā€œ:ā€ should be ā€œ=ā€. Each literal string should be surrounded by double quotes. Line 5: The terminating ā€œ;ā€ is missing. Line 6: ā€œint mainā€ should be ā€œint main()ā€. Line 9: ā€œcharacterā€ should be ā€œcharā€. Line 10: Martin, Luther, and King are undeclared identifiers. (The statement should be name = FIRST + MID + LAST;) Line 11: A string expression (MID) cannot be stored into a char variable (initial). Line 12: A constant (LAST) cannot be assigned a new value. Line 13: ā€œcountā€ should be ā€œcoutā€. The literal string should be surrounded by double quotes, not single quotes. Line 14: mid is an undeclared identifier. It should be MID. The terminating ā€œ;ā€ is missing. After line 15: The main function is missing the terminating ā€œ}ā€.

Programming Warmāˆ’up Exercises

  1. cout << " student’s name ";
  2. cout << "Value of make: " << make << endl; cout << "Value of model: " << model << endl; cout << "Value of color: " << color << endl;

cout << endl; cout << "Value of plateType: " << plateType << endl; cout << "Value of classification: " << classification << endl;

// PrintName program // This program prints a name in three different formats //**************************************************************** #include #include

using namespace std;

const string FIRST = "Herman"; // Person’s first name const string LAST = "Smith"; // Person’s last name const string MIDDLE = "G"; // Person’s middle initial

int main() { string firstLast; // Name in firstāˆ’last format string lastFirst; // Name in lastāˆ’first format string firstMidLast; // Name in firstāˆ’initialāˆ’last format

firstLast = FIRST + " " + LAST; cout << "Name in firstāˆ’last format is " << firstLast << endl;

lastFirst = LAST + ", " + FIRST + ", "; cout << "Name in lastāˆ’firstāˆ’initial format is "; cout << lastFirst << MIDDLE << ’.’ << endl;

firstMidLast = FIRST + " " + MIDDLE + ". " + LAST; cout << "Name in firstāˆ’initialāˆ’last format is " << firstMidLast << endl;

return 0; }

  1. Below, the answer to part (a) is written three different ways (and there are other possibilities as well). The answer to each remaining part is written as a single output statement but could be broken up into several output statements.

a. cout << "Four score" << endl; cout << "and seven years ago" << endl; or cout << "Four score" << endl << "and seven years ago" << endl; or cout << "Four score" << endl << "and seven years ago" << endl;

b. cout << "Four score" << endl << "and seven" << endl << "years ago" << endl;

c. cout << "Four score" << endl << "and" << endl << endl << "seven" << endl << "years ago" << endl;

d. cout << "Four" << endl << " score" << endl << " and" << endl << "seven" << endl << " years" << endl << " ago" << endl;

  1. This is a laboratory exercise for the student and requires no answer here.