Mirror a 2d array

Oct 5, 2016 at 3:08pm
Hi i´m writing a program that takes user inputs into a 2d array. The user decides number of row and columns and inputs the numbers in the array. The program needs to print out the array and print out a mirrored version. This is what I have came up with but I need help with the mirrored array.

By mirroring I mean reflecting an array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>

using namespace std;



int main()
{

    int row;
    int col;
    cin >> row;
    cin >>col;

    int array[row][col], mirror[row][col];


     for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
           cin >> array[i][j];
        }

    }

    
    cout << "orginal: " << endl;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << array[i][j] << " ";
        }
        cout << endl;
    }


    cout << endl;

     for(int i=0; i<row; i++){
            for(int j=i-1; j<col; j++)
    {
       mirror[i][j]=array[row-1-i][col-1-j];
    }
     }


    cout << "mirrored: " << endl;
    for(int i=0; i<col; i++){
            for(int  j=0; j<row; j++)
    {
        cout << mirror[i][j] << " " ;

    }
    }

    return 0;
}
Last edited on Oct 5, 2016 at 3:49pm
Oct 5, 2016 at 3:34pm
closed account (iE8M92yv)
By "mirrored" do you mean the usual mathematical matrix term "transposed" (i.e. swap rows and columns)?

If so then:
(i) mirrow would actually have to have col "rows" and row "cols" (e.g. a 2x5 matrix would become 5x2 when transposed). At the moment this isn't what you have.
(ii) mirror[i][j] = array[j][i], with i cycling through the "rows" of mirror and similarly j cycling through the "cols" of mirror.

If I have the wrong end of the stick then please clarify "mirrored".

Couple of minor issues:
(iii) Might be a good idea to prompt the user for his input, rather than assuming knowledge of the order of inputs.
(iv) I'm never quite sure about declaring the size of arrays (here, row and col) at RUN-time; it always seems to work, but I sort of feel one ought to be dynamically allocating them.
Oct 5, 2016 at 3:49pm
By mirroring an array I mean reflecting the array. Like if if have 1x4 array with
1 2 3 4 the mirrored version is suppose to be 4 3 2 1
Oct 5, 2016 at 4:00pm
closed account (iE8M92yv)
But if you have a 2-d array like
1 2 3 4
5 6 7 8

what is the "mirrored" version of that?

The "transposed" version would be
1 5
2 6
3 7
4 8
Oct 5, 2016 at 4:13pm
the mirrored version of the 2-d array would be
4 3 2 1
8 7 6 5
Oct 5, 2016 at 4:16pm
closed account (iE8M92yv)
If you want to "mirror" (i.e. reverse) in BOTH horizontal and vertical directions for a 2d array then the last pair of nested loops could be written as follows, with ii and jj declared as integers at the start of your program. (Note that I'm still wondering if "transposed" might have been what was really intended.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    for(int i=0; i<row; i++)
    {
       for(int j=0; j<col; j++)
       {
          ii = row - 1 - i;
          jj = col - 1 - j;
          mirror[i][j]=array[ii][jj];
       }
    }


    cout << "mirrored: " << endl;
    for(int i=0; i<row; i++)
   {
         for(int  j=0; j<col; j++)
        {
           cout << mirror[i][j] << " " ;
        }
        cout << endl;
    }


As it stands, your array is restricted to integers (it could be "Template'd") and the program is quite hard to use without user prompts.
Oct 5, 2016 at 4:22pm
closed account (iE8M92yv)
Sorry - missed your previous reply. However, if you want to get the LEFT-RIGHT mirrored version then you can just change the ii= line in my suggestion to
ii = i;
(so allowing you some variants later, if, as I still suspect, this wasn't what was intended by the question; it's quite an unusual matrix operation to be asked to do.)
Oct 5, 2016 at 4:39pm
I´m not supposed to transpose the matrix just reflect the matrix as I explained in previous comment. when I run the code I have like I put in the first comment I get :

2
2
1
2
3
4
orginal:
1 2
3 4

mirrored:
4 3 2 1

the mirrored is suppose to be :
2 1
3 4
Oct 5, 2016 at 5:17pm
thank you ! it works
Topic archived. No new replies allowed.