return n*n matrix like example

  • Thread starter Thread starter Krishtikri
  • Start date Start date
K

Krishtikri

Guest
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:

Please help me


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("");

}
Console.ReadLine();

}
}
}

Continue reading...
 
Back
Top