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};

Practice selection

The legacy Runestone question pool c192_cp_12_ac_2_sq is represented by these exercises:

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}

Practice selection

The legacy Runestone question pool c192_cp_12_ac_4_sq is represented by these exercises:

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 height;
15    std::size_t 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 << std::endl;
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 height;
16    std::size_t 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 > height || stop_col > 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    height = stop_row - start_row + 1;
35    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 << std::endl;
47    image.crop_image(2, 3, 1, 2);
48    image.print_image();
49}

Practice selection

The legacy Runestone question pool c192_cp_12_ac_6_sq is represented by these exercises:

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 height;
16    std::size_t 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 << std::endl;
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 height;
16    std::size_t 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 < height; ++r) {
26        std::size_t start = 0;
27        std::size_t end = width == 0 ? 0 : 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 << std::endl;
44    image.flip_horizontal();
45    image.print_image();
46}

Practice selection

The legacy Runestone question pool c192_cp_12_ac_8_sq is represented by these exercises:

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 height;
16    std::size_t 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 << std::endl;
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 height;
16    std::size_t 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 (height == 0 || width == 0) return;
28    for (std::size_t r = 0; r < height; ++r) {
29        matrix[r][0] = p;
30        matrix[r][width - 1] = p;
31    }
32    for (std::size_t c = 0; c < width; ++c) {
33        matrix[0][c] = p;
34        matrix[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 << std::endl;
47    pixel p = { 0, 0, 0 };
48    image.create_border(p);
49    image.print_image();
50}

Practice selection

The legacy Runestone question pool c192_cp_12_ac_10_sq is represented by these exercises: