12.17. Mixed Up Code Practice¶
Let's write the struct definition for song. song should have instance variables title, artist, and num_likes. Put the necessary blocks of code in the correct order.
-
std::size_t num_likes; -
std::string artist; -
std::string title; -
struct song { -
Struct song { #distractor -
} #distractor -
};
Let's make an album! Write the struct definition for album, which should have instance variables name, year and a vector of Songs. Put the necessary blocks of code in the correct order.
-
int year; -
song song; #distractor -
std::string album { #distractor -
std::string name; -
std::string title; #distractor -
std::vector<song> songs; -
std::vector<string> song; #distractor -
struct album { -
} #distractor -
};
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. Put the necessary blocks of code in the correct order.
-
bool song::song_equal (const song& song) { #distractor -
bool song_equal (const song& a, const &song b) { -
bool song_equal (song const &a, song const &b) { #distractor -
else { -
if (a.title == b.title && a.artist == b.artist) { -
if (title == b.title && artist == b.artist) { #distractor -
return false; -
return true; -
} -
} -
}
What if we'd like to search an album for our favorite song? Write the album member 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! Put the necessary blocks of code in the correct order.
-
bool search_album (song const &a) { #distractor -
for (std::size_t i = 0; i < album.size(); ++i) { #distractor -
for (std::size_t i = 0; i < song.size(); ++i) { #distractor -
for (std::size_t i = 0; i < songs.size(); ++i) { -
if (album.song == a) { #distractor -
if (song[i] == a) { #distractor -
if (song_equal (songs[i], a)) { -
if (songs[i] == a) { #distractor -
return -1; -
return false; #distractor -
return static_cast<std::ptrdiff_t>(i); -
return true; #distractor -
std::ptrdiff_t album::search_album (const song& a) { -
std::ptrdiff_t search_album (const album& album, const song& a) { #distractor -
} -
} -
}
What's the most popular song within an album? Let's write the album member 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." Put the necessary blocks of code in the correct order.
-
for (std::size_t i = 0; i < album.size(); ++i) { #distractor -
for (std::size_t i = 0; i < songs.size(); ++i) { -
i = max_likes; #distractor -
if (songs[i].num_likes > max_likes) { -
int album::most_liked_song () { #distractor -
max_index = i; -
max_likes = num_likes; #distractor -
max_likes = songs[i].num_likes; -
std::cout << " by " << songs[max_index].artist << " with "; -
std::cout << "The most liked song is " << songs[max_index].title; -
std::cout << songs[max_index].num_likes << " likes." << std::endl; -
std::size_t max_index = 0; -
std::size_t max_likes = 0; -
void album::most_liked_song () { -
void album::most_liked_song (const song& a) { #distractor -
} -
} -
}
Let's write the struct definition for product. product should have instance variables name and price. Put the necessary blocks of code in the correct order.
-
double price; -
int price; #distractor -
std::string name; -
struct product { -
struct product { #distractor -
} #distractor -
};
Let's make a shopping list! Write the struct definition for list, which should have instance variables type and a vector of Products. Put the necessary blocks of code in the correct order.
-
product type; #distractor -
std::string type; -
std::vector<> product; #distractor -
std::vector<product> products; -
struct list { -
Struct list { #distractor -
} #distractor -
};
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. Put the necessary blocks of code in the correct order.
-
bool product_equal (const product& a, const &product b) { -
bool product_equal (product const &a, product const &b) { #distractor -
else { -
for (std::size_t i = 0; i < num_products; ++i) { #distractor -
for (std::size_t i = 0; i < products.size(); ++i) { -
if (a.name == b.name && a.price == b.price) { -
if (a.name == b.name) { #distractor -
if (album.song == a) { #distractor -
if (product_equal (products[i], a)) { -
return -1; -
return 1; #distractor -
return false; -
return static_cast<std::ptrdiff_t>(i); -
return true; -
std::ptrdiff_t list::search_list (const product& a) { -
std::ptrdiff_t search_list (const product& a) { #distractor -
} -
} -
} -
} -
} -
}
Time to checkout! Write the list member function total_price which calculates and returns the total price of all the Products. Put the necessary blocks of code in the correct order.
-
double list : total_price () { #distractor -
double list::total_price () { -
double total = 0; -
double total; -
for (double i = 0; i < products.size(); ++i) { #distractor -
for (std::size_t i = 0; i < products.size(); ++i) { -
for (std::size_t i = 0; i > products.size(); ++i) { #distractor -
int total_price () { #distractor -
return total; -
total += products.price; -
total += products[i].price; -
} -
}
Remove the product at index, filling its position with the last product. Reject an invalid index. The order of the remaining products need not be preserved.
-
products.pop_back(); } -
if (index >= products.size()) throw std::out_of_range("product index"); -
products[index] = products.back(); -
void list::remove_product(std::size_t index) {