10 - 20 - 30
(10 - 20) - 30
10 - (20 - 30)
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/j
d/e
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"; }
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.
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; }
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
cout << a + 10 * b << "\n"
cout << 1 - 2 - 3 << "\n"
cout << a << b << c << "\n"
z = y = x = 10
cout << "\tX\n";
int i {16}; i += 2; cout << i << "\n";
int i {16}; i *= 2; cout << i << "\n";
int i {16}; i /= 2; cout << i << "\n";
int i {16}; ++i; cout << i << "\n";
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).
if ( ) { cout << i << " ends with a 0.\n"; } else { cout << i << " does not end with a 0.\n"; }
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.)
for
loops.
int n {4}; for (int i {0}; i < n; ++i) { cout << i << "\n"; }
int n {4}; for (int i {1}; i < n; ++i) { cout << i << "\n"; }
int n {4}; for (int i {0}; i <= n; ++i) { cout << i << "\n"; }
int n {4}; for (int i {1}; i <= n; ++i) { cout << i << "\n"; }
int n {4}; for (int i {1}; i <= n; ++i) { cout << i; } cout << "\n";
for (int i {0}; i < 3; ++i) { for (int j {0}; j < 2; ++j) { cout << "X"; } cout << "\n"; }
for (int i {0}; i < 10; ++i) { cout << i << "\n"; } cout << "After the loop is over, the value of i is " << i << "\n";
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";
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 |