H W ( 2.1-2.80
Given a non-negative number x, which yields a number in the range 5 - 10? x % 5 x % 10 x % 11 (x % 6) + 5 2) Given a non-negative number x, which expression has the range -10 to 10? x % -10 (x % 21) - 10 (x % 20) - 10 3) Which gets the tens digit of x. Ex: If x = 693, which yields 9? x % 10 x % 100 (x / 10) % 10 4) Given a 16-digit credit card number stored in x, which gets the last (rightmost) four digits? (Assume the fourth digit from the right is non-zero). x / 10000 x % 10000
(x % 6) + 5 (x % 21) - 10 (x / 10) % 10 x % 10000
(5 + 10 + 15) * (1 / 3)
0
100 * (1 / 2)
0
4 / 9
0
50 % 2
0
51 % 2
1
10 / 4.0
2.5
Select the expression whose parentheses match the evaluation order of the original expression. 1) y + 2 * z (y + 2) * z y + (2 * z) 2) z / 2-x (z / 2) - x z / (2 - x) 3) x * y * z (x * y) * z x * (y * z) 4) x + 1 * y/2 ((x + 1) * y) / 2 x + ((1 * y) / 2) x+ ( 1 * (y / 2)) 5) x / 2 + y / 2 ((x / 2) + y) / 2 (x / 2) + (y / 2) 6) What is totCount after executing the following? numItems = 5; totCount = 1 + (2 * numItems) * 4;
y + (2 * z) (z / 2) - x (x * y) * z x + ((1 * y) / 2) (x / 2) + (y / 2) 41
Is the following an error? Suppose an int's maximum value is 2,147,483,647. numYears = 1,999,999,999;
yes
Indicate which are valid expressions. x and y are variables, and are the only available variables. 1) x + 1 2) 2 * (x - y) 3) x 4) 2 5) 2x 6) 2 + (xy) 7) x - -2
1-4 Valid 5-6 Not valid 7 Valid
If the user enters 10, what will userAgeYears be assigned?
10
numAtoms is initially 7. What is numAtoms after: numAtoms += 5?
12
numAtoms is initially 7. What is numAtoms after: numAtoms *= 2?
14
x / y where int x = 10 and int y = 4.
2
x / y where int x = 10 and double y = 4.0
2.5
z = pow(2.0, pow(2.0, 3.0));
256.0
x = -9.0; z = sqrt(fabs(x));
3.0
x = 9.0; z = pow(sqrt(x) + sqrt(x), 2.0);
36.0
If the user enters 10, what is userAgeDays assigned?
3650
13 / 3
4
Determine the result. 1) 13.0 / 3.0 4 4.333333 Positive infinity 2) 0.0 / 5.0 0.0 Positive infinity Negative infinity 3) 12.0 / 0.0 12.0 Positive infinity Negative infinity 4) 0.0 / 0.0 0.0 Infinity Not a number
4.333333 0.0 Positive infinity Not a number
100 * 1 / 2
50
100 / 2
50
596 % 10
6
sqrt(36.0) evaluates to _____ . 2) What is y? y = sqrt(81.0); 3) What is y?y = pow(2.0, 8.0); 4) Is this a valid function call?y = sqrt(2.0, 8.0); 5) Is this a valid function call?y = pow(8.0); 6) If w and x are double variables, is this a valid function call? y = pow(w, x); 7) What is y? w = 3.0; y = pow(w + 1.0, 2.0);
6.0 9.0 256.0 no no yes 16.0
78 % 10
8
If initMass is 10.0, growthRate is 1.0 (100%), and yearsGrow is 3, what is finalMass? finalMass = initMass * pow(1.0 + growthRate, yearsGrow);
80.0
runtime error,
A divide-by-zero error is an example of a runtime error, a severe error that occurs at runtime and causes a program to terminate early.
divide-by-zero error
A divide-by-zero error occurs at runtime if a divisor is 0, causing a program to terminate.
double
A double variable stores a floating-point number. Ex: double milesTravel; declares a double variable.
floating-point literal
A floating-point literal is a number with a fractional part, even if the fraction is 0, as in 1.0, 0.0, or 99.573.
floating-point number
A floating-point number is a real number containing a decimal point that can appear anywhere (or "float") in the number. Ex: 98.6, 0.0001, or -55.667.
function
A function is a list of statements executed by invoking the function's name
literal
A literal is a specific value in code like 2.
Choosing a variable type (double vs. int)
A programmer should choose a variable's type based on the type of value held. Integer variables are typically used for values that are counted, like 42 cars, 10 pizzas, or -95 days. Floating-point variables are typically used for measurements, like 98.6 degrees, 0.00001 meters, or -55.667 degrees. Floating-point variables are also used when dealing with fractions of countable items, such as the average number of cars per household.
unary minus.
An exception is minus used as negative, as in: xCoord = -yCoord. Minus (-) used as negative is known as unary minus.
evaluates
An expression evaluates to a value, which replaces the expression.
expression
An expression is any individual item or combination of items, like variables, literals, operators, and parentheses, that evaluates to a value, like 2 * (x + 1).
precedence rules
An expression is evaluated using the order of standard mathematics, such order known in programming as precedence rules, listed below.
constant variable
An initialized variable whose value cannot change is called a constant variable.
operator
An operator is a symbol that performs a built-in calculation, like +, which performs addition. Common programming operators are shown below.
arguments
Any function input values, or arguments, appear within ( ), separated by commas if more than one.
Floating-point division by zero
Dividing a nonzero floating-point number by zero results in infinity or -infinity, depending on the signs of the operands. Printing a floating-point variable that holds infinity or -infinity outputs inf or -inf.
Incremental development
Incremental development is the process of writing, compiling, and testing a small amount of code, then writing, compiling, and testing a small amount more (an incremental amount), and so on.
Modulo (%)
The modulo operator (%) evaluates the remainder of the division of two integer operands. Ex: 23 % 10 is 3.
Manipulating floating-point output
The syntax for outputting the double myFloat with two digits after the decimal point iscout << fixed << setprecision (2) << myFloat;
Division: Integer rounding
When the operands of / are integers, the operator performs integer division, which does not generate any fraction.
Does the expression correctly capture the intended behavior? 1) 6 plus numItems: 6 + numItems 2) 6 times numItems:6 x numItems 3) totDays divided by 12:totDays / 12 4) 5 times i:5i 5) The negative of userVal:-userVal 6) n factorialn!
Yes No yes no yes no
Assign ballRadius with ballHeight multiplied by one half, namely (1.0 / 2.0). Use the parentheses around the fraction.
ballRadius = ballHeight * (1.0 / 2.0); or ballRadius = (1.0 / 2.0) * ballHeight;
Assign ballRadius with ballHeight divided by 2.0. Do not use the fraction 1.0 / 2.0; instead, divide ballHeight directly by 2.0.
ballRadius = ballHeight / 2.0;
Rewrite the statement using a compound operator. If the statement can't be rewritten using a compound operator, type: Not possible
carCount /= 2;
Which statement best declares and initializes the double variable? double currHumidity = 99%; double currHumidity = 99.0; double currHumidity = 99; 2) Which statement best assigns the variable? Both variables are of type double. cityRainfall = measuredRain - 5; cityRainfall = measuredRain - 5.0; 3) Which statement best assigns the variable? cityRainfall is of type double. cityRainfall = .97; cityRainfall = 0.97;
double currHumidity = 99.0; cityRainfall = measuredRain - 5.0; cityRainfall = 0.97;
Declare a double variable named packageWeight and initialize the variable to 7.1.
double packageWeight = 7.1;
Declare a double variable named personHeight.
double personHeight;
100 / (1 / 2)
error
x = 2; y = 5; z = 1 / (y - x - 3);
error
100.0 % 40
error % is only defined for integer operands; "remainder" makes no sense if floating-point division is performed. 100.0 / 40 is 2.5; no remainder exists because a fraction was generated. In contrast, 100 % 40 is 20 (100 / 40 is 2 remainder 20).
100 % (1 / 2)
error 1 / 2 is 0. 100 % 0 is undefined (as 100 / 0 is undefined) and causes the program to terminate.
Which of the following statements are valid declarations and uses of a constant integer variable named STEP_SIZE? Assume that variables totalStepHeight and numSteps have previously been declared as integers. 1) int STEP_SIZE = 5; 2) const int STEP_SIZE = 14; 3) totalStepHeight = numSteps * STEP_SIZE; 4) STEP_SIZE = STEP_SIZE + 1;
false 2-4 true
Which manipulator(s) is/are used to set cout to output two digits after the decimal point? cout << _____ << (7.0 / 3.0); setprecision(2) fixed fixed << setprecision(2) 2) What is output by cout << fixed << setprecision(1) << 0.125;? 0 0.1 0.13 3) What is output by cout << fixed << setprecision(3) << 9.1357;? 9.136 9.135 9.14
fixed << setprecision(2) 0.1 9.136
Choose the best type for a variable to represent each item. 1) The number of cars in a parking lot. 2) The current temperature in Celsius. 3) A person's height in centimeters. 4) The number of hairs on a person's head. 5) The average number of kids per household. double int
int 2-3double int double
Rewrite the statement using a compound operator. If the statement can't be rewritten using a compound operator, type: Not possible numItems = boxCount + 1;
not possible
compound operators
provide a shorthand way to update a variable, such as userAge += 1 being shorthand for userAge = userAge + 1.
Select the three statements needed to calculate the value of x in the following:x=y2+z2For this exercise, calculate y2 before z2. 1) First statement is: temp1 = pow(x , 2.0); temp1 = pow(z , 3.0); temp1 = pow(y , 2.0); temp1 = sqrt(y); 2) Second statement is: temp2 = sqrt(x , 2.0); temp2 = pow(z , 2.0); temp2 = pow(z); temp2 = x + sqrt(temp1 + temp2); 3) Third statement is: temp2 = sqrt(temp1 + temp2); x = pow(temp1 + temp2, 2.0); x = sqrt(temp1) + temp2; x = sqrt(temp1 + temp2);
temp1 = pow(y , 2.0); temp2 = pow(z , 2.0); x = sqrt(temp1 + temp2);
A florist wants to create as many bunches of 12 flowers as possible. totalFlowers holds the total number of flowers available. 1) Complete the statement to assign numBunches with the maximum number of bunches that can be made. numBunches =_____________ ; 2) Using only the variable totalFlowers, complete the statement to assign remainingFlowers with the number of remaining flowers after creating as many bunches of 12 as possible. remainingFlowers = _______________;
totalFlowers / 12 totalFlowers % 12
The expression (userAgeYears / 4) assumes a leap year occurs every four years? 2) The statement userAgeDays = userAgeDays + (userAgeYears / 4); requires parentheses to evaluate correctly. 3) If the user enters 20, what is userAgeDays after the first assignment statement? 7300 7305 4) If the user enters 20, what is userAgeDays after the second assignment statement? 7300 7305
true false 7300. The first assignment statement assigns userAgeDays with 20 * 365 or 7300. 7305 The second assignment statement assigns userAgeDays with 7300 + (20 / 4), which is 7300 + 5 or 7305.
Which variable is used for the user's age in years?
userAgeYears