Chapter 3 Expressions And Interactivity

5 September 2022
4.7 (114 reviews)
37 test answers

Unlock all answers in this set

Unlock answers (33)
question
The cin object can be used to read data typed at the keyboard. So far you have written programs with built-in data. These types of programs are limited to performing their task with only a single set of starting data. If you decide to change the initial value of any variable, the program must be modified and recompiled. 3.1
answer
In reality, most programs ask for values that will be assigned to variables. This means thevprogram does not have to be modified if the user wants to run it several times with different sets of data.
question
This question is known as a prompt, and it tells the user what data he or she should enter.
answer
Your program should always display a prompt before it uses cin to read input. This way, the user will know that he or she must type a value at the keyboard.
question
The >> symbol is the stream extraction operator. It gets characters from the stream object on its left and stores them in the variable whose name appears on its right.
answer
In this line, characters are taken from the cin object (which gets them from the keyboard) and are stored in the length variable.
question
Notice that the << and >> operators appear to point in the direction that data is flowing. In a statement that uses the cout object, the << operator always points toward cout. This indicates that data is flowing from a variable or a literal to the cout object.
answer
In a statement that uses the cin object, the >> operator always points toward the variable that is receiving the value. This indicates that data is flowing from cin to a variable
question
The cin object causes a program to wait until data is typed at the keyboard and the [Enter] key is pressed. No other lines in the program will be executed until cin gets its input. If the user enters a floating-point value for an integer variable,
answer
cin automatically converts the data read from the keyboard to the data type of the variable used to store it. If the user types 10, it is read as the characters '1' and '0'. cin is smart enough to know this will have to be converted to an int value before it is stored in the length variable. cin is also smart enough to know a value like 10.7 cannot be stored in an integer variable. You must INCLUDE the iostream file in any program that uses cin.
question
Entering Multiple Values
answer
cin >> length >> width;
question
When the user types values at the keyboard, those values are first stored in an area of memory known as the keyboard buffer.
answer
So, when the user enters the values 5.7, 4, and b, they are stored in the keyboard buffer 3.3
question
C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.
answer
An expression is a programming statement that has a value. Usually, an expression consists of an operator and its operands. Program 3.4
question
Precedence of Arithmetic Operators (Highest to Lowest)
answer
Level 1 - * / % Level 2 = + -
question
An operator's associativity is either left to right, or right to left. If two operators sharing an operand have the same precedence, they work according to their associativity.
answer
5 βˆ’ 3 + 2 Both the βˆ’ and + operators in this expression have the same precedence, and they have left to right ASSOCIAVITY
question
Grouping with Parentheses
answer
Parts of a mathematical expression may be grouped with parentheses to force some operations to be performed before others
question
Converting Algebraic Expressions to Programming Statements
answer
(3)(12) === 3 * 12 4 xy ==== 4 * x * y
question
No Exponents Please! Unlike many programming languages, C++ does not have an exponent operator. Raising a number to a power requires the use of a library function.
answer
Think of a library function as a "routine" that performs a specific operation. One of the library functions is called pow, and its purpose is to raise a number to a power. Here is an example of how it's used: area = pow(4.0, 2.0); Program 3.6
question
When an operator's operands are of different data types, C++ will automatically convert them to the same data type. This can affect the results of mathematical expressions.
answer
If an int is multiplied by a float, what data type will the result be? What if a double is divided by an unsigned int? Is there any way of predicting what will happen in these instances? The answer is yes. C++ follows a set of rules when performing mathematical operations on variables of different data types. It's helpful to understand these rules to prevent subtle errors from creeping into your programs.
question
Table 3-7 Data Type Ranking
answer
long double double float unsigned long long unsigned int int when an int and a long are the same size. In that case, an unsigned int outranks long because it can hold a higher value
question
When C++ is working with an operator, it strives to convert the operands to the same type. This automatic conversion is known as type coercion. When a value is converted to a higher data type, it is said to be promoted.
answer
To demote a value means to convert it to a lower data type. Let's look at the specific rules that govern the evaluation of mathematical expressions.
question
Rule 1: chars, shorts, and unsigned shorts are automatically promoted to int You will notice that char, short, and unsigned short do not appear in Table 3-7 .
answer
That's because anytime they are used in a mathematical expression, they are automatically promoted to an int. The only exception to this rule is when an unsigned short holds a value larger than can be held by an int. This can happen on systems where shorts are the same size as ints. In this case, the unsigned short is promoted to unsigned int.
question
Rule 2: When an operator works with two values of different data types, the lower-ranking value is promoted to the type of the higher-ranking value.
answer
In the following expression, assume that years is an int and interestRate is a float : years * interestRate Before the multiplication takes place, years will be promoted to a float.
question
Rule 3: When the final value of an expression is assigned to a variable, it will be converted to the data type of that variable.
answer
In the following statement, assume that area is a long int, while length and width are both int s: area = length * width; Since length and width are both ints, they will not be converted to any other data type. The result of the multiplication, however, will be converted to long so it can be stored in area. Watch out for situations where an expression results in a fractional value being assigned to an integer variable. Here is an example: int x, y = 4; float z = 2.7; x = y * z; In the expression y * z, y will be promoted to float and 10.8 will result from the multiplication. Since x is an integer, however, 10.8 will be truncated and 10 will be stored in x.
question
When you divide an integer by another integer in C++, the result is always an integer. If there is a remainder, it will be discarded. For example, in the following code, parts is assigned the value 2.0: double parts; parts = 15 / 6;
answer
Even though 15 divided by 6 is really 2.5, the .5 part of the result is discarded because we are dividing an integer by an integer. It doesn't matter that parts is declared as a double because the fractional part of the result is discarded before the assignment takes place. In order for a division operation to return a floating-point value, at least one of the operands must be of a floating-point data type. For example, the previous code could be written as: double parts; parts = 15.0 / 6;
question
When a variable is assigned a value that is too large or too small in range for that variable's data type, the variable overflows or underflows. Trouble can arise when a variable is being assigned a value that is too large for its type. Here is a statement where a, b, and c are all short integers: a = b * c;
answer
If b and c are set to values large enough, the multiplication will produce a number too big to be stored in a. To prepare for this, a should have been defined as an int, or a long int. When a variable is assigned a number that is too large for its data type, it overflows. Likewise, assigning a value that is too small for a variable causes it to underflow Program 3.7
question
When floating-point variables overflow or underflow, the results depend upon how the compiler is configured. Your system may produce programs that do any of the following:
answer
β€’ Produces an incorrect result and continues running. β€’ Prints an error message and immediately stops when either floating point overflow or underflow occurs. β€’ Prints an error message and immediately stops when floating point overflow occurs, but stores a 0 in the variable when it underflows. β€’ Gives you a choice of behaviors when overflow or underflow occurs.
question
Type casting allows you to perform manual data type conversion. A type cast expression lets you manually promote or demote a value. The general format of a type cast expression is static_cast< DataType >( Value )
answer
where Value is a variable or literal value that you wish to convert and DataType is the data type you wish to convert Value to. Program 3.9
question
Multiple assignment means to assign the same value to several variables with one statement.
answer
C++ allows you to assign a value to multiple variables at once. If a program has several variables, such as a, b, c, and d, and each variable needs to be assigned a value, such as 12, the following statement may be constructed: a = b = c = d = 12; The value 12 will be assigned to each variable listed in the statement
question
Combined Assignment Operators. Quite often, programs have assignment statements of the following form: number = number + 1;
answer
The expression on the right side of the assignment operator gives the value of number plus 1. The result is then assigned to number, replacing the value that was previously stored there.
question
More elaborate statements may be expressed with the combined assignment operators. Here is an example: result *= a + 5;
answer
In this statement, result is multiplied by the sum of a + 5. When constructing such statements, you must realize the precedence of the combined assignment operators is lower than that of the regular math operators. The statement above is equivalent to result = result * (a + 5);
question
The cout object provides ways to format data as it is being displayed. This affects the way data appears on the screen. The same data can be printed or displayed in several different ways. For example, all of the following numbers have the same value, although they look different: 720 720.0 720.00000000 7.2e+2 +720.0
answer
The way a value is printed is called its formatting. The cout object has a standard way of formatting variables of each data type. Sometimes, however, you need more control over the way data is displayed. Program 3.13
question
The setprecision Manipulator Floating-point values may be rounded to a number of significant digits, or precision, which is the total number of digits that appear before and after the decimal point.
answer
You can control the number of significant digits with which floating-point values are displayed by using the setprecision manipulator.
question
The fixed Manipulator. The setprecision manipulator can sometimes surprise you in an undesirable way.
answer
When the precision of a number is set to a lower value, numbers tend to be printed in scientific notation. Program 3-17
question
The showpoint Manipulator. By default, floating-point numbers are not displayed with trailing zeroes, and floating-point numbers that do not have a fractional part are not displayed with a decimal point. For example, look at the following code. double x = 123.4, y = 456.0; cout << setprecision(6) << x << endl; cout << y << endl;
answer
The cout statements will produce the following output. 123.4 456 With most compilers, trailing zeroes are displayed when the setprecision and fixed manipulators are used together.
question
Special functions exist for working with characters and string objects. Although it is possible to use cin with the >> operator to input strings, it can cause problems that you need to be aware of.
answer
When cin reads input, it passes over and ignores any leading whitespace characters (spaces, tabs, or line breaks). Once it comes to the first nonblank character and starts reading, it stops reading when it gets to the next whitespace character.
question
Inputting a Character. Sometimes you want to read only a single character of input. For example, some programs display a menu of items for the user to choose from. Often the selections are denoted by the letters A, B, C, and so forth.
answer
The user chooses an item from the menu by typing a character. The simplest way to read a single character is with cin and the >> operator
question
Random numbers are useful for lots of different programming tasks. The following are just a few examples: y = rand(); C++ library has a function, rand(), that you can use to generate random numbers. (The rand() function requires the cstdlib header file.) The number returned from the function is an int. Here is an example of its usage:
answer
β€’ Random numbers are commonly used in games. β€’ Random numbers are useful in simulation programs. In some simulations, the computer must randomly decide how a person, animal, insect, or other living being will behave. Formulas can be constructed in which a random number is used to determine various actions and events that take place in the program. β€’ Random numbers are useful in statistical programs that must randomly select data for analysis. β€’ Random numbers are commonly used in computer security to encrypt sensitive data.
question
Hand tracing is a debugging process where you pretend that you are the computer executing a program. You step through each of the program's statements one by one.
answer
As you look at a statement, you record the contents that each variable will have after the statement executes. This process is often helpful in finding mathematical mistakes and other logic errors.
question
cerr - Standard output stream for errors
answer
Object of class ostream that represents the standard error stream oriented to narrow characters (of type char). It corresponds to the C stream stderr.
question
cout - Standard output stream
answer
Object of class ostream that represents the standard output stream oriented to narrow characters (of type char). It corresponds to the C stream stdout.
question
clog - Standard output stream for logging
answer
Object of class ostream that represents the standard logging stream oriented to narrow characters (of type char). It corresponds, along with cerr, to the C stream stderr.