12.15. Coding Practice

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

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.

Parsonsprob

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 lines to construct the code, then go back to complete the Activecode tab.

  1. std::size_t height;
  2. std::size_t width;
  3. std::vector<pixel> matrix; #distractor
  4. std::vector<std::vector<pixel>> matrix;
  5. std::vector<std::vector> matrix; #distractor
  6. struct image {
  7. };

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

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 height;
14    std::size_t 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}

Parsonsprob

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 lines to construct the code, then go back to complete the Activecode tab.

  1. for (std::size_t c = 0; c < width; ++ c) {
  2. for (std::size_t r = 0; r < height; ++r) {
  3. matrix[r][c].print_pixel();
  4. std::cout << " ";
  5. std::cout << std::endl;
  6. void image::print_image() {
  7. }
  8. }
  9. }

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

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 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};
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 << std::endl;
30    image.swap_pixel(0, 0, 0, 2);
31    image.print_image();
32}

Parsonsprob

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 lines to construct the code, then go back to complete the Activecode tab.

  1. matrix[row1][col1] = matrix[row2][col2];
  2. matrix[row2][col2] = temp;
  3. pixel temp = { matrix[row1][col1].r, matrix[row1][col1].g,  matrix[row1][col1].b };
  4. void image::swap_pixel(std::size_t row1, std::size_t col1, std::size_t row2, std::size_t col2) {
  5. }

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

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 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};
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 << std::endl;
35    image.flip_vertical();
36    image.print_image();
37}

Parsonsprob

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 lines to construct the code, then go back to complete the Activecode tab.

  1. ++start;
  2. --end;
  3. std::size_t end = height == 0 ? 0 : height - 1;
  4. std::size_t start = 0;
  5. swap_pixel(start, c, end, c);
  6. void image::flip_vertical() {
        for (std::size_t c = 0; c < width; ++c) {
  7. while (start < end) {
  8. }
  9. }
  10. }

Activecode

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. Select the Parsonsprob tab for hints for the construction of the code.

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 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    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 << std::endl;
36    image.clear_image();
37    image.print_image();
38}

Parsonsprob

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 lines to construct the code, then go back to complete the Activecode tab.

  1. for (std::size_t c = 0; c < width; c++) {
  2. for (std::size_t r = 0; r < height; r++) {
  3. matrix[r][c].b = 255;
  4. matrix[r][c].g = 255;
  5. matrix[r][c].r = 255;
  6. void image::clear_image () {
  7. }
  8. }
  9. }