Variables, Constants, Arithmetic Operators, and

Assignment Statements

Overview

This tutorial begins with discussing the difference between variables, named constants, and literal constants. Once defined the discussion moves to how to properly define each. This includes selecting an appropriate name, data type, and initial value (for variables and named constants). In addition to defining variables and named constants, the tutorial covers how each type of data will be stored. For numeric data, the concept of type casting is covered, as well as using numeric data in calculations. After numeric data is covered, the concept of obtaining string data is covered with the use of the getline function. The tutorial concludes with the building of a console application.

Objectives

After completing the tutorial, the student will be able to:

• Distinguish between a variable, a named constant, and a literal constant
• Select an appropriate name, data type, and initial value for a memory location
• Explain how data is stored in memory
• Reserve and initialize a memory location
• Type cast data
• Use an assignment statement to assign data to a variable
• Include arithmetic operators in an expression
• Get string input using the getline function
• Create a console application

Lecture Notes

Variables

Variables were introduced in tutorial 3 to understand how to write the code from an algorithm. However it is important to understand all the details associated with variables. Remember that there are three parts to declaring a variable in C++. First determine the name of the variable, then the data type, and finally assign an initial value to it.

The first item coded is the data type, which is set using the C++ keyword that corresponds to the data type. A list of data types available in C++ is listed in figure 4-5 on page 116. Choosing an appropriate data type is important because it determines the type of data that can be stored by the variable. If storing a person’s name, you would choose the string data type. If storing a person’s age, you would choose the short data type. Note that the short data type is chosen vs. the int or long data types. This is because the short data type stores a maximum positive value of 32767, which will more than accommodate a person’s age. If the int or long data types were chosen, they would be able to accommodate the age, however because they take up more space internally (4 bytes vs. 2 bytes), storing the age would be very inefficient.

Secondly, we state the variable name. While most C++ programmers use lower case for names, if names are more than one word then the beginning of the second word is capitalized to stand out. Remember that in most programming languages that variable names can consist of letters, numbers, and maybe an underscore, however no punctuation characters or spaces. The actual rules for naming variables in C++ are as follows:

Rules for naming variables in C++

• The name must begin with a letter
• The name must contain only letters, numbers, and the underscore (no punctuation characters or spaces)
• The C++ compiler you are using determines the maximum number of characters in a name. In Microsoft Visual C++ 6.0, a name cannot be longer than 247 characters.
• The name cannot be a keyword (reserved word) because a keyword has a special meaning in C++. See page 136 for a list of keywords in C++.
• Names in C++ are case sensitive

Finally, after determining the data type and naming the variable, we assign as initial value to the variable after an equal sign. It is important to give the variable an initial value or garbage may result. The initial value assigned to a variable must be of the same type as the variable.

The entire syntax for declaring a variable in C++ would look like the following:

(Note that the variable declaration ends with a semicolon)

datatype variableName = initialValue;

An example of this is the following:

float length = 0.0;

Named Constants

A named constant is a memory location whose contents cannot change while the program is running. This is different from a variable in that the contents of a variable are permitted to change while the program is running. This is the major difference between named constants and variables. Named constants require a data type, constant name, and an initial value, similar to that of a variable. However, a named constant declaration begins with the keyword const. The syntax for declaring a named constant in C++ is as follows:

const datatype constantname = expression; (where expression would be the computed value of the constant)

Literal Constants

Unlike variables and named constants, literal constants are not memory locations. Rather, a literal constant is an item of data that can appear in a program instruction, and that can be stored in a memory location. Literal constants can have a data type that is numeric, character, or string. You typically initialize a memory location by assigning a literal constant to it.

A numeric literal constant is simply a number. Numeric literals can consist of numbers, a plus sign, a minus sign, a decimal point, and the letter e in uppercase or lowercase representing exponential notation. Numeric literal constants cannot contain a space, comma, or special characters such as a dollar sign or percent sign.

A character literal constant is a single character enclosed in single quotation marks. Character literal constants store data that could be stored in a char variable type. Please note that if you wish to store more than one character, you would not use a character literal constant, but would use a string literal constant.

A string literal constant is zero or more characters enclosed in double quotation marks. To avoid the possibility of a string variable initially containing garbage, many times a string variable is initialized to the empty string. The empty string is two double quotation marks side-by-side with no space between them. By initializing a string variable to the empty string, the programmer will have more predictable results and debugging will be made easier if necessary.

Implicit type conversion

When a program instructs the computer to initialize a memory location, the computer first compares the data type of the value assigned to the memory location with the with the data type of the memory location itself to verify that the value is appropriate for the memory location. If the value’s data type does not match that of the memory location’s data type, most programming languages use a process call implicit type conversion to convert the value to fit the memory location. However it is not always possible to convert a value to match the memory location’s data type. For example, using a string literal constant to initialize a memory location that can store only integers will produce an error during the compiling phase, because the compiler cannot convert a string literal constant to an integer.

Be aware that the C++ compiler might display the warning message ‘initializing’: truncation from ‘const double’ to ‘float’ when you initialize a float memory location to a number other than 0.0. This warning message appears because Visual C++ treats all numeric literal constants containing a decimal place as a double data type. When you assign a double number (which requires 8 bytes of memory – see chart on page 116) to a float memory location (which can only store 4 bytes – see chart on page 116), Visual C++ warns you that it may need to truncate part of the number in order to store it in the memory location. To avoid this warning message, a process called type casting can be used.

Type casting

Type casting (explicit type conversion) is the process of converting data from one type to another. You typecast an item of data by enclosing the data in parentheses, and then preceding it with the C++ keyword that represents the desired data type. The following example would type cast the double number 5.1 to a float data type: float(5.1).

Note that it is also possible to type cast an existing variable. However in doing so, the data stored within the variable is temporarily type cast to the desired data type, not permanently converted. For example in the statement salary = (float)money; pretend a variable called salary has already been declared to be of the type float, and the variable money has already been declared to be of the type int. Even though we need to convert (type cast) the value in money to be of the float type before storing it in the salary variable, we do not wish to change the type of the money variable. Therefore during the statement, the value stored in the money variable will be temporarily converted to the float type and stored in the salary variable, however after the statement is complete the money variable will contain the same int type data as before.

Assignment statement

The format of the assignment statement in C++ is variablename = expression. When an assignment statement is encountered in a program, the computer assigns the value (or outcome) of the expression appearing on the right hand side of the assignment operator (=) to the variable whose variablename appears on the left hand side of the assignment operator. The expression can include literal constants, named constants, variables, arithmetic operators, and functions. The value (or outcome) of the expression must match the data type of the variable to which it is assigned. C++ will attempt implicit type conversion, however like with initializing variables, it may be necessary to perform type casting when assigning values to variables.

It is important to note that a variable may only store one piece of data at a time. Therefore once a variable has been assigned one value, and you assign another one to it, the new value replaces the original value.

Arithmetic operations

Most programs require the computer to perform one or more arithmetic calculations. The computer is instructed to perform a calculation by writing an arithmetic expression that contains one or more arithmetic operators. As you can see in figure 4-15 on page 127, each arithmetic operator is associated with a precedence number, which controls the order in which the operation is performed in an expression. Note that you can always use parentheses, which override the normal order of precedence, because parentheses have the highest precedence of the operators.

If there is an occasion in an arithmetic expression in which two operators have the same priority, the leftmost operation will be performed first. When dealing with addition and multiplication the outcome will produce the same result, however with division the results may or may not be the same. For example take the arithmetic expression 4 / 2 / 4. If the first division is performed first (4 / 2), then that result (2.0) is divided by 4, the final result would be 0.5. Where, on the other hand, if the second division were executed first (2 / 4), then 4 were divided by that result (0.5), the final result would be 8.0. You can see that in this particular case the outcome is totally different. This is why it is important to understand that parentheses may help you formulated your equations properly.

The one operator that may be new to you would be the % (or modulus) operator. The modulus operator yields the whole number remainder of a division operation. For example if I would like to know the remainder after dividing 5 into 16, I would write the following: 16 % 5. While 5 goes into 16 three time, the remainder of the operation would be 1. Note that since the modulus operator produces a whole number (integer), you need to pay close attention to the types of variables being used and whether you need to perform any type casting of the results.

getline function

The cin stream can be used to obtain a string of characters from the keyboard. However, if the string of characters the user wishes to enter contains a space, then the cin stream will not work properly. This is due to the fact that the cin stream stops reading characters from the keyboard when a space is encountered. To obtain a string of characters from the keyboard that contains a space, the getline function can be used. The syntax for using the getline function is as follows: getline (object, variablename);

In the syntax listed above, the object currently being used is the keyboard, therefore cin is coded. (Note that in the future we could use an input file object when reading data from a file). The variablename is the name of the variable that we would like to place the data from the keyboard. An example that would read data from the keyboard into the variable address would be as follows: getline(cin,address);

Creating a console application

Beginning of page 141 of your textbook the author begins the process of creating a console application. Please follow along with this example on your computer. Be sure that you understand the terminology involved. Terms that you should understand at the end of this example would be project, workspace, source file, as well as the relationship between them.

Discussion Topics

• Discuss what happens when variables are not properly initialized. Show an example where when testing a program improperly, the error may not show up until after the program is in production.
• Discuss why certain features of C++ may be version and machine dependent.
• Discuss why naming conventions are important and why programmers should consistently follow them. Include in this discussion problems that could arise in naming variables if one convention is not followed.
• Discuss the various situations that could arise if the user were to enter more characters than a string variable could store. List the best and worst case possibilities.
• Discuss the various types of data that programs could request from the user. After obtaining a list, discuss the various statements that have been covered involving input of data. Then match the various types of data with the appropriate type of input method.
• Discuss with what could result if you were not to follow the precedence rules for evaluating arithmetic expressions when you create a program.
• Discuss why type casting should occur. Give examples where significant data could be lost if the programmer did not implement explicit type conversions. Make sure to include examples where an arithmetic expression contains both float and integer data.

• Research different C++ compilers for differences. They could look at features such as maximum length of variable names, number of bytes allocated to different data types, etc.
• Create a program using a single variable name keyed in differently (like case did not matter) and print it out for the class (do not build and execute the program). Perform different arithmetic operations with the names. Have the class trace through the program and they will see first-hand how easy it is to make mistakes if they do not follow defined naming conventions.
• With the above assignment actually build and execute the program. Poll the class about what they believe may or may not happen in both the build (compile) and execution stages. Once everyone has given their input, then actually build and execute the program to show them if they were correct. Discuss with the class why the outcomes occurred.
• Research the terms commutative and associative. Ask them to determine which of the arithmetic functions (addition, subtraction, multiplication, and division) are commutative and/or associative.
• Create an example project called EXPROJ and save it on a formatted floppy disk. Then have them create and key in a simple C++ source file called EXFILE that simply prints their names on the screen. Note that both the project and source code are on the floppy disk (not the hard drive). Once created have them build and execute the program. Have them discuss their results.
• Create the same example project and source file as listed above, except this time have them save it on their hard drive (or network drive space). Have them discuss their results vs. the previous process.

Technical Review

• Review with the class the concepts involving Microsoft Visual C++ 6.0. Specifically review the concepts of files, workspace, compiling, building, and executing that were covered in Tutorial 3. It is imperative that the student understands the environment before proceeding past this point in the text. In addition review the process of dealing with the student files and make sure all questions related to the process chosen are answered.
• Another point would be to review the debugging process involved. If you do not require the student to perform the debugging exercises on his/her own (as an assignment), please perform these in a classroom environment. This will enable them to see first-hand the process that must be followed in order to debug a program.

Solutions to Questions

1. c a memory location whose value can change 9. b a space enclosed in single

while the program is running quotation marks

2. d all of the above 10. c float rate = 0.0;

3. a int numItems = 0; 11. d all of the above

4. c amt_Sold 12. d none of the above

5. a float 13. b const float RATE = float(16.5);

6. c numeric 14. d 15

7. b initial = ‘H’; 15. b 6

8. c the number 0 16. d none of the above (6.0 is stored)

Solutions to Concept Lesson Exercises

1. float price = 0.0;
2. price = float(16.23);

3. float height = 0.0;
4. float width = 0.0;

height = float(4.5);

width = float(6.9);

5. int population = 0;
6. population = 60000;

7. char letter = ‘ ’;
8. letter = ‘A’;

9. const float TAXRATE = float(.15);
10. const int MAXPAY = 20;
11. const char INSURED = ‘Y’;
12. const string PROMPT = "Press any key to continue";
13. int beginStock = 0;
14. int purchased = 0;

int sold = 0;

int endStock = 0;

endStock = beginStock + purchased – sold;

15. (memory location names may vary)
16. float sales = 0.0;

float bonus = 0.0;

const float RATE = float(.05);

bonus = sales * RATE;

17. (memory location names may vary)
18. string name = "";

int hours = 0;

int payRate = 0;

int gross = 0;

gross = hours * payRate;

19. d. The number 57, which is the ASCII code for the character 9, appears in the DOS window.
20. e. The compiler issues an error message.

21. In this exercise, the student discovers his or her system’s memory requirements (in bytes) for each of the C++ data types listed in the lesson’s Figure 4-5. Sample memory requirements are:

char 1

short 2

int 4

long 4

float 4

double 8

long double 8

boolean 1

14.

 //T4ConE14.cpp - displays the total purchase price #include using namespace std; int main() { //declare variables float numCds = 0.0; float cdPrice = 0.0; float totalPrice = 0.0; //get user's input cout << "Number of CDs purchased: "; cin >> numCds; cout << "CD price: "; cin >> cdPrice; //calculate total price totalPrice = numCds * cdPrice; //display total price cout << "Total price: " << totalPrice << endl; return 0; } //end of main function

Solutions to Application Lesson Exercises

1. getline(cin, company);
2. getline(cin, bookTitle);
3. tax = taxRate * grossPay;
4. totalPrice = numItems * price;

5.a.

 Input Processing Output sales rate (.05) Processing items: none Algorithm: enter the sales calculate the bonus by multiplying the sales by the rate display the bonus bonus

b.

 sales rate bonus 330.50 .05 16.525

d.

 sales RATE bonus 0.0 330.50 .05 0.0 16.525
 //T4AppE05.cpp - displays a bonus #include using namespace std; int main() { //declare named constant and variables const float RATE = float(.05); float sales = 0.0; float bonus = 0.0; //enter input item cout << "Enter sales: "; cin >> sales; //calculate bonus bonus = sales * RATE; //display bonus cout << "Bonus: " << bonus << endl; return 0; } //end of main function

6.

a.

 Input Processing Output number Processing items: none Algorithm: enter the number calculate the number squared by multiplying the number by itself display the number squared number squared

b.

 number number squared 10 100

d.

 number numberSquared 0 10 0 100
 //T4AppE06.cpp - displays the result of squaring a number #include using namespace std; int main() { //declare variables int number = 0; int numberSquared = 0; //enter input item cout << "Enter number: "; cin >> number; //calculate number squared numberSquared = number * number; //display number squared cout << "Number squared: " << numberSquared << endl; return 0; } //end of main function

7.

a.

 Input Processing Output diameter price per foot pi (3.141593) Processing items: none Algorithm: enter the diameter and price per foot calculate the circumference by multiplying the diameter by pi calculate the total price by multiplying the circumference by the price per foot display the circumference and total price circumference total price

b.

 diameter price per foot pi circumference total price 36.5 2.35 3.141593 114.668 269.47

d.

 diameter pricePerFoot PI circumference totalPrice 0.0 36.5 0.0 2.35 3.141593 0.0 114.668 0.0 269.47
 //T4AppE07.cpp - displays the circumference and total price #include using namespace std; int main() { //declare named constant and variables float PI = float(3.141593); float diameter = 0.0; float pricePerFoot = 0.0; float circumference = 0.0; float totalPrice = 0.0; //enter input items cout << "Enter the diameter (in feet): "; cin >> diameter; cout << "Enter the price per foot: "; cin >> pricePerFoot; //calculate circumference and total price circumference = diameter * PI; totalPrice = circumference * pricePerFoot; //display output items cout << "Circumference: " << circumference << endl; cout << "Total price: " << totalPrice << endl; return 0; } //end of main function

8.

a.

 Input Processing Output begin amount number purchased number sold Processing items: none Algorithm: enter the begin amount, number purchased, and number sold calculate the end amount by adding the number purchased to the begin amount, and then subtracting the number sold display the end amount end amount

b.

 begin amount number purchased number sold end amount 5000 1000 3500 2500

d.

 begin purchased sold end 0 5000 0 1000 0 3500 0 2500
 //T4AppE08.cpp - displays the number of dishwashers in stock at the end of the month #include using namespace std; int main() { //declare variables int begin = 0; int purchased = 0; int sold = 0; int end = 0; //enter input items cout << "Enter the beginning amount: "; cin >> begin; cout << "Enter the number purchased: "; cin >> purchased; cout << "Enter the number sold: "; cin >> sold; //calculate the ending amount end = begin + purchased - sold; //display ending amount cout << "Ending amount: " << end << endl; return 0; } //end of main function

9.

a.

 Input Processing Output length width price per foot Processing items: none Algorithm: enter the length, width, and price per foot calculate the area by multiplying the length by the width calculate the total price by multiplying the area by the price per foot display the area and total price area total price

b.

 length width price per foot area total price 12.5 14.5 3.10 181.25 561.875

d.

 length width pricePerFoot area totalPrice 0.0 12.5 0.0 14.5 0.0 3.10 0.0 181.25 0.0 561.875
 //T4AppE09.cpp - displays the area and total price #include using namespace std; int main() { //declare variables float length = 0.0; float width = 0.0; float pricePerFoot = 0.0; float area = 0.0; float totalPrice = 0.0; //enter input items cout << "Enter the length: "; cin >> length; cout << "Enter the width: "; cin >> width; cout << "Enter the price per foot: "; cin >> pricePerFoot; //calculate the area and total price area = length * width; totalPrice = area * pricePerFoot; //display output items cout << "Area: " << area << endl; cout << "Total Price: " << totalPrice << endl; return 0; } //end of main function

10.

a.

 Input Processing Output length width depth Processing items: none Algorithm: enter the length, width, and depth calculate the volume by multiplying the length by the width, and then multiplying the result by the depth display the volume volume

b.

 length width depth volume 100 30.5 4 12200

d.

 length width depth volume 0.0 100 0.0 30.5 0.0 4 0.0 12200
 //T4AppE10.cpp - displays the volume of a rectangular pool #include using namespace std; int main() { //declare variables float length = 0.0; float width = 0.0; float depth = 0.0; float volume = 0.0; //enter input items cout << "Enter the length: "; cin >> length; cout << "Enter the width: "; cin >> width; cout << "Enter the depth: "; cin >> depth; //calculate the volume volume = length * width * depth; //display the volume cout << "Volume: " << volume << endl; return 0; } //end of main function

11.

a.

 Input Processing Output name hours worked Processing items: remaining hours Algorithm: enter the name and hours worked calculate the weeks by dividing the hours worked by 40 calculate the remaining hours by finding the remainder of the hours worked divided by 40 calculate the number of days worked by dividing the remaining hours by 8 calculate the number of hours worked by finding the remainder of the remaining hours divided by 8 display the name, weeks, days, and hours name weeks days hours

b.

 name hours worked remaining hours weeks days hours Mary Claire Jackie Smith Sue Jones 88 111 12 8 31 12 2 2 0 1 3 1 0 7 4

d.

 name hoursWorked remainHours weeks days hours Mary Claire Jackie Smith Sue Jones 0 88 0 111 0 12 0 8 0 31 0 12 0 2 0 2 0 0 0 1 0 3 0 1 0 0 0 7 0 4

 //T4AppE11.cpp - displays the number of weeks, days, and hours worked #include #include using namespace std; int main() { //declare variables string name = ""; int hoursWorked = 0; int remainHours = 0; int weeks = 0; int days = 0; int hours = 0; //enter input items cout << "Enter the name: "; getline(cin, name); cout << "Enter the hours worked: "; cin >> hoursWorked; //calculate the weeks, days, and hours weeks = hoursWorked / 40; remainHours = hoursWorked % 40; days = remainHours / 8; hours = remainHours % 8; //display the output items cout << "Name: " << name << endl; cout << "Weeks: " << weeks << endl; cout << "Days: " << days << endl; cout << "Hours: " << hours << endl; return 0; } //end of main function

12.

a.

 Input Processing Output item quantity units per box Processing items: none Algorithm: enter the item, quantity, and units per box calculate the full boxes packed by dividing the quantity by the units per box calculate the remaining items by finding the remainder of the full boxes packed divided by the units per box display the item, full boxes packed, and remaining items item full boxes packed remaining items

b.

 item quantity units per box full boxes packed remaining items Cleanser Hair Spray Comb 45 100 78 6 3 5 7 33 15 3 1 3

d.

 item quantity unitsPerBox fullBoxes remainItems Cleanser Hair Spray Comb 0 45 0 100 0 78 0 6 0 3 0 5 0 7 0 33 0 15 0 3 0 1 0 3

 //T4AppE12.cpp - displays the number of full boxes that can be packed and the number of items remaining #include #include using namespace std; int main() { //declare variables string item = ""; int quantity = 0; int unitsPerBox = 0; int fullBoxes = 0; int remainItems = 0; //enter input items cout << "Enter the item name: "; getline(cin, item); cout << "Enter the quantity in inventory: "; cin >> quantity; cout << "Enter the number of items that can be packed in a box: "; cin >> unitsPerBox; //calculate the number of full boxes and the number of items remaining fullBoxes = quantity / unitsPerBox; remainItems = quantity % fullBoxes; //display the output items cout << "Item: " << item << endl; cout << "Full boxes: " << fullBoxes << endl; cout << "Items remaining: " << remainItems << endl; return 0; } //end of main function

13.

a.

 Input Processing Output original pennies Processing items: leftover Algorithm: enter the original pennies calculate the dollars by dividing the original pennies by 100 calculate the leftover by finding the remainder of dividing the original pennies by 100 calculate the quarters by dividing the leftover by 25 calculate the leftover by finding the remainder of dividing the leftover by 25 calculate the dimes by dividing the leftover by 10 calculate the leftover by finding the remainder of dividing the leftover by 10 calculate the nickels by dividing the leftover by 5 calculate the pennies by finding the remainder of dividing the leftover by 5 display the dollars, quarters, dimes, nickels, and pennies dollars quarters dimes nickels pennies

b.

 original pennies leftover dollars quarters dimes nickels pennies 2311 7333 11 11 1 33 8 8 23 73 0 1 1 0 0 1 1 3

d.

 origPennies leftover dollars quarters dimes nickels pennies 0 2311 0 7333 0 11 11 1 0 33 8 8 0 23 0 73 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 3

 //T4AppE13.cpp - displays the number of dollars, quarters, dimes, nickels, and pennies #include #include using namespace std; int main() { //declare variables int origPennies = 0; int leftover = 0; int dollars = 0; int quarters = 0; int dimes = 0; int nickels = 0; int pennies = 0; //enter input item cout << "Enter the number of pennies: "; cin >> origPennies; //calculate the number of dollars, quarters, dimes, nickels, and pennies dollars = origPennies / 100; leftover = origPennies % 100; quarters = leftover / 25; leftover = leftover % 25; dimes = leftover / 10; leftover = leftover % 10; nickels = leftover / 5; pennies = leftover % 5; //display the output items cout << "Dollars: " << dollars << endl; cout << "Quarters: " << quarters << endl; cout << "Dimes: " << dimes << endl; cout << "Nickels: " << nickels << endl; cout << "Pennies: " << pennies << endl; return 0; } //end of main function

14.

a.

 Input Processing Output owes paid Processing items: leftover Algorithm: enter the owes and paid calculate the change by subtracting the owes from the paid calculate the leftover by multiplying the change by 100 calculate the dollars by dividing the leftover by 100 calculate the leftover by multiplying the dollars by 100, and then subtracting the result from the leftover calculate the quarters by dividing the leftover by 25 calculate the leftover by multiplying the quarters by 25, and then subtracting the result from the leftover calculate the dimes by dividing the leftover by 10 calculate the leftover by multiplying the dimes by 10, and then subtracting the result from the leftover calculate the nickels by dividing the leftover by 5 calculate the pennies by multiplying the nickels by 5, and then subtracting the result from the leftover display the change, dollars, quarters, dimes, nickels, and pennies change dollars quarters dimes nickels pennies

b.

 owes paid leftover change dollars quarters dimes nickels pennies 75.34 39.67 45.55 80.00 50.00 45.55 466 66 16 6 1033 33 8 8 0 0 0 0 4.66 10.33 0 4 10 0 2 1 0 1 0 0 1 1 0 1 3 0

d.

 owes paid leftover change dollars quarters dimes nickels pennies 0.0 75.34 0.0 39.67 0.0 45.55 0.0 80.00 0.0 50.00 0.0 45.55 0 466 66 16 6 0 1033 33 8 8 0 0 0 0 0 0.0 4.66 0.0 10.33 0.0 0.0 0 4 0 10 0 0 0 2 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 3 0 0

 //T4AppE14.cpp - displays the change and the number of dollars, quarters, dimes, nickels, and pennies #include #include using namespace std; int main() { //declare variables float owes = 0.0; float paid = 0.0; float change = 0.0; int leftover = 0; int dollars = 0; int quarters = 0; int dimes = 0; int nickels = 0; int pennies = 0; //enter input items cout << "Enter the amount owed: "; cin >> owes; cout << "Enter the amount paid: "; cin >> paid; //calculate the change and the number of dollars, quarters, dimes, nickels, and pennies change = (paid - owes); leftover = change * 100; dollars = leftover / 100; leftover = leftover - dollars * 100; quarters = leftover / 25; leftover = leftover - quarters * 25; dimes = leftover / 10; leftover = leftover - dimes * 10; nickels = leftover / 5; pennies = leftover - nickels * 5; //display the output items cout << "Change: " << change << endl; cout << "Dollars: " << dollars << endl; cout << "Quarters: " << quarters << endl; cout << "Dimes: " << dimes << endl; cout << "Nickels: " << nickels << endl; cout << "Pennies: " << pennies << endl; return 0; } //end of main function

15. a. 0

1. 1
2. 0
3. 1
 //T4AppE15.cpp - demonstrates the bool data type #include using namespace std; int main() { //declare and initialize variable bool insured = 1; //display value in variable cout << "The value in the insured variable is: " << insured << endl; return 0; } //end of main function