Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
For this assignment
you will want to consult
the
C++ operator associativity/precedence table.
-
[10pts] For each of the below, circle the correctly
parenthesized version of the expression and circle
"precedence" or "associativity" as appropriate.
- x + y * z
evaluates as
(x + y) * z OR x + (y * z)
due to: precedence associativity
- x > y + z
evaluates as
(x > y) + z OR x > (y + z)
due to: precedence associativity
- cin >> x >> y
evaluates as
(cin >> x) >> y OR cin >> (x >> y)
due to: precedence associativity
- x = y = 1
evaluates as
(x = y) = 1 OR x = (y = 1)
due to: precedence associativity
- cin >> x = y
evaluates as
(cin >> x) = y OR cin >> (x = y)
due to: precedence associativity
-
[10pts]
Assume the following delcarations fill in the table.
New Note: each expression should be taken as independent. I.e. if one expression modifies some variable values, those modifications do not carry over to the next expression.
int n = 5;
double x = 1.5;
char c = 'R';
| expression | type | value |
n = 2 | | |
n == 2 | | |
c < 200 | | |
1 != x | | |
n*x | | |
- [5pts] Explain why
1 < x < 10
always returns true no matter what x is.
- [75pts]
A year is a leap year if it is evenly divisible by 4,
Except that years that are evenly divisible by 100
are not leap years unless they are evenly divisible by
400. Clear? For example:
| Year |
divisible by 4? |
divisible by 100? |
divisible by 400? |
Is leap year? |
| 703 | no | no | no | not leap year |
| 704 | yes | no | no | leap year |
| 700 | yes | yes | no | not leap year |
| 800 | yes | yes | yes | leap year |
Write a program that reads a year and prints out
(correctly!) Is Leap Year or Is Not Leap Year.
Here are two example runs.
$ ./hw
Enter year: 703
Is Not Leap Year
$ ./hw
Enter year: 704
Is Leap Year
Important: For those of you who already know about
&&, || or !, do not use them!
Turn in a printout of this cover sheet with your
answers to the questions, your source
code, and a screen capture of your program running each of the
four examples from the table above.