12.14. Coding PracticeΒΆ

Question

A pixel is the smallest controllable element of a picture represented on the screen. Images are comprised of numerous individual pixels, and each pixel's color sample has three numerical RGB (red, green, blue) components to represent the color of that pixel. The intensity value of each RGB component ranges from 0 to 255, where 0 is no intensity and 255 is highest intensity. Write the struct definition for pixel, which has values for each component r, g, and b.

Example c192_cp_12_ac_1q
1#include <iostream>
2#include <vector>
3
4// Write your code for the struct pixel here.

Answer

Below is one way to implement the program. We declare the pixel struct and create the instance variables in order.

Example c192_cp_12_ac_1a
1#include <iostream>
2#include <vector>
3
4struct pixel {
5    int r;
6    int g;
7    int b;
8};

Question

Let's print out a pixel! Write the pixel member function print_pixel, which prints out the values of the pixel in this form: (r, g, b).

Example c192_cp_12_ac_3q
 1#include <iostream>
 2#include <vector>
 3
 4struct pixel {
 5    int r;
 6    int g;
 7    int b;
 8    void print_pixel();
 9};
10
11// Write your implementation of print_pixel here.
12
13int main() {
14    pixel p = {0, 0, 0};
15    p.print_pixel();
16}

Answer

Below is one way to implement the program. We use the scope resolution operator to make print_pixel a pixel member function.

Example c192_cp_12_ac_3a
 1#include <iostream>
 2#include <vector>
 3
 4struct pixel {
 5    int r;
 6    int g;
 7    int b;
 8    void print_pixel();
 9};
10
11void pixel::print_pixel() {
12    std::cout << '('<< r << ", " << g << ", " << b << ')';
13}
14
15int main() {
16    pixel p = {0, 0, 0};
17    p.print_pixel();
18}

Question

Somebody photobombed our image! What if we wanted to crop the photobomber out? Let's write the image member function crop_image, which takes four paramenters, a start and stop row and a start and stop column. It then modifies the matrix to the cropped matrix.

Example c192_cp_12_ac_5q
 1#include <cstddef>
 2#include <iostream>
 3#include <vector>
 4using std::cout;
 5
 6struct pixel {
 7    int r;
 8    int g;
 9    int b;
10    void print_pixel();
11};
12
13struct image {
14    std::size_t m_height;
15    std::size_t m_width;
16    std::vector<std::vector<pixel> > matrix;
17    void print_image();
18    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
19};
20
21// Write your implementation of crop_image here.
22
23int main() {
24    std::vector<std::vector<pixel> > matrix = { { { 0, 255, 255 }, { 0, 0, 0 }, { 255, 255, 255 } },
25                                      { { 30, 60, 50 }, { 20, 135, 200 }, { 60, 80, 125 } },
26                                      { { 10, 0, 50 }, { 30, 65, 225 }, { 25, 105, 125 } },
27                                      { { 255, 60, 0 }, { 20, 25, 255 }, { 65, 55, 0 } } };
28    image image = { 4, 3, matrix };
29    image.print_image();
30    cout << '\n';
31    image.crop_image(2, 3, 1, 2);
32    image.print_image();
33}

Answer

Below is one way to implement the program. First we make a new matrix with the correct amount of rows. Then we push back the pixels we want into the new matrix. Afterwards, we must update the height and width of the image and set the image's matrix equal to the new one we created.

Example c192_cp_12_ac_5a
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20};
21
22void image::crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col) {
23    // Crop coordinates are one-based and inclusive.
24    if (start_row == 0 || start_col == 0 || stop_row < start_row ||
25        stop_col < start_col || stop_row > m_height || stop_col > m_width) {
26        throw std::out_of_range("crop coordinates");
27    }
28    std::vector<std::vector<pixel> > new_matrix(stop_row - start_row + 1);
29    for (std::size_t r = start_row - 1; r < stop_row; ++r) {
30        for (std::size_t c = start_col - 1; c < stop_col; ++c) {
31            new_matrix[r - (start_row - 1)].push_back(matrix[r][c]);
32        }
33    }
34    m_height = stop_row - start_row + 1;
35    m_width = stop_col - start_col + 1;
36    matrix = new_matrix;
37}
38
39int main() {
40    std::vector<std::vector<pixel> > matrix = { { { 0, 255, 255 }, { 0, 0, 0 }, { 255, 255, 255 } },
41                                      { { 30, 60, 50 }, { 20, 135, 200 }, { 60, 80, 125 } },
42                                      { { 10, 0, 50 }, { 30, 65, 225 }, { 25, 105, 125 } },
43                                      { { 255, 60, 0 }, { 20, 25, 255 }, { 65, 55, 0 } } };
44    image image = { 4, 3, matrix };
45    image.print_image();
46    cout << '\n';
47    image.crop_image(2, 3, 1, 2);
48    image.print_image();
49}

Question

When you take a selfie on your phone, the image is mirrored. We can do the same to an image by flipping it horizontally. Write the image member function flip_horizontal, which flips an image horizontally. Use the swap_pixel function we created previously.

Example c192_cp_12_ac_7q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21    void flip_horizontal();
22};
23
24// Write your implementation of flip_horizontal here.
25
26int main() {
27    std::vector<std::vector<pixel> > matrix = { { { 0, 0, 0 }, { 10, 10, 10 }, { 255, 255, 255 } },
28                                      { { 50, 50, 50 }, { 10, 10, 10 }, { 255, 255, 255 } },
29                                      { { 100, 100, 100 }, { 10, 10, 10 }, { 255, 255, 255 } },
30                                      { { 150, 150, 150 }, { 10, 10, 10 }, { 255, 255, 255 } } };
31    image image = { 4, 3, matrix };
32    image.print_image();
33    cout << '\n';
34    image.flip_horizontal();
35    image.print_image();
36}

Answer

Below is one way to implement the program. We loop through each row in the matrix. We create start and end indices and repeatedly swap pixels, moving both indices toward the middle. Once they meet in the middle, we have finished flipping the image.

Example c192_cp_12_ac_7a
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21    void flip_horizontal();
22};
23
24void image::flip_horizontal() {
25    for (std::size_t r = 0; r < m_height; ++r) {
26        std::size_t start = 0;
27        std::size_t end = m_width == 0 ? 0 : m_width - 1;
28        while (start < end) {
29            swap_pixel(r, start, r, end);
30            ++start;
31            --end;
32        }
33    }
34}
35
36int main() {
37    std::vector<std::vector<pixel> > matrix = { { { 0, 0, 0 }, { 10, 10, 10 }, { 255, 255, 255 } },
38                                      { { 50, 50, 50 }, { 10, 10, 10 }, { 255, 255, 255 } },
39                                      { { 100, 100, 100 }, { 10, 10, 10 }, { 255, 255, 255 } },
40                                      { { 150, 150, 150 }, { 10, 10, 10 }, { 255, 255, 255 } } };
41    image image = { 4, 3, matrix };
42    image.print_image();
43    cout << '\n';
44    image.flip_horizontal();
45    image.print_image();
46}

Question

Let's write the image member function called create_border, which sets the pixels on the edge of an image to a given pixel.

Example c192_cp_12_ac_9q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21    void flip_horizontal();
22    void flip_vertical();
23    void create_border(pixel p);
24};
25
26// Write your implementation of create_border here.
27
28int main() {
29    std::vector<std::vector<pixel> > matrix = { { { 25, 65, 23 }, { 73, 56, 24 }, { 255, 255, 255 }, { 253, 61, 56 } },
30                                      { { 50, 50, 50 }, { 145, 52, 102 }, { 2, 0, 25 }, { 52, 47, 35 } },
31                                      { { 45, 34, 100 }, { 213, 67, 45 }, { 2, 45, 255 }, { 34, 16, 76 } },
32                                      { { 2, 2, 78 }, { 164, 16, 23 }, { 5, 255, 25 }, { 32, 65, 34 } },
33                                      { { 150, 150, 150 }, { 241, 42, 64 }, { 1, 4, 255 }, { 16, 73, 84 } } };
34    image image = { 5, 4, matrix };
35    image.print_image();
36    cout << '\n';
37    pixel p = { 0, 0, 0 };
38    image.create_border(p);
39    image.print_image();
40}

Answer

Below is one way to implement the program. We set the first and last row and first and last column of pixels in the image to the given pixel.

Example c192_cp_12_ac_9a
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21    void flip_horizontal();
22    void flip_vertical();
23    void create_border(pixel p);
24};
25
26void image::create_border(pixel p) {
27    if (m_height == 0 || m_width == 0) return;
28    for (std::size_t r = 0; r < m_height; ++r) {
29        matrix[r][0] = p;
30        matrix[r][m_width - 1] = p;
31    }
32    for (std::size_t c = 0; c < m_width; ++c) {
33        matrix[0][c] = p;
34        matrix[m_height - 1][c] = p;
35    }
36}
37
38int main() {
39    std::vector<std::vector<pixel> > matrix = { { { 25, 65, 23 }, { 73, 56, 24 }, { 255, 255, 255 }, { 253, 61, 56 } },
40                                      { { 50, 50, 50 }, { 145, 52, 102 }, { 2, 0, 25 }, { 52, 47, 35 } },
41                                      { { 45, 34, 100 }, { 213, 67, 45 }, { 2, 45, 255 }, { 34, 16, 76 } },
42                                      { { 2, 2, 78 }, { 164, 16, 23 }, { 5, 255, 25 }, { 32, 65, 34 } },
43                                      { { 150, 150, 150 }, { 241, 42, 64 }, { 1, 4, 255 }, { 16, 73, 84 } } };
44    image image = { 5, 4, matrix };
45    image.print_image();
46    cout << '\n';
47    pixel p = { 0, 0, 0 };
48    image.create_border(p);
49    image.print_image();
50}

12.14.1. Additional coding exercisesΒΆ

Question

An image is just a matrix of pixels. Write the struct definition for image, which should store information about its height and width and contain a matrix of pixels. Use the code prompt to write your solution.

Example c192_cp_12_ac_2q
 1#include <iostream>
 2#include <vector>
 3
 4struct pixel {
 5    int r;
 6    int g;
 7    int b;
 8};
 9
10// Write your code for the struct image here.

Question

Now let's print an image. Unfortunately we can't print out the actual image to the terminal, but we can print out the pixels in the image matrix. Write the image member function print_image. Separate pixels in the same row with a space and add a new line at the end of each row. Use the print_pixel function we created previously. Use the code prompt to write your solution.

Example c192_cp_12_ac_4q
 1#include <cstddef>
 2#include <iostream>
 3#include <vector>
 4
 5struct pixel {
 6    int r;
 7    int g;
 8    int b;
 9    void print_pixel();
10};
11
12struct image {
13    std::size_t m_height;
14    std::size_t m_width;
15    std::vector<std::vector<pixel>> matrix;
16    void print_image();
17};
18
19// Write your implementation of print_image here.
20
21int main() {
22    std::vector<std::vector<pixel> > matrix = { { { 0, 255, 255 }, { 0, 0, 0 }, { 255, 255, 255 } },
23                                    { { 30, 60, 50 }, { 20, 135, 200 }, { 60, 80, 125 } } };
24    image image = { 2, 3, matrix };
25    image.print_image();
26}

Question

Let's write a swap_pixel member function for image. swap_pixel takes two pairs of row indices and column indices from a matrix and swaps the two pixels at those locations. Note that these indices are 0-indexed, unlike the previous crop_index parameters. Use the code prompt to write your solution.

Example c192_cp_12_ac_6q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21};
22
23// Write your implementation of swap_pixel here.
24
25int main() {
26    std::vector<std::vector<pixel> > matrix = { { { 0, 140, 255 }, { 0, 0, 0 }, { 15, 20, 255 } } };
27    image image = { 1, 3, matrix };
28    image.print_image();
29    cout << '\n';
30    image.swap_pixel(0, 0, 0, 2);
31    image.print_image();
32}

Question

Oops! Somehow our image came out upside down. Let's write the image member function flip_vertical, which reverts an image to be right side up. Use the code prompt to write your solution.

Example c192_cp_12_ac_8q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21    void flip_horizontal();
22    void flip_vertical();
23};
24
25// Write your implementation of flip_vertical here.
26
27int main() {
28    std::vector<std::vector<pixel> > matrix = { { { 255, 255, 255 }, { 255, 255, 255 }, { 255, 255, 255 } },
29                                    { { 50, 50, 50 }, { 10, 10, 10 }, { 50, 50, 50 } },
30                                    { { 30, 30, 30 }, { 70, 70, 70 }, { 30, 30, 30 } },
31                                    { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } } };
32    image image = { 4, 3, matrix };
33    image.print_image();
34    cout << '\n';
35    image.flip_vertical();
36    image.print_image();
37}

Question

Let's return our image to the state of a clean slate. Write the function clear_image, which sets the color of every pixel to white. Use the code prompt to write your solution.

Example c192_cp_12_ac_10q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7struct pixel {
 8    int r;
 9    int g;
10    int b;
11    void print_pixel();
12};
13
14struct image {
15    std::size_t m_height;
16    std::size_t m_width;
17    std::vector<std::vector<pixel> > matrix;
18    void print_image();
19    void crop_image(std::size_t start_row, std::size_t stop_row, std::size_t start_col, std::size_t stop_col);
20    void swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2);
21    void flip_horizontal();
22    void flip_vertical();
23    void create_border(pixel p);
24    void clear_image();
25};
26
27// Write your implementation of clear_image here.
28
29int main() {
30    std::vector<std::vector<pixel> > matrix = { { { 0, 0, 0 }, { 10, 10, 10 }, { 65, 70, 255 } },
31                                    { { 26, 48, 205 }, { 43, 12, 15 }, { 45, 30, 70 } },
32                                    { { 89, 36, 65 }, { 75, 43, 26 }, { 40, 75, 70 } } };
33    image image = { 3, 3, matrix };
34    image.print_image();
35    cout << '\n';
36    image.clear_image();
37    image.print_image();
38}

Question

Let's write the struct definition for song. song should have instance variables title, artist, and num_likes.

Example c192_mucp_12_1_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to define the song struct.

Example c192_mucp_12_1_ac_a
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.

Example c192_mucp_12_2_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to define the album struct.

Example c192_mucp_12_2_ac_a
 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 m_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.

Example c192_mucp_12_3_ac_q
1#include <iostream>
2
3// YOUR CODE HERE

Answer

Below is one way to write the song_equal function.

Example c192_mucp_12_3_ac_a
 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 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!

Example c192_mucp_12_4_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the album member function.

Example c192_mucp_12_4_ac_a
 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 m_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 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."

Example c192_mucp_12_5_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the album member function.

Example c192_mucp_12_5_ac_a
 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 m_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." << '\n';
34}

Question

Let's write the struct definition for product. product should have instance variables name and price.

Example c192_mucp_12_6_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to define the product struct.

Example c192_mucp_12_6_ac_a
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.

Example c192_mucp_12_7_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to define the list struct.

Example c192_mucp_12_7_ac_a
 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.

Example c192_mucp_12_8_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the search_list member function.

Example c192_mucp_12_8_ac_a
 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_price which calculates and returns the total price of all the Products.

Example c192_mucp_12_9_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the total_price member function.

Example c192_mucp_12_9_ac_a
 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.

Example c192_mucp_12_10_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the remove_product member function.

Example c192_mucp_12_10_ac_a
 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}