You're given the number of rows and columns and the indices for the reference point. Reference point is = 0.
i.e:
3 4 3 3
output:
4 3 2 3
3 2 1 2
2 1 0 1
I managed to solve the problem using the easiest solution there is. I calculated the elements from the same column, and then using 2 for loops I used for every single element the already calculated value from the respective row as a new reference point. I was stuck on this at first because I tried to come up with a formula so I asked on stackoverflow, and told me to submit a code, any solution.. so I came up with this:
#include <iostream>
using namespace std;
int main()
{
int v[501][501], i, j, m, n, o, p;
cin >> m >> n >> o >> p;
for (i = o; i >= 1; i--)
v[i][p] = o - i;
for(i = o;i <= m; i++)
v[i][p] = i - o;
for(i = 1; i <= m; i++)
for(j = 1; j <= n; j++){
if(j < p)
v[i][j] = -j + p + v[i][p];
else if(j > p)
v[i][j] = j - p + v[i][p];
}
for(i = 1 ; i <= m; i++){
for(j = 1; j <= n; j++)
cout << v[i][j] << " ";
cout << '\n';
}
}
How can I improve it?