Coding Practice¶
How long is a year on other planets? Let’s write a program that prints out the number of days
in a year on each planet using a switch statement. These values are, in planetary order,
88 days, 225 days, 365 days, 687 days, 4333 days, 10759 days, 30687 days, and 60190 days.
Print out this information in the following format: Planet planet has numDays number of days in
a year! Select the Parsonsprob tab for hints for the construction of the code.
How long is a year on other planets? Let’s write a program that prints out the number of days
in a year on each planet using a switch statement. These values are, in planetary order,
88 days, 225 days, 365 days, 687 days, 4333 days, 10759 days, 30687 days, and 60190 days.
Print out this information in the following format: Planet planet has numDays number of days in
a year! Use the lines to construct the code, then go back to complete the Activecode tab.
Now let’s generate a BingoBoard! We want to fill the 25 Spaces on the BingoBoard with
random values from 1 to 75 without repititon. To do this, we’ll make a vector
of numbers from 1 to 75 and shuffle it using the same method as shown in this chapter. Then
we will select the first 25 values for the 25 spaces on the BingoBoard. We will
do this entire process in multiple steps. First, write the function randomInt, which
generates a random value between low and high, inclusive. Be sure to include the relevant libraries!
Select the Parsonsprob tab for hints for the construction of the code.
Now let’s generate a BingoBoard! We want to fill the 25 Spaces on the BingoBoard with
random values from 1 to 75 without repititon. To do this, we’ll make a vector
of numbers from 1 to 75 and shuffle it using the same method as shown in this chapter. Then
we will select the first 25 values for the 25 spaces on the BingoBoard. We will
do this entire process in multiple steps. First, write the function randomInt, which
generates a random value between low and high, inclusive. Be sure to include the relevant libraries!
Use the lines to construct the code, then go back to complete the Activecode tab.
Now that we have the functions randomInt and swapValues, we can write the function
generateRandVec. generateRandVec creates a vector with values from 1 to 75,
shuffles it using randomInt and swapValues, and returns the shuffled vector.
Select the Parsonsprob tab for hints for the construction of the code.
Now that we have the functions randomInt and swapValues, we can write the function
generateRandVec. generateRandVec creates a vector with values from 1 to 75,
shuffles it using randomInt and swapValues, and returns the shuffled vector.
Use the lines to construct the code, then go back to complete the Activecode tab.
Let’s print out our BingoBoard! Write the BingoBoard member function
printBoard. Insert tabs between each value in each row to make the board
print out neater. Select the Parsonsprob tab for hints for the construction of the code.
Let’s print out our BingoBoard! Write the BingoBoard member function
printBoard. Insert tabs between each value in each row to make the board
print out neater. Use the lines to construct the code, then go back to complete the Activecode tab.
You may have noticed that in some cases, our version of bubbleSort does
an unnecessary amount of work. For example, if our vector was {1, 2, 3, 5, 4},
bubbleSort would swap 4 and 5, but then keep going even though our vector
is already in order! We can save some work by including a bool called is_changed.
If we swap values during a pass, we set is_changed to true. If nothing has been swapped,
then is_changed stays false, and we know to break out of the loop since our vector
is already sorted. Write the function fastBubbleSort, which is bubbleSort with this
modification. Select the Parsonsprob tab for hints for the construction of the code.
You may have noticed that in some cases, our version of bubbleSort does
an unnecessary amount of work. For example, if our vector was {1, 2, 3, 5, 4},
bubbleSort would swap 4 and 5, but then keep going even though our vector
is already in order! We can save some work by including a bool called is_changed.
If we swap values during a pass, we set is_changed to true. If nothing has been swapped,
then is_changed stays false, and we know to break out of the loop since our vector
is already sorted. Write the function fastBubbleSort, which is bubbleSort with this
modification. Use the lines to construct the code, then go back to complete the Activecode tab.