Reversed 2d dynamic array

I´m writing a program that user inputs number of row and columns of a 2d dynamic array and inserts the number in the matrix and the programs prints out the original matrix and a reversed matrix

Like if user inputs 2 5 1 2 3 4 5 6 7 8 9 0
then the program prints out

orginal:
1 2 3 4 5
6 7 8 9 0

mirrored:
5 4 3 2 1
0 9 8 7 6

This is what I came up with but the reversed version is not right, can anyone help me ?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 #include <iostream>
#include <string>


using namespace std;

int row;
int col;


void read_arr(int **arr, int row, int col);
void print_2d_arr(int **arr, int row, int col);
void mirror_arr(int **arr, int row,int col);


int main()
{
    int row;
    int col;
    cin >> row;
    cin >>col;
   


    int **arr;
    arr = new int*[row];
    for (int i = 0; i < row; i++)
    {
        arr[i] = new int[col];
    }

    int k = 0;
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            arr[i][j] = k++;
        }
    }

    read_arr(arr,row,col);

    cout << endl<<  "orginal:" << endl;

    print_2d_arr(arr,row,col);



  mirror_arr(arr, row, col);

   cout << endl<<  "mirrored:" << endl;

    print_2d_arr(arr,row,col);


    





    for (int i = 0; i < row; i++)
    {
        delete [] arr[i];
    }
    delete [] arr;



    return 0;
}



void read_arr(int **arr, int row, int col)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cin >> arr[i][j];
        }
        cout << endl;
    }



}


void print_2d_arr(int **arr, int row, int col)
{
   
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cout << arr[i][j] <<" ";
        }
        cout << endl;
    }
}

void mirror_arr(int **arr, int row,int col)
{

    for (int i = 0; i < row/2; i++)
    {
        for (int j=0; j < col/2; j++)
        {
            int tmp = arr[i][j];
            arr[i][j] = arr[i][col-1-j];
            arr[i][row-1-j] = tmp;
        }
    }
}

Last edited on
This is what I came up with but the reversed version is not right

Can you show us your broken output?
2
5
1
2
3
4
5

6
7
8
9
0


orginal:
1 2 3 4 5
6 7 8 9 0

mirrored:
1 4 3 4 5
6 7 8 9 0
1
2
3
4
5
6
7
8
9
10
11
12
void mirror_arr(int **arr, int row,int col)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col / 2; j++)
        {
            int tmp = arr[i][j];
            arr[i][j] = arr[i][col-1-j];
            arr[i][col-1-j] = tmp;
        }
    }
}
Last edited on
Topic archived. No new replies allowed.