# return n*n matrix like example

• ### Question

• HI everyone,

I need help with this:

Given a number 𝑛 return an 𝑛 * 𝑛 matrix of numbers like the following:
𝑛 = 3
111
121
111
𝑛 = 5
11111
12221
12321
12221
11111
Given the two examples. Understand what matrix you need to return for
each 𝑛 and write a program to return it.

As I can understand,  for n=7 we need to return this matrix:

1111111

1222221

1233321

1234321

1233321

1222221

1111111

I understand how to fill matrix element with 4, but then I can't fill another elements:

int n = 7;

int[,] matrix = new int[n, n];

int k = (n + 1) / 2;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j && i == (n - 1) / 2)
matrix[i, j] = k;

}
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write(" " + matrix[i, j]);

Console.WriteLine("");

}

}
}
}

Tuesday, April 10, 2018 10:13 AM

• Try presenting this solution:

```int n = 7;
int[,] matrix = new int[n, n];
var r = Enumerable.Range( 0, n );
r.All( i => r.All( j => { matrix[i, j] = new[] { i, j, n - i - 1, n - j - 1 }.Min() + 1; return true; } ) );
```

Then display the results.

• Marked as answer by Thursday, April 12, 2018 8:34 AM
Tuesday, April 10, 2018 11:23 AM

### All replies

• Try presenting this solution:

```int n = 7;
int[,] matrix = new int[n, n];
var r = Enumerable.Range( 0, n );
r.All( i => r.All( j => { matrix[i, j] = new[] { i, j, n - i - 1, n - j - 1 }.Min() + 1; return true; } ) );
```

Then display the results.

• Marked as answer by Thursday, April 12, 2018 8:34 AM
Tuesday, April 10, 2018 11:23 AM

Can you explain how it works?

`r.All( i => r.All( j => { matrix[i, j] = new[] { i, j, n - i - 1, n - j - 1 }.Min() + 1; return true; } ) );`
• Marked as answer by Thursday, April 12, 2018 8:34 AM
• Unmarked as answer by Thursday, April 12, 2018 8:34 AM
Tuesday, April 10, 2018 11:30 AM
• In this approach, for each cell, it calculates the distances between the cell and all of four margins, then determine the minimum, and write it to the cell.

You can rewrite it using for loops.

Tuesday, April 10, 2018 11:49 AM
• ok, I will try with for loops

thanks!

Tuesday, April 10, 2018 12:11 PM
• Just think about how you would do this as a human computer.  You'd put 1s around the outside edge, then move in one layer and do the 2s, then in one more layer and do the 3s, etc.  That means two nested loops: one for the layer, running 0 to (N+1)/2, and one to count along the edge of that layer.

The first loop runs from 0 to N-1. You shove a 1 into matrix[0,i], matrix[i,0], matrix[n-1,i] and matrix[i,n-1]  That fills the outside edge.

The second loop runs from 1 to N-2.  You shove a 2 into matrix[1,i], matrix[i,1], matrix[n-2,i] and matrix[i,n-2].  That fills the next layer.

See the pattern?  Once you see the pattern, it becomes easy to write the code.

Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

Tuesday, April 10, 2018 6:05 PM
• thanks!
Thursday, April 12, 2018 8:34 AM
•   int n = 7;

int[,] matrix = new int[n, n];

int k = (n + 1) / 2;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j && i == (n - 1) / 2)
matrix[i, j] = k;

}
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write(" " + matrix[i, j]);

Console.WriteLine("");

}