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
planethasnum_daysnumber of days in a year! Select the Parsonsprob tab for hints for the construction of the code.
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
planethasnum_daysnumber of days in a year! Use the lines to construct the code, then go back to complete the Activecode tab.
-
case 1: std::cout << "planet Mercury has 88 number of days in a year!" << std::endl; break; -
case 2: std::cout << "planet Venus has 225 number of days in a year!" << std::endl; break; -
case 3: std::cout << "planet Earth has 365 number of days in a year!" << std::endl; break; -
case 4: std::cout << "planet Mars has 687 number of days in a year!" << std::endl; break; -
case 5: std::cout << "planet Jupiter has 4333 number of days in a year!" << std::endl; break; -
case 6: std::cout << "planet Saturn has 10759 number of days in a year!" << std::endl; break; -
case 7: std::cout << "planet Uranus has 30687 number of days in a year!" << std::endl; break; -
case 8: std::cout << "planet Neptune has 60190 number of days in a year!" << std::endl; break; -
enum planet { mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune }; -
int main() { -
planet p = venus; -
switch (p) { -
} -
}
Activecode
Now let's generate a
bingo_board! We want to fill the 25spaces on thebingo_boardwith random values from 1 to 75 without repititon. To do this, we'll make avectorof 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 thebingo_board. We will do this entire process in multiple steps. First, write the functionrandom_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.
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 25spaces on thebingo_boardwith random values from 1 to 75 without repititon. To do this, we'll make avectorof 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 thebingo_board. We will do this entire process in multiple steps. First, write the functionrandom_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.
-
return std::uniform_int_distribution<std::size_t>{low, high}(engine); -
static std::mt19937 engine(std::random_device{}()); -
std::size_t random_int(std::size_t low, std::size_t high) { -
}
Activecode
Now that we have the functions
random_intandswap_values, we can write the functiongenerate_rand_vec.generate_rand_veccreates avectorwith values from 1 to 75, shuffles it usingrandom_intandswap_values, and returns the shuffledvector. Select the Parsonsprob tab for hints for the construction of the code.
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_intandswap_values, we can write the functiongenerate_rand_vec.generate_rand_veccreates avectorwith values from 1 to 75, shuffles it usingrandom_intandswap_values, and returns the shuffledvector. Use the lines to construct the code, then go back to complete the Activecode tab.
-
for (std::size_t i = 0; i < vec.size(); ++i) { -
iota(vec.begin(), vec.end(), 1); -
return vec; -
std::size_t x = random_int(i, vec.size() - 1); -
std::vector<int> generate_rand_vec() { -
std::vector<int> vec(75); -
swap_values(vec, i, x); -
} -
}
Activecode
Let's print out our
bingo_board! Write thebingo_boardmember functionprint_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.
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 thebingo_boardmember functionprint_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.
-
for (std::size_t i = 0; i < board[j].size(); i++) { -
for (std::size_t j = 0; j < board.size(); j++) { -
std::cout << board[j][i].value << "\t"; -
std::cout << std::endl; -
void bingo_board::print_board () { -
} -
} -
}
Activecode
You may have noticed that in some cases, our version of
bubble_sortdoes an unnecessary amount of work. For example, if ourvectorwas {1, 2, 3, 5, 4},bubble_sortwould swap 4 and 5, but then keep going even though ourvectoris already in order! We can save some work by including aboolcalledis_changed. If we swap values during a pass, we setis_changedto true. If nothing has been swapped, thenis_changedstays false, and we know to break out of the loop since ourvectoris already sorted. Write the functionfast_bubble_sort, which isbubble_sortwith this modification. Select the Parsonsprob tab for hints for the construction of the code.
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_sortdoes an unnecessary amount of work. For example, if ourvectorwas {1, 2, 3, 5, 4},bubble_sortwould swap 4 and 5, but then keep going even though ourvectoris already in order! We can save some work by including aboolcalledis_changed. If we swap values during a pass, we setis_changedto true. If nothing has been swapped, thenis_changedstays false, and we know to break out of the loop since ourvectoris already sorted. Write the functionfast_bubble_sort, which isbubble_sortwith this modification. Use the lines to construct the code, then go back to complete the Activecode tab.
-
bool is_changed = false; -
break; -
for (std::size_t i = 0; i + 1 < vec.size(); ++i) { -
for (std::size_t j = 0; j + 1 < vec.size() - i; ++j) { -
if (is_changed == false) { -
if (vec[j] > vec[j + 1]) { -
is_changed = true; -
swap_values(vec, j, j + 1); -
void fast_bubble_sort(std::vector<int> &vec) { -
} -
} -
} -
} -
}