.Let color be the following structure: struct color int red;int green;int blue;};Suppose the resolution of the picture is 300 x 300 that is, you need to create a two-dimensional array of 300 x 300 pixels, each of which stores information in three channels: red, green, and blue. The color value is a random integer.
时间: 2023-06-27 15:07:27 浏览: 42
To create a two-dimensional array of 300 x 300 pixels, each storing information in the three color channels, we can use the following code in C++:
```
struct color {
int red;
int green;
int blue;
};
const int width = 300;
const int height = 300;
color image[height][width];
// To initialize the color values with random integers between 0 and 255
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
image[i][j].red = rand() % 256;
image[i][j].green = rand() % 256;
image[i][j].blue = rand() % 256;
}
}
```
This code defines a struct `color` that has three integer fields `red`, `green`, and `blue`. We then define two constants, `width` and `height`, to specify the dimensions of the image. The two-dimensional array `image` is then created with dimensions `height` and `width`, with each element of the array storing a `color` struct.
To initialize the color values with random integers between 0 and 255, we use a nested loop to iterate over each element of the array and set its `red`, `green`, and `blue` fields to a random integer between 0 and 255 using the `rand()` function.