6.9. Activecode Exercises¶
Answer the following Activecode questions to assess what you have learned in this chapter.
Fix the code below so that it prints “THE TEAM” “THE TEAM” “THE TEAM” on three separate lines.
Below is one way to fix the program. Since we want “THE TEAM”
to print three times, we must check all three conditons. this
means changing the else if
statements to if
statements.
You are part of a class where everyone passes, but it’s very hard to pass with an A. Fix the function so it prints your letter grade according to this scheme. [0, 50) = C, [50, 85) = B, and [85, 100] = A.
Fix the infinite recursion in the code below. The function should not count any numbers after 10 (the highest numbers that should print are 9 or 10). When it is done counting, the function should print that.
Below is one way to fix the program. The infinite recursion
happens when we use an odd number as an argument. By checking
that a number is less than 99, the highest numbers to recurse
are 98 and 97. 98 + 2 == 100
and 97 + 2 == 99
, so we
never count past 100.
In the following question,
std::boolalpha
is an I/O manipulator that
replaces ‘falsy’ values witht he word false and ‘truthy’
expressions witht he word true.
Finish the code below so that it prints true if x
is even
and false if x
is odd.
Finish the code below so that the function will continue to ask for input until the user guesses the word correctly.
Below is one way to complete the program.
Write the function greater
that prints true
if the first double
argument is greater than the
second double
argument. Be sure to include any
necessary headers.
Write the function good_vibes
that prints “I’m having a mood
day!”
depending on the value of mood
. If mood
is “bad”, then the function
should not do anything since it’s good vibes only. Be sure to
include any necessary headers.
Below is one way to write the program. The return allows the function to exit if there are bad vibes in the room. Otherise, the function prints as directed.
Write the function exclusive_or
that prints true If
either a
OR b
is true, and prints false otherwise.
Be sure to include any necessary headers.
Write the function countdown
that takes a positive integer
and decrements it until eaching zero, printing the number at each
step of the way. Once it reaches zero, it should print “Blastoff!”
Below is one way to write the program.
Write the function print_negative
that asks the user
for a negative number. If the user does not provide a negative
number, it should contine asking until the user provides one.
It should then print the negative number.