12.16. Activecode ExercisesΒΆ
Answer the following Activecode questions to assess what you have learned in this chapter.
Question
Let's write the struct definition for
song. song should have instance variables title, artist, and num_likes.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to define the
songstruct.
1#include <cstddef>
2#include <string>
3#include <iostream>
4
5struct song {
6 std::string title;
7 std::string artist;
8 std::size_t num_likes;
9};
Question
Let's make an album! Write the struct definition for
album, which should have instance variables name, year and a vector of Songs.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to define the
albumstruct.
1#include <cstddef>
2#include <string>
3#include <iostream>
4#include <vector>
5
6struct song {
7 std::string title;
8 std::string artist;
9 std::size_t num_likes;
10};
11
12struct album {
13 std::string name;
14 int year;
15 std::vector<song> songs;
16};
Question
Two Songs are equal if the title and artist of the Songs are equal. Write the function
song_equal, which takes two Songs as parameters and returns true if they are equal.
1#include <iostream>
2
3// YOUR CODE HERE
Answer
Below is one way to write the
song_equalfunction.
1#include <cstddef>
2#include <string>
3#include <iostream>
4
5struct song {
6 std::string title;
7 std::string artist;
8 std::size_t num_likes;
9};
10
11bool song_equal (const song& a, const song& b) {
12 if (a.title == b.title && a.artist == b.artist) {
13 return true;
14 }
15 else {
16 return false;
17 }
18}
Question
What if we'd like to search an album for our favorite song? Write the
albummember function search_album which takes a song as a parameter and returns the location of the song in the album. If the song isn't found, return -1. Use the song_equal function we defined earlier!
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
albummember function.
1#include <cstddef>
2#include <string>
3#include <iostream>
4#include <vector>
5
6struct song {
7 std::string title;
8 std::string artist;
9 std::size_t num_likes;
10};
11
12struct album {
13 std::string name;
14 int year;
15 std::vector<song> songs;
16public:
17 std::ptrdiff_t search_album(const song& a);
18};
19
20std::ptrdiff_t album::search_album (const song& a) {
21 for (std::size_t i = 0; i < songs.size(); ++i) {
22 if ((songs[i].title == a.title && songs[i].artist == a.artist)) {
23 return static_cast<std::ptrdiff_t>(i);
24 }
25 }
26 return -1;
27}
Question
What's the most popular song within an album? Let's write the
albummember function most_liked_song, which prints out the information of the most liked song in the format "The most liked song is title by artist with num_likes likes."
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
albummember function.
1#include <cstddef>
2#include <string>
3#include <iostream>
4#include <vector>
5using std::cout;
6
7struct song {
8 std::string title;
9 std::string artist;
10 std::size_t num_likes;
11};
12
13struct album {
14 std::string name;
15 int year;
16 std::vector<song> songs;
17public:
18 void most_liked_song();
19};
20
21void album::most_liked_song () {
22 if (songs.empty()) { cout << "The album is empty.\n"; return; }
23 std::size_t max_index = 0;
24 std::size_t max_likes = 0;
25 for (std::size_t i = 0; i < songs.size(); ++i) {
26 if (songs[i].num_likes > max_likes) {
27 max_index = i;
28 max_likes = songs[i].num_likes;
29 }
30 }
31 cout << "The most liked song is " << songs[max_index].title;
32 cout << " by " << songs[max_index].artist << " with ";
33 cout << songs[max_index].num_likes << " likes." << std::endl;
34}
Question
Let's write the struct definition for
product.productshould have instance variables name and price.
1#include <iostream>
2// YOUR CODE HERE
Answer
Below is one way to define the
productstruct.
1#include <string>
2#include <iostream>
3
4struct product {
5 std::string name;
6 double price;
7};
Question
Let's make a shopping list! Write the struct definition for
list, which should have instance variables type and a vector of Products.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to define the
liststruct.
1#include <string>
2#include <iostream>
3#include <vector>
4
5struct product {
6 std::string name;
7 double price;
8};
9
10struct list {
11 std::string type;
12 std::vector<product> products;
13};
Question
Two Products are equal if the name and price of the Products are equal. Write the function product_equal, which takes two Products as parameters and returns true if they are equal. What if we want to check to see if we have bananas in our shopping list? Write the list member function
search_list, which takes a product as a parameter and returns the location of the product in the list. Return -1 if it's not in the list.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
search_listmember function.
1#include <cstddef>
2#include <string>
3#include <iostream>
4#include <vector>
5
6struct product {
7 std::string name;
8 double price;
9};
10
11bool product_equal (const product& a, const product& b) {
12 if (a.name == b.name && a.price == b.price) {
13 return true;
14 }
15 else {
16 return false;
17 }
18}
19
20struct list {
21 std::string type;
22 std::vector<product> products;
23 std::ptrdiff_t search_list(const product& a);
24};
25
26std::ptrdiff_t list::search_list (const product& a) {
27 for (std::size_t i = 0; i < products.size(); ++i) {
28 if (product_equal (products[i], a)) {
29 return static_cast<std::ptrdiff_t>(i);
30 }
31 }
32 return -1;
33}
Question
Time to checkout! Write the list member function
total_pricewhich calculates and returns the total price of all the Products.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
total_pricemember function.
1#include <cstddef>
2#include <string>
3#include <iostream>
4#include <vector>
5
6struct product {
7 std::string name;
8 double price;
9};
10
11struct list {
12 std::string type;
13 std::vector<product> products;
14public:
15 double total_price();
16};
17
18double list::total_price () {
19 double total = 0;
20 for (std::size_t i = 0; i < products.size(); ++i) {
21 total += products[i].price;
22 }
23 return total;
24}
Question
Oops! We made a mistake and grabbed pineapple pizza. What if we want to remove an product from our list? Write the list member function
remove_product, which takes an index as a parameter and removes it. Then it fills the gap with the last product in the list.
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE
Answer
Below is one way to write the
remove_productmember function.
1#include <stdexcept>
2#include <cstddef>
3#include <string>
4#include <iostream>
5#include <vector>
6
7struct product {
8 std::string name;
9 double price;
10};
11
12struct list {
13 std::string type;
14 std::vector<product> products;
15public:
16 void remove_product(std::size_t index);
17};
18
19void list::remove_product (std::size_t index) {
20 if (index >= products.size()) throw std::out_of_range("product index");
21 products[index] = products.back();
22 products.pop_back();
23}