13.14. Coding Practice¶
Create the enumerated type Planet, which maps the planets in our solar system to integers starting at 1. Make sure to list the planets out in order! (Sadly, Pluto is not a planet :( )
Below is one way to implement the program. The planets in our solar system are Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.
Loading a dynamic question ...
Selecting from: cp_13_AC_2q, cp_13_AC_2_pp
A Bingo board has 25 Spaces in a matrix-like grid. A Space has a number value randomly selected from 1 to 75 and can
either be filled or not. Write the struct definitions for Space
and BingoBoard
.
Below is one way to implement the program. We declare the Space
and BingoBoard
struct
and create the instance variables in order. Make sure to set is_filled
to false
!
Loading a dynamic question ...
Selecting from: cp_13_AC_4q, cp_13_AC_4_pp
Now we need a way to swap the values at two indices in a vector. Write the function swapValues
,
which takes a vector
of int
s and two indices as parameters.
Below is one way to implement the program. We store the value at index1
in a temp
variable, replace the value at index1
with the value at index2
, and then finally
replace the value at index2
with the value of temp
. Make sure to pass
vec
by reference!
Loading a dynamic question ...
Selecting from: cp_13_AC_6q, cp_13_AC_6_pp
We can now fill our BingoBoard
with values! Write the BingoBoard
member function makeBoard
. Use the generateRandVec
function and select the first 25 values to fill up the board. Make sure
to create a free space in the middle of the board! Set the value of the
free space to 0 and is_filled
to true
. All other
spaces should have is_filled
set to false
.
Below is one way to implement the program. First we need to initialize
the board to the correct dimensions. Then, we use generateRandVec
to create a vector
of random values from 1 to 75. Afterwards, we set
the values of the 25 Space
s to the first 25 values in the
random vector
. Lastly, we set the middle Space
to 0 and
set its is_filled
to true
.
Loading a dynamic question ...
Selecting from: cp_13_AC_8q, cp_13_AC_8_pp
Bubble sort is a method of sorting that involves repeatedly swapping the
adjacent elements if they are in the wrong order. For example, let’s say
we have the vector
with elements {3, 2, 4, 1}. On the first pass, we take
a look at the first two elements, 3 and 2. Since 3 is bigger than 2, we swap them.
Thus, the vector
now looks like {2, 3, 4, 1}. Next, we look at the next two
elements, 3 and 4. Since 3 is less than 4, we don’t swap. Lastly, we look at
the last two elements, 4 and 1. Since 4 is greater than 1, we swap the.
Thus the vector
now looks like {2, 3, 1, 4}. Now we restart and look at the
first two elements again and the process continues. This way, the biggest elements
“bubble” to the back. Write the function bubbleSort
,
which takes a vector
as a parameter and sorts it. Feel free to use the provided
swapValues
function.
Below is one way to implement the program. We must loop through all elements
in the vector. Since we know the last i
elements are already in place,
our inner loop only goes up to vec.size() - 1 - i
. If the next element
is greater than the current element, we swap the two elements.
Loading a dynamic question ...
Selecting from: cp_13_AC_10q, cp_13_AC_10_pp