13.15. Coding Practice

Activecode

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 num_days number of days in a year! Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_13_ac_2q
1#include <iostream>
2
3enum planet { mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune };
4
5int main() {
6    planet p = jupiter;
7    // Write your code here.
8}

Parsonsprob

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 num_days number of days in a year! Use the lines to construct the code, then go back to complete the Activecode tab.

  1. case 1:
        std::cout << "planet Mercury has 88 number of days in a year!" << std::endl;
        break;
  2. case 2:
        std::cout << "planet Venus has 225 number of days in a year!" << std::endl;
        break;
  3. case 3:
        std::cout << "planet Earth has 365 number of days in a year!" << std::endl;
        break;
  4. case 4:
        std::cout << "planet Mars has 687 number of days in a year!" << std::endl;
        break;
  5. case 5:
        std::cout << "planet Jupiter has 4333 number of days in a year!" << std::endl;
        break;
  6. case 6:
        std::cout << "planet Saturn has 10759 number of days in a year!" << std::endl;
        break;
  7. case 7:
        std::cout << "planet Uranus has 30687 number of days in a year!" << std::endl;
        break;
  8. case 8:
        std::cout << "planet Neptune has 60190 number of days in a year!" << std::endl;
        break;
  9. enum planet { mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune };
  10. int main() {
  11. planet p = venus;
  12. switch (p) {
  13. }
  14. }

Activecode

Now let's generate a bingo_board! We want to fill the 25 spaces on the bingo_board 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 bingo_board. We will do this entire process in multiple steps. First, write the function random_int, 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.

Example c192_cp_13_ac_4q
1#include <iostream>
2// Add any relevant libraries here.
3
4// Write your code for the random_int function here.

Parsonsprob

Now let's generate a bingo_board! We want to fill the 25 spaces on the bingo_board 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 bingo_board. We will do this entire process in multiple steps. First, write the function random_int, 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.

  1. return std::uniform_int_distribution<std::size_t>{low, high}(engine);
  2. static std::mt19937 engine(std::random_device{}());
  3. std::size_t random_int(std::size_t low, std::size_t high) {
  4. }

Activecode

Now that we have the functions random_int and swap_values, we can write the function generate_rand_vec. generate_rand_vec creates a vector with values from 1 to 75, shuffles it using random_int and swap_values, and returns the shuffled vector. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_13_ac_6q
1#include <iostream>
2#include <vector>
3#include <cstdlib>
4#include <numeric>
5
6// Write your code for the generate_rand_vec function here.

Parsonsprob

Now that we have the functions random_int and swap_values, we can write the function generate_rand_vec. generate_rand_vec creates a vector with values from 1 to 75, shuffles it using random_int and swap_values, and returns the shuffled vector. Use the lines to construct the code, then go back to complete the Activecode tab.

  1. for (std::size_t i = 0; i < vec.size(); ++i) {
  2. iota(vec.begin(), vec.end(), 1);
  3. return vec;
  4. std::size_t x = random_int(i, vec.size() - 1);
  5. std::vector<int> generate_rand_vec() {
  6. std::vector<int> vec(75);
  7. swap_values(vec, i, x);
  8. }
  9. }

Activecode

Let's print out our bingo_board! Write the bingo_board member function print_board. 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.

Example c192_cp_13_ac_8q
 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    void print_board ();
17};
18
19std::size_t random_int(std::size_t low, std::size_t high);
20void swap_values (std::vector<int> &vec, std::size_t index1, std::size_t index2);
21std::vector<int> generate_rand_vec ();
22
23// Write your code for the print_board function here.
24
25int main() {
26    bingo_board bingo;
27    bingo.make_board ();
28    bingo.print_board ();
29}

Parsonsprob

Let's print out our bingo_board! Write the bingo_board member function print_board. 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.

  1. for (std::size_t i = 0; i < board[j].size(); i++) {
  2. for (std::size_t j = 0; j < board.size(); j++) {
  3. std::cout << board[j][i].value << "\t";
  4. std::cout << std::endl;
  5. void bingo_board::print_board () {
  6. }
  7. }
  8. }

Activecode

You may have noticed that in some cases, our version of bubble_sort does an unnecessary amount of work. For example, if our vector was {1, 2, 3, 5, 4}, bubble_sort 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 fast_bubble_sort, which is bubble_sort with this modification. Select the Parsonsprob tab for hints for the construction of the code.

Example c192_cp_13_ac_10q
 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 fast_bubble_sort function here.
12
13int main() {
14    std::vector<int> vec = { 1, 3, 5, 4, 6, 8, 9 };
15    fast_bubble_sort (vec);
16    for (std::size_t i = 0; i < vec.size(); ++i) {
17        std::cout << vec[i] << " ";
18    }
19}

Parsonsprob

You may have noticed that in some cases, our version of bubble_sort does an unnecessary amount of work. For example, if our vector was {1, 2, 3, 5, 4}, bubble_sort 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 fast_bubble_sort, which is bubble_sort with this modification. Use the lines to construct the code, then go back to complete the Activecode tab.

  1. bool is_changed = false;
  2. break;
  3. for (std::size_t i = 0; i + 1 < vec.size(); ++i) {
  4. for (std::size_t j = 0; j + 1 < vec.size() - i; ++j) {
  5. if (is_changed == false) {
  6. if (vec[j] > vec[j + 1]) {
  7. is_changed = true;
  8. swap_values(vec, j, j + 1);
  9. void fast_bubble_sort(std::vector<int> &vec) {
  10. }
  11. }
  12. }
  13. }
  14. }