12.15. Coding Practice¶
Activecode
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. Select the Parsonsprob tab for hints for the construction of the code.
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
structdefinition forimage, which should store information about its height and width and contain a matrix ofpixels. Use the lines to construct the code, then go back to complete the Activecode tab.
-
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 { -
};
Activecode
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. Select the Parsonsprob tab for hints for the construction of the code.
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 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 go back to complete the Activecode tab.
-
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 << std::endl; -
void image::print_image() { -
} -
} -
}
Activecode
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. Select the Parsonsprob tab for hints for the construction of the code.
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_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 go back to complete the Activecode tab.
-
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) { -
}
Activecode
Oops! Somehow our image came out upside down. Let's write the
imagemember functionflip_vertical, which reverts an image to be right side up. Select the Parsonsprob tab for hints for the construction of the code.
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
imagemember functionflip_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.
-
++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) { -
} -
} -
}
Activecode
Let's return our image to the state of a clean slate. Write the function
clear_image, which sets the color of everypixelto white. Select the Parsonsprob tab for hints for the construction of the code.
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 everypixelto white. Use the lines to construct the code, then go back to complete the Activecode tab.
-
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 () { -
} -
} -
}