Engr 111 ch 3.5 Numeric Expressions
What is x after executing the line x = 5; y = 9; y = x; x = y;
5 (y = x assigns y with 5. Then x = y assigns x with y's value, or 5. A proper swap requires a third variable: tmp = x; y = x; x = tmp;)
Multiple statements may be appear on a single line separated by commas and semicolons. Such statements will be evaluated from left to right. Thus, the line num = 2; val = 5; num = val + num; will end with num being ____
7
What is val after executing the line num = 6; num = num * 2; val = num + 1;
13 (num will assigned with 6 * 2 or 12. Then val will be assigned with 12 + 1 or 13.)
Determine how many cans of soda (variable: numCans) result in one pound of fat, given a can's Calories (variable: calPerCan). 1 pound of fat is 3500 Calories. (For the curious, if a can has 150 Calories, then the answer is 23.3 cans). Note: Uses the U.S. convention of a Calorie (capital C) being 1,000 calories (1 kcal).
numCans = 3500 / calPerCan;
Good practice involves using ________ to explicitly direct the desired order of operations rather than relying on precedence rules.
parentheses (t = d + (p * n) is better programming style than writing t = d + p * n even though both evaluate the same), except when the order is unimportant as in w = x + y + z.)
Determine the cost of driving 1 mile (variable: pricePerMile) given a car's miles-per-gallon (variable: milePerGallon), the price per gallon of gasoline (variable: pricePerGal), and adding 5 cents per mile for tire/oil/other maintenance. (Ex: at 25 miles-per-gallon and $4/gallon, the cost would be 21 cents/mile).
pricePerMile = (pricePerGal / milePerGallon) + 0.05;
What does a scalar refer to?
(a) single number
When using multiplication in an expression, which expression is correct, writing 1.6M or 1.6*M?
1.6*M
T or F: The following indicates the expression continues on the next line: x = a + b ..
False (That line ends with two periods. Three or more indicate continuation. The two periods is a syntax error.)
Yes or No: Does -4 ^ 2 equal -16? Note: This activity omits parentheses to test knowledge of precedence rules. But programmers should use parentheses to make evaluation order explicit.
Yes (^ has higher precedence than -, so -(4^2) is -(16).)
Some expressions may be too long to fit on a single line. A long expression can be continued on the next line by using a ________ , which is a sequence of three or more periods "..." at the end of a line.
continuation
An __________ is a combination of items like variables, constants, and operators, that results in a value.
expression
Write a statement that assigns finalResult with firstSample plus secondSample, divided by 3.
finalResult = (firstSample + secondSample) / 3
Convert kilometers (variable: kilometers) to miles (variable: miles), assuming 1.6 km/mi
miles = kilometers / 1.6
Compute the tip (variable: tip) to add to a restaurant bill (variable: bill), assuming a 15% tipping rate.
tip = 0.15 * bill