CE20201E Editorial

PROBLEM LINK:

contest: CodeChef: Practical coding for everyone
practice: Contest Page | CodeChef

Author: nash_057
Tester: shanvid
Editorialist: shanvid](shanvid | CodeChef User Profile for Shantanu Vidwans | CodeChef)

DIFFICULTY:

Easy

PREREQUISITES:

Maths, Matrix

PROBLEM:

Form a square matrix whose border is made of numbers, in such a manner that the border is made of a single number. Start from number 1 in the outer border and increase the number as the border converges. Each Matrix with Size S will be a S x S Matrix.

EXPLANATION:

Eg. If size is 5,

Output should be:

  1    1   1   1   1 
  1    2   2   2   1
  1    2   3   2   1
  1    2   2   2   1
  1    1   1   1   1

Or if size Is 4:

  1    1   1   1
  1    2   2   1
  1    2   2   1
  1    1   1   1

Input:

  • First line will contain SS, size of matrix.

Output:

Output is a space separated matrix

Constraints

  • 3≤S≤1000

SOLUTIONS:

#include

using namespace std;

main()
{
int i, j,n,k;
cout<<“\n Enter the sie of matrix”;
cin>>n;
k=0;
int a[n][n];
while(k<=n/2)
{
for(i=k;i<n-k;++i)
{
for(j=k;j<n-k;++j)
{
if(i==k || j==k || (i==n-k-1) || (j==n-k-1))
a[i][j]=k+1;
}
}
++k;
}
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
cout<<a[i][j]<<" “;
}
cout<<”\n";
}

}