12.13. Mixed-Up Code Exercises¶
Arrange the following Mixed-Up Code exercises to assess what you have learned in this chapter.
12.13.1. Parsons exercises¶
An image is just a matrix of pixels. Write the
structdefinition forimage, which should store information about its height and width and contain a matrix ofpixels. Use the lines to construct the code, then complete the code in the correct order.
-
std::size_t height; -
std::size_t width; -
std::vector<pixel> matrix; #distractor -
std::vector<std::vector<pixel>> matrix; -
std::vector<std::vector> matrix; #distractor -
struct image { -
};
Now let's print an
image. Unfortunately we can't print out the actual image to the terminal, but we can print out thepixels in theimagematrix. Write theimagemember functionprint_image. Separate pixels in the same row with a space and add a new line at the end of each row. Use theprint_pixelfunction we created previously. Use the lines to construct the code, then complete the code in the correct order.
-
for (std::size_t c = 0; c < width; ++ c) { -
for (std::size_t r = 0; r < height; ++r) { -
matrix[r][c].print_pixel(); -
std::cout << ' '; -
std::cout << '\n'; -
void image::print_image() { -
} -
} -
}
Let's write a
swap_pixelmember function forimage.swap_pixeltakes two pairs of row indices and column indices from a matrix and swaps the twopixels at those locations. Note that these indices are 0-indexed, unlike the previouscrop_indexparameters. Use the lines to construct the code, then complete the code in the correct order.
-
matrix[row1][col1] = matrix[row2][col2]; -
matrix[row2][col2] = temp; -
pixel temp = { matrix[row1][col1].r, matrix[row1][col1].g, matrix[row1][col1].b }; -
void image::swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2) { -
}
Oops! Somehow our image came out upside down. Let's write the
imagemember functionflip_vertical, which reverts an image to be right side up. Use the lines to construct the code, then complete the code in the correct order.
-
++start; -
--end; -
std::size_t end = height == 0 ? 0 : height - 1; -
std::size_t start = 0; -
swap_pixel(start, c, end, c); -
void image::flip_vertical() { for (std::size_t c = 0; c < width; ++c) { -
while (start < end) { -
} -
} -
}
Let's return our image to the state of a clean slate. Write the function
clear_image, which sets the color of everypixelto white. Use the lines to construct the code, then complete the code in the correct order.
-
for (std::size_t c = 0; c < width; c++) { -
for (std::size_t r = 0; r < height; r++) { -
matrix[r][c].b = 255; -
matrix[r][c].g = 255; -
matrix[r][c].r = 255; -
void image::clear_image () { -
} -
} -
}
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." << '\n'; -
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) {