13.14. Coding PracticeΒΆ
Question
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 :( )
1#include <iostream>
2
3// Write your code for the enumerated type planet.
Answer
Below is one way to implement the program. The planets in our solar system are Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune.
1#include <iostream>
2
3enum planet { mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune };
Practice selection
The legacy Runestone question pool c192_cp_13_ac_2_sq is represented by these exercises:
Question
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 bingo_board.
1#include <iostream>
2#include <vector>
3
4// Write your code for the struct space here.
5
6// Write your code for the struct bingo_board here.
Answer
Below is one way to implement the program. We declare the space and bingo_board struct
and create the instance variables in order. Make sure to set is_filled to false!
1#include <iostream>
2#include <vector>
3
4struct space {
5 int value;
6 bool is_filled;
7};
8
9struct bingo_board {
10 std::vector<std::vector<space> > board;
11};
Practice selection
The legacy Runestone question pool c192_cp_13_ac_4_sq is represented by these exercises:
Question
Now we need a way to swap the values at two indices in a vector. Write the function swap_values,
which takes a vector of ints and two indices as parameters.
1#include <iostream>
2#include <vector>
3
4// Write your code for the swap_values function here.
Answer
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!
1#include <cstddef>
2#include <iostream>
3#include <vector>
4
5void swap_values (std::vector<int> &vec, std::size_t index1, std::size_t index2) {
6 int temp = vec[index1];
7 vec[index1] = vec[index2];
8 vec[index2] = temp;
9}
Practice selection
The legacy Runestone question pool c192_cp_13_ac_6_sq is represented by these exercises:
Question
We can now fill our bingo_board with values! Write the bingo_board
member function make_board. Use the generate_rand_vec
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.
1#include <cstddef>
2#include <random>
3#include <iostream>
4#include <vector>
5#include <cstdlib>
6#include <numeric>
7
8struct space {
9 int value;
10 bool is_filled;
11};
12
13struct bingo_board {
14 std::vector<std::vector<space> > board;
15 void make_board ();
16};
17
18std::size_t random_int(std::size_t low, std::size_t high);
19void swap_values (std::vector<int> &vec, std::size_t index1, std::size_t index2);
20std::vector<int> generate_rand_vec ();
21
22// Write your code for the make_board function here.
Answer
Below is one way to implement the program. First we need to initialize
the board to the correct dimensions. Then, we use generate_rand_vec
to create a vector of random values from 1 to 75. Afterwards, we set
the values of the 25 spaces to the first 25 values in the
random vector. Lastly, we set the middle space to 0 and
set its is_filled to true.
1#include <cstddef>
2#include <random>
3#include <iostream>
4#include <vector>
5#include <cstdlib>
6#include <numeric>
7
8struct space {
9 int value;
10 bool is_filled;
11};
12
13struct bingo_board {
14 std::vector<std::vector<space> > board;
15 void make_board ();
16};
17
18std::size_t random_int(std::size_t low, std::size_t high);
19void swap_values (std::vector<int> &vec, std::size_t index1, std::size_t index2);
20std::vector<int> generate_rand_vec ();
21
22void bingo_board::make_board() {
23 // Initialize board
24 space s = {0, false};
25 std::vector<space> cols(5, s);
26 for (std::size_t i = 0; i < 5; ++i) {
27 board.push_back(cols);
28 }
29
30 // Fill board with random values
31 std::vector<int> vec = generate_rand_vec();
32 std::size_t count = 0;
33 for (std::size_t row = 0; row < board.size(); ++row) {
34 for (std::size_t col = 0; col < board[row].size(); ++col) {
35 board[row][col].value = vec[count];
36 ++count;
37 }
38 }
39
40 // Create free space
41 board[2][2].value = 0;
42 board[2][2].is_filled = true;
43}
Practice selection
The legacy Runestone question pool c192_cp_13_ac_8_sq is represented by these exercises:
Question
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 bubble_sort,
which takes a vector as a parameter and sorts it. Feel free to use the provided
swap_values function.
1#include <cstddef>
2#include <iostream>
3#include <vector>
4
5void swap_values(std::vector<int> &vec, std::size_t index1, std::size_t index2) {
6 int temp = vec[index1];
7 vec[index1] = vec[index2];
8 vec[index2] = temp;
9}
10
11// Write your code for the bubble_sort function here.
12
13int main() {
14 std::vector<int> vec = { 5, 1, 4, 2, 8 };
15 bubble_sort (vec);
16 for (std::size_t i = 0; i < vec.size(); ++i) {
17 std::cout << vec[i] << " ";
18 }
19}
Answer
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.
1#include <cstddef>
2#include <iostream>
3#include <vector>
4
5void swap_values(std::vector<int> &vec, std::size_t index1, std::size_t index2) {
6 int temp = vec[index1];
7 vec[index1] = vec[index2];
8 vec[index2] = temp;
9}
10
11void bubble_sort(std::vector<int> &vec) {
12 for (std::size_t i = 0; i + 1 < vec.size(); ++i) {
13 for (std::size_t j = 0; j + 1 < vec.size() - i; ++j) {
14 if (vec[j] > vec[j + 1]) {
15 swap_values(vec, j, j + 1);
16 }
17 }
18 }
19}
20
21int main() {
22 std::vector<int> vec = { 5, 1, 4, 2, 8 };
23 bubble_sort (vec);
24 for (std::size_t i = 0; i < vec.size(); ++i) {
25 std::cout << vec[i] << " ";
26 }
27}
Practice selection
The legacy Runestone question pool c192_cp_13_ac_10_sq is represented by these exercises: