1.
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 :( )
planet
has numDays
number of days in a year! Check the hint below for help with the construction of the code.
planet
has numDays
number of days in a year! Use the lines to construct the code, then go back to complete the Activecode.
Space
and BingoBoard
.
Space
and BingoBoard
struct and create the instance variables in order. Make sure to set is_filled
to false
!
#include <iostream>
#include <vector>
using namespace std;
struct Space {
int value;
bool is_filled;
};
struct BingoBoard {
vector<vector<Space> > board;
};
BingoBoard
! We want to fill the 25 Space
s 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! Check the hint below for help with the construction of the code.
BingoBoard
! We want to fill the 25 Space
s 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.
swapValues
, which takes a vector
of int
s and two indices as parameters.
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!
#include <iostream>
#include <vector>
using namespace std;
void swapValues (vector<int> &vec, int index1, int index2) {
int temp = vec[index1];
vec[index1] = vec[index2];
vec[index2] = temp;
}
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
. Check the hint below for help with the construction of the code.
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.
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
.
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
.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <numeric>
using namespace std;
struct Space {
int value;
bool is_filled;
};
struct BingoBoard {
vector<vector<Space> > board;
void makeBoard ();
};
int randomInt (int low, int high);
void swapValues (vector<int> &vec, int index1, int index2);
vector<int> generateRandVec ();
void BingoBoard::makeBoard() {
// Initialize board
Space s = {0, false};
vector<Space> cols(5, s);
for (size_t i = 0; i < 5; ++i) {
board.push_back(cols);
}
// Fill board with random values
vector<int> vec = generateRandVec();
int count = 0;
for (size_t row = 0; row < board.size(); ++row) {
for (size_t col = 0; col < board[row].size(); ++col) {
board[row][col].value = vec[count];
++count;
}
}
// Create free space
board[2][2].value = 0;
board[2][2].is_filled = true;
}
BingoBoard
! Write the BingoBoard
member function printBoard
. Insert tabs between each value in each row to make the board print out neater. Check the hint below for help with the construction of the code.
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.
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.
#include <iostream>
#include <vector>
using namespace std;
void swapValues(vector<int> &vec, int index1, int index2) {
int temp = vec[index1];
vec[index1] = vec[index2];
vec[index2] = temp;
}
void bubbleSort(vector<int> &vec) {
for (size_t i = 0; i < vec.size() - 1; ++i) {
for (size_t j = 0; j < vec.size() - 1 - i; ++j) {
if (vec[j] > vec[j + 1]) {
swapValues(vec, j, j + 1);
}
}
}
}
int main() {
vector<int> vec = { 5, 1, 4, 2, 8 };
bubbleSort (vec);
for (size_t i = 0; i < vec.size(); ++i) {
cout << vec[i] << " ";
}
}
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. Check the hint below for help with the construction of the code.
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.