-1

I want to know the conditions for the 4 main areas of a matrix besides the diagonals.

For example in the following matrix

A=[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;] 

The elements from the north side would be 2,3,4,8; from the west side would be 6,11,16,12; from the east side 10,15,20,14; and from the south side 22,23,24,18;

All I figured out is that the north part can be written as:

for(i=0;i<n;i++)
    for(j=i+1;j<n-i-1;j++)
        printf("%d",v[i][j])

For the other areas, I'm stuck. Can anyone help me?

  • What do you think `v[j][i]`, `v[n-1-i][n-1-j]` and `v[n-1-j][n-1-i]` give? – Quirk Dec 02 '16 at 18:10
  • Might get this wrong, since my head is fuming from that problem. `v[j][i]` is the way of reading the matrix from top-bottom, left-right instead of the usual left-right, top-bottom. `v[n-1-i][n-1-j]` is from right-left, bottom-top and `v[n-1-j][n-1-i]` is bottom-top right-left. Please correct me if I'm wrong. –  Dec 02 '16 at 18:17
  • Seems right. I suggest you try to run and experiment with it. Often with directions, I simply use standard mathematical line reflection transformations. They just work and I don't give it a second thought. Here at SO, when you post a question, we expect it to be [MVCE](http://stackoverflow.com/help/mcve). That means you should include a sample input and a sample expected output as well. – Quirk Dec 02 '16 at 18:30
  • You should look into [proper C formatting](//prohackr112.tk/r/proper-c-formatting#loops). Or learn how to [thoroughly obfuscate your code](//prohackr112.tk/r/proper-c-obfuscation). – MD XF Dec 02 '16 at 19:19

2 Answers2

1

Suppose we interpret the first index, i, as a row index, and the second, j, as a column index. This is consistent with the usual index order in mathematical notation. C in no way requires such an interpretation, but we must choose a convention by which to interpret the compass-direction region labels, and that one is pretty natural.

The indices of elements v[i][j] on the main diagonal satisfy i == j. Moving up the matrix (to smaller row numbers) reduces i, so matrix elements above the main diagonal satisfy i < j. Similarly, elements below the main diagonal satisfy i > j.

The indices of elements on the secondary diagonal satisfy i == n - 1 - j (assuming zero-based indexing). Again, moving up decreases i and moving down increases it, so elements above this diagonal satisfy i < n - 1 - j, and those below it satisfy i > n - 1 - j.

Each of the four regions of interest is characterized uniquely by whether it is above or below those diagonals, so the index conditions for each region are a combination of the corresponding conditions for being above or below the diagonals. For example, the west region is below the main diagonal and above the secondary, so it follows that its index conditions are i > j && i < n - 1 - j. The index conditions for the other regions can be determined analogously.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

I figured it out, many thanks to John for explaining the process of finding said elements and Quirk for the suggestion.

The condition for the north part is mentioned in the question.

The condition for the south part is:

for(i=0;i<n;i++)
    for(j=i+1;j<n-i-1;j++)
        printf("%d ",v[n-i-1][j]);

For the east part, the code is:

for(i=0;i<n;i++)
    for(j=i+1;j<n-i-1;j++)
        printf("%d ",v[j][n-i-1]);

And for the west part, the code is:

for(i=0;i<n;i++)
    for(j=i+1;j<n-i-1;j++)
        printf("%d ",v[j][i]);

Have a great day, everybody.