10 - 20 - 30The value is −40, because subtraction has left-to-right associativity.
(10 - 20) - 30The value is −40, because the operation in the parentheses is executed first.
10 - (20 - 30)The value is 20, because the operation in the parentheses is executed first.
int i {10}; int j {20}; double d {10.0}; double e {20.0};What would be the value of each of the following C++ expressions?
i/jThe value is 0, because the value is of data type
int
and an
int
cannot hold a fraction.
d/eThe value is 0.5, because the value is of data type
double
and a
double
can hold a fraction.
harris
and trump
?
if (harris <= trump) { cout << "Harris did not win.\n"; } if (harris >= trump) { cout << "Trump did not win.\n"; }
if (harris <= trump) { cout << "Harris did not win.\n"; } else { cout << "Trump did not win.\n"; }No. If the two variables hold the same value, then the two pieces of code produce different outputs.
dollars
,
of data type
int
,
containing the number of dollars you have.
(You must not create this variable:
it
already
exists.)
Write C++ code that will output an English sentence such as
the following, to show the value of the variable.
The English sentence should end with a period.
Please output one newline character after the sentence.
Output the newline without using
endl
.
You have 5 dollars.The code should show the value of the variable no matter what that value is. In particular, the code should show the value of the variable even if that value is not 5. In the sentence that you output, the last word must be singular (“dollar”) if the value of the variable
dollars
is 1,
plural (“dollars”) otherwise.
Be sure to output a space on both sides of the number: you don’t want to output something like
You have 5dollars.
if (dollars == 1) { cout << "You have " << dollars << " dollar.\n"; //singular } else { cout << "You have " << dollars << " dollars.\n"; //plural }Answer to the corresponding question in the first midterm.
ac239
,
jcrews1
,
lam2
,
kheslin2
,
jrn4
.
cout << dolars; if (dollars == 5) { cout << " dollars.\n"; } else { cout << " dollar.\n"; }
If (dollars > 1) { cout << "You have ___ " << dollars << "___ dollars." << "\n"; } else if (dollars == 1) { cout << "You have ___ " << dollars << "___ dollar." << "\n"; } else { cout << "You have no money." << "\n"; }
cout << "You have " << dollars << " dollars.\n";
int dollars cout << "You have 5 dollars." >>
if (dollars > 1) { cout << "You have " << dollars << " dollars.\n";} if (dollars == 1) { cout << "You have 1 dollar.\n";
if (dollar == 5) cout << "You have " << 5 dollars << " dollars.\n"; } else { cout << "You have " << 5 dollars << " dollars.\n";
if dollars (== 1) { cout << dollar. else cout << dollars.
double
,
named
a
,
b
,
c
,
d
.
(You do not need to create these variables: they already exist.)
if (b != 0.0 || d != 0.0) { cout << "It's okay to go ahead and perform both divisions.\n"; cout << "The quotients are " << a/b << " and " << c/d << ".\n"; } else { cerr << "Sorry, you can't divide by 0.\n"; return EXIT_FAILURE; }The
||
(“or”)
should have been
&&
(“and”).
hour
?
cout << "Good "; if (hour >= 12) { if (hour >= 12 + 5) { cout << "evening"; } else { cout << "afternoon"; } } else { cout << "morning"; } cout << ".\n";
cout << "Good "; if (hour < 12) { cout << "morning"; } else if (hour < 12 + 5) { cout << "afternoon"; } else { cout << "evening"; } cout << ".\n";
*
is number 1,
and the
+
is number 2.
a + 10 * b 2 1
-a * 10 - b 1 2 3 (Negation has higher precedence than multiplication.)
cout << a + 10 * b << "\n" 3 2 1 4
cout << 1 - 2 - 3 << "\n" 3 1 2 4
cout << a << b << c << "\n" 1 2 3 4 (Output has left-to-right associativity.)
z = y = x = 10 3 2 1 (Assignment has right-to-left associativity.)
cout << "\tX\n";3 characters: tab, uppercase X, newline.
int i {16}; i += 2; cout << i << "\n";
18
int i {16}; i *= 2; cout << i << "\n";
32
int i {16}; i /= 2; cout << i << "\n";
8
int i {16}; ++i; cout << i << "\n";
17
i
,
of data type
int
,
holding a positive number.
(You must not create this variable:
it
already
exists.)
In the following parentheses,
write a C++ expression whose value will be
true
if
i
is a number that ends with a 0 (such as 10, 20, 120, etc.),
false
if
i
is a number that does not end with a 0 (such as 11, 12, 21, 22, etc).
ordinal.C
for i % 10
.
if (i % 10 == 0) { cout << i << " ends with a 0.\n"; } else { cout << i << " does not end with a 0.\n"; }The following students got this question right:
did4
,
dg51
,
lam2
,
jsr1
.
if (int i {0}; i % == 0) { cout << i << ends with a 0.\n"; } else { cout << i << ends with a 0.\n"
if (i % 2 == 0)
if (i % 10)
If (i % == 0)
if (i >= (
m
,
of data type
int
,
containing the number of minutes that a job took.
(You must not create this variable:
it
already
exists.)
Define two more variables,
named
hours
and
minutes
,
of type
int
.
Initialize
hours
to the number of hours that this job took.
Initialize
minutes
to the number of remaining minutes that this job took.
(Hint: the initial value of
minutes
must be a number in the range 0 to 59 inclusive.)
int hours {m / 60}; int minutes {m % 60};Answer to the corresponding question in the first midterm.
ac239
,
dg51
,
lam2
,
sm56
.
Hours (m / 24) minutes (m / 60)
int hours {minutes / 60}; int minutes {minutes % 60};
int m {130}; int Hour {60} int min {30}
int hours = m / 60; //This is how they wrote C++ many years ago. int minutes = m % 60;
int hours {minutes / 60}; int minutes {minutes / 59};
int hours {m / 60} int minutes {m % 60}
int min hours {24} minutes {59}
int hours {minutes / 60}; int minutes {hours % 60};
for
loops.
int n {4}; for (int i {0}; i < n; ++i) { cout << i << "\n"; }
0 1 2 3
int n {4}; for (int i {1}; i < n; ++i) { cout << i << "\n"; }
1 2 3
int n {4}; for (int i {0}; i <= n; ++i) { cout << i << "\n"; }
0 1 2 3 4
int n {4}; for (int i {1}; i <= n; ++i) { cout << i << "\n"; }
1 2 3 4
int n {4}; for (int i {1}; i <= n; ++i) { cout << i; } cout << "\n";
1234
for (int i {0}; i < 3; ++i) { for (int j {0}; j < 2; ++j) { cout << "X"; } cout << "\n"; }
XX XX XXAnswer to the corresponding question in the first midterm.
for (int i {0}; i < 10; ++i) { cout << i << "\n"; } cout << "After the loop is over, the value of i is " << i << "\n";The variable
i
no longer exists (and therefore certainly no longer holds any value)
after the loop is over.
double money {100.00}; //Increase the money by 6 percent each year. for (int year {0}; year < 10; ++year) { money *= 1.06; } cout << "The money has grown to " << money << "\n";10 multiplications, because the
for
loop iterates 10 times.
Precedence | Operator | Description | Associativity |
---|---|---|---|
3 |
-a
|
negation (as in “negative 5”)
increment (i.e., “add 1”) |
right-to-left |
5 |
a*b
a/b
a%b
|
multiplication, division, and remainder | left-to-right |
6 |
a+b
a-b
|
addition, subtraction | left-to-right |
7 |
cout<<x
cin>>x
|
output, input | left-to-right |
9 |
a<b
a<=b
|
is less than, is less than or equal to,
is greater than, is greater than or equal to |
left-to-right |
10 |
a==b
a!=b
|
is equal to, is not equal to | left-to-right |
14 |
a&&b
|
and | left-to-right |
15 |
a||b
|
or | left-to-right |
16 |
a=b
|
assignment
(change the value of a variable) |
right-to-left |