Wrong answers I received are in yellow.
1 - 2 - 3
The value is −4,
because
-
(subtraction)
has left-to-right associativity
(line 6 in the table).
The answers I received included
-2
(1 - 2) - 3
The value is −4, because an operator in parentheses is executed before the ones outside the parentheses.
The answers I received included the following:
4
error due to closing parenthesis
2
1 - (2 - 3)
The value is 2, because an operator in parentheses is before the ones outside the parentheses.
The answers I received included the following:
0
2
*
is number 1,
and the
+
is number 2.
a + 10 * b 2 1 Two operators, at lines 5 and 6 in the table.
-a * 10 - b 1 2 3 Three operators, at lines 3, 5, and 6 in the table.Answers I received included the following:
* is 1, - is 2, - is 3
* is 1, - is 2
cout << a + 10 * b 3 2 1 Three operators, at lines 5, 6, and 7 in the table.
cout << 1 - 2 - 3 3 1 2 Three operators, with left-to-right associativity (line 6 in the table).
cout << a << b << c << "\n" 1 2 3 4 Four operators, with left-to-right associativity (line 7 in the table).
Answers I received included the following:
1. << out operator before comparison 2. comparison 3. c5 logical and 4. << output comparison after operator
z = y = x = 10 3 2 1 Three operators, with right-to-left associativity (line 16 in the table).Answers I received included the following:
= is 1, = is 2, = is 3
cout << "\n";
The answer is 1 character. (That character is the newline character).
Answers I receieved included
this is a newline character cout is "\n" because it's in quotes or until conditions for a for loop is met.
80 "because a line is 80 characters."
Zero
0
assignment.C
and
increment.C
.
int i {10}; i *= 2; //means i = i * 2; cout << i << "\n";
20
Answers I received included the following:
10 20 30 40 ...
int i {10}; = 1033; int i *= 2; cout << i << "\n"; The output will be 2066, as 1033 * 2 = 2066
int i {10}; i /= 2; //means i = i / 2; cout << i << "\n";
5
Answers I received included the following:
10 5 2.5 ...
int i {10}; = 18; i /= 2; cout << i << "\n"; The output will be 9, as 18/2 = 9
output = .20
int i {10}; ++i; //means i = i + 1; cout << i << "\n";
11
Answers I received included the following:
11, 12, 13, 14 ...
10 11 12 13 ...
int i {10}; = 10; ++i; cout << i << "\n"; The output will be 100, as the 10 is printed twice, which the isaline will be 10 * 10 = 100
1, 2, 3 4 5 6 7 8 9 10
i
,
of data type
int
.
In the following parentheses,
write a C++ expression whose value is
true
if
i
is even,
false
if
i
is odd.
if
inside a
for
loop,
product4.C
.
if (i % 2 == 0) { //if i divided by 2 has a remainder of 0 cout << i << " is even.\n"; } else { cout << i << " is odd.\n"; }Answers I received included the following:
if (1 => 1 * 2) {
if (int i {2}
if (i % != 2) {
if (n > 0) {
if (i % 2 = 0) {
if (i % 2 = 0) {
if (int i % 2 == 0) {
if (i {2} i * 2;) {
if (i / 2) {
if (i <= 10) {
pennies
,
of data type
int
,
containing the number of pennies you have.
Define two more variables,
named
dollars
and
cents
,
of type
int
.
Initialize
dollars
to the number of dollars that you have in this pile of pennies.
Initialize
cents
to the number of remaining cents.
(Hint: the initial value of
cents
must be a number in the range 0 to 99 inclusive.)
remainder.C
.
int dollars {pennies / 100}; int cents {pennies % 100};Answers I receieved included the following:
int dollars {0}; cin >> dollars; int cents {0}; cin >> cents;
int dollars {0}; int cents {0}; {(cents / 100) % = cents;} cout <<
for (int d {0} d <= n int cent {0} << 99
int dollars = pennies / 100; int cents = pennies % 100;
int pennies(0); int dollars {0}; pennies / 100; int cents {0}; pennies % 100;
double dollars {0.0}; double cents {0.0};
Dollar = Pennies / 100 cents = Pennies %
int dollars {20} int dollars {20} - int cents {99} int pennies {1}
Int Pennies {150} Int dollars {Pennies <= 100} Int Cents {Pennies < 50}
int pennies {100} int dollars (pennies / 100) int cents (pennies % 100)
int dollars {0}; int cents {0}; dollars = pennies / 100; cents = pennies % 100;
for
loops?
for2.C
.
int n {3}; for (int i {0}; i < n; ++i) { cout << i << "\n"; }
0 1 2
Answers I received included the following:
1, 2, 3
0 1 2 0 1 2 ....
return EXIT_SUCCESS
1, 2.
0 1 2 3
1 2
output = 1, 2
return n {0}
int n {3}; for (int i {1}; i < n; ++i) { cout << i << "\n"; }
1 2
Answers I received included the following:
1 2 1 2 1 2 ....
EXIT_SUCCESS
2.
1 2 3
2
output = 2,
return 3
int n {3}; for (int i {0}; i <= n; ++i) { cout << i << "\n"; }
0 1 2 3
Answers I received included the following:
1, 2, 3
0 1 2 3 0 1 2 3 ...
return EXIT_FAILURE
1, 2, 3
0 1 2 3 4
1 2 3
output = 1, 2, 3
n <= 0
int n {3}; for (int i {1}; i <= n; ++i) { cout << i << "\n"; }
1 2 3
Answers I receieved included the following:
1 2 3 1 2 3 ...
return EXIT_FAILURE
2, 3.
2 3
output = 2, 3
n <= 3
line.C
and
rectangle.C
.
for (int i {0}; i < 2; ++i) { for (int j {0}; j < 3; ++j) { cout << "X"; } cout << "\n"; }
XXX XXXAnswers I received included the following:
XX
X
X X
XXXX XXXX XXXX
XX XX XX
i
no longer exists at the time when you try to output its value.
See
for2.C
.
for (int i {0}; i < 10; ++i) { cout << i << "\n"; } cout << "After the loop is over, the value of i is " << i << "\n";
The answers I received included the following:
There is nothing wrong here, the loop will print 'X' 10 times, and once the loop finishes, it will print the final value of i, which will be 10. The output will be After the loop is over the value of i=10 XXXXXXXXXX
Nothing.
All good.
nothing wrong
EXIT_FAILURE
This cout [the last one] should be in the for loop. Because it's not the line wont be iterated with the rest of the loop. Only printing one value not 9
Everything appears in order.
money
is born holding unpredictable garbage.
When we try to output its value after the 10 multiplications,
it holds unpredictable garbage that is somewhat larger.
for
loop,
see
for2.C
.
*=
operator, see
assignment.C
again.
double money; //Increase the money by 6 percent each year, for 10 years. for (int year {0}; year < 10; ++year) { money *= 1.06; //means money = money * 1.06 } cout << "After 10 years, the money has grown to " << money << "\n";
The answers I received included the following:
The thing that goes wrong here is that it would count up to 9.
return EXIT_FAILURE was not used
because it will only go up to year 9.
There is no variable after the times sign so the computer inputs garbage.
The value of i will always be less than 10.
The amount of money won't be anything due due to the sentence being out of the for loop.
The problem is that the program will only run to 9 years. If you want 10 years, change the value to 11.
nothing goes wrong but the initialization of money is outdated and has garbage at first.
harris
and trump
?
harris
and
trump
hold the
same
value, then the two pieces of code will produce different outputs.
if (harris < trump) { cout << "Harris lost.\n"; } if (harris > trump) { cout << "Harris won.\n"; }
if (harris < trump) { cout << "Harris lost.\n"; } else { cout << "Harris won.\n"; }
Answers I receieved included
Yes.
Yes Yes
No same output
Yes!
dollars
,
of data type
int
,
containing the number of dollars you have.
Write C++ code that will output an English sentence such as
You have 1 dollar.or
You have 5 dollars.to show the value of this variable. In this sentence, the last word must be singular (“dollar”) if the value of the variable
dollar
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.As shown above, the English sentence should end with a period. Please output one newline character after the sentence. Output the newline without using
endl
.
if (dollars == 1) { cout << "You have " << dollars << " dollar.\n"; } else { cout << "You have " << dollars << " dollars.\n"; }
cout << "You have " << dollars; if (dollars == 1) { cout << " dollar.\n"; //singular } else { cout << " dollars.\n"; //plural }
cout << "You have " << dollars << " "; if (dollars == 1) { cout << "dollar"; //singular } else { cout << "dollars"; //plural } cout << ".\n"; //Period at end of sentence, followed by newline.
cout << "You have " << dollars << " dollar"; if (dollars != 1) { //is not equal to 1 cout << "s"; //Make the noun plural. } cout << ".\n"; //Period at end of sentence, followed by newline.
Answers I received included the following:
if (dollars == 1) { cout "1 dollar\n."; else { cout "5 dollars\n.";
cout << "dollar " << 5 << \n";
You have 5 dollars.\n;
dollar == 1 cout << You have " << dollar << "dollar.\n"; } else { cout << You have " << dollar << dollar.\n"; }
cout << "you have " << dollars << " dollars.\n";
int i {0}; cout << "you have " << i << "dollars.\n"; if i < 2; cout << "you have " << i << "dollar.\n";
cout << "you have " << dollars << else if cout << "dollar" >> cout << "you have " << dollars << if (double) cout << "dollars" >> cout << "you have " << dollars << "\n";
if (dollars == 1) { cout << "You have " << dollar >> "dollar"\n."; } if (dollar > 1) { cout << "You have << dollar >> "dollars"\n."; }
if (dollars : {1}; i > 1; ++i) cout << "You have __ dollars." << i << ".\n";
Int dollar {1} cout << "You have " << dollar << "__ dollar.\n" Int dollars {-5} cout << "you have " << dollars << "__ dollars.\n";
if (dollars = 1) { cout << You have 1 dollar.\n"; } else { cout << You have " << dollars << " dollars\n"; }
&&
(“and”)
should have been
||
(“or”).
See the
&&
in
and2.C
.
cout << "In what year were you born?\n"; int year {0}; cin >> year; if (!cin) { cerr << "Sorry, that wasn't a number.\n"; return EXIT_FAILURE; } if (year < 1924 && year > 2024) { cerr << "Sorry, you could not have been born then.\n"; return EXIT_FAILURE; }
The answers I received included:
The computer reads our C++ program from top to bottom. Not defining, declaring, and initializing the variable at the very top of our program will not allow our program to execute. Ans: No, this C++ program won't run. [Comment by teacher: What the student wrote was true in the language C, but is no longer true in the language C++.]
int year {0}; should have been declared first - and then the output cout <<
No else was used between if and if
Answers I receieved included
High performance application
A machine
list instructions
Answers I receieved included
It's a series of code that is built for a function.
hour
?
hour
that would cause the two pieces of code
to produce different outputs.)
if (hour >= 12) { if (hour >= 12 + 5) { cout << "Good evening.\n"; } else { cout << "Good afternoon.\n"; } } else { cout << "Good morning.\n"; }
if (hour < 12) { cout << "Good morning.\n"; } else if (hour < 12 + 5) { cout << "Good afternoon.\n"; } else { cout << "Good evening.\n"; }
Answers I received included
NO
NO same outputs
This output [box (b)] would not be the same as (a) due to the way the nested if statements are set up.
An excerpt from this table.
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 |