152k views
0 votes
A grayscale picture can be stored in a 2-dimensional array of integers, where the value 0 represents black and 255 represents white. We can create a mirror image of a picture by reversing the contents of each row in the picture. Write a function that takes in a 2-dimensional integer array and changes it to be its mirror image by reversing each row. Assume that the constants NUMROWS and NUMCOLUMNS store the number of rows and columns, respectively, in the array.

void makeMirror(int picture[][NUMCOLUMNS]);
Please write the solution in C language

User Lena Queen
by
8.1k points

1 Answer

5 votes

Final answer:

To create a mirror image of a grayscale picture stored in a 2-dimensional array, you can use a C function that iterates through each row and swaps the elements from the beginning and the end until the midpoint is reached. This function will modify the original array to be its mirror image.

Step-by-step explanation:

To create a mirror image of a grayscale picture stored in a 2-dimensional array, you can use the following C function:



void makeMirror(int picture[][NUMCOLUMNS]) {



  1. Create a temporary variable to store the reversed row.

  2. Iterate through each row of the picture array.

  3. Within each row, swap the elements from the beginning and the end until the midpoint is reached.

  4. Store the reversed row in the temporary variable.

  5. Copy the reversed row back to its original position in the picture array.

This function will modify the original array to be its mirror image. Here's the implementation:



void makeMirror(int picture[][NUMCOLUMNS]) {
int temp;
for (int i = 0; i < NUMROWS; i++) {
for (int j = 0; j < NUMCOLUMNS / 2; j++) {
temp = picture[i][j];
picture[i][j] = picture[i][NUMCOLUMNS - 1 - j];
picture[i][NUMCOLUMNS - 1 - j] = temp;
}
}
}

User Mabn
by
7.7k points