The program must accept five integers A, B, C, D and N as the input. The value of N is always even. The program must form an integer matrix of size N*N. Then the program must fill the matrix layer by layer based on the following conditions.
– In each layer of the matrix, the program must fill the integers A, B, C and D at top-left, top-right, bottom-right and bottom-left corners respectively.
– In the top edge of each layer, the program must fill the remaining integers starting from A+1 (left to right).
– In the right edge of each layer, the program must fill the remaining integers starting from B+1 (top to bottom).
– In the bottom edge of each layer, the program must fill the remaining integers starting from C+1 (right to left).
– In the left edge of each layer, the program must fill the remaining integers starting from D+1 (bottom to top).
Finally, the program must print the N*N integer matrix as the output.
Boundary Condition(s):
1 <= A, B, C, D <= 10^3
2 <= N <= 50
Input Format:
The first line contains A, B, C and D separated by a space.
The second line contains N.
Output Format:
The first N lines contain the integer matrix.
Example Input/Output 1:
Input:
10 20 40 50
6
Output:
10 11 12 13 14 20
54 10 11 12 20 21
53 52 10 20 21 22
52 51 50 40 22 23
51 50 42 41 40 24
50 44 43 42 41 40
Explanation:
Here A = 10, B = 20, C = 40, D = 50 and N = 6.
So the 6*6 integer matrix is formed and filled with the integers based on the given conditions.
10 11 12 13 14 20
54 10 11 12 20 21
53 52 10 20 21 22
52 51 50 40 22 23
51 50 42 41 40 24
50 44 43 42 41 40
Example Input/Output 2:
Input:
11 51 61 31
8
Output:
11 12 13 14 15 16 17 51
37 11 12 13 14 15 51 52
36 35 11 12 13 51 52 53
35 34 33 11 51 52 53 54
34 33 32 31 61 53 54 55
33 32 31 63 62 61 55 56
32 31 65 64 63 62 61 57
31 67 66 65 64 63 62 61
n=list(map(int,input().split()))
m=int(input())
b=[[0]*m for _ in range(m)]
for i in range(m//2):
n1,n2,n3,n4=n
for jj in range(i,m-i-1):
b[i][jj]=n1
n1+=1;
for ii in range(i,m-i-1):
b[ii][m-i-1]=n2
n2+=1;
for jj in range(m-i-1,i,-1):
b[m-i-1][jj]=n3
n3+=1;
for ii in range(m-i-1,i,-1):
b[ii][i]=n4
n4+=1
for i in b:
print(*i)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,d,n,z;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&n);
int mat[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
mat[i][j]=0;
}
}
for(int i=0;i<n/2;i++){
z=0;
for(int j=i;j<n-i-1;j++){
mat[i][j]=a+z;
mat[j][n-1-i]=b+z;
mat[n-1-i][n-j-1]=c+z;
mat[n-j-1][i]=d+z;
z++;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d ",mat[i][j]);
}
printf("n");
}
}
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> z;
int a,b,c,d,n;
void display(){
for(int i=0;i<n;i++){
for(int j:z[i])
cout<<j<<" ";
cout<<endl;
}
}
void fit(int x){
int y=a;
for(int i=x+1;i<n-x-1;i++){
z[x][i]=++y;
}
y=b;
for(int i=x+1;i<n-x-1;i++){
z[i][n-1-x]=++y;
}
y=c;
for(int i=n-2-x;i>x;i--)
z[n-1-x][i]=++y;
y=d;
for(int i=n-2-x;i>x;i--)
z[i][x]=++y;
// display();
}
int main(int argc, char** argv)
{
cin>>a>>b>>c>>d>>n;
z.resize(n,vector<int>(n));
for(int i=0;i<n/2;i++){
z[i][i]=a;
z[i][n-1-i]=b;
z[n-1-i][i]=d;
z[n-1-i][n-i-1]=c;
fit(i);
}
display();
}
import java.util.*;
public class Hello {
public static void main(String[] args) {
//Your Code Here
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int n = sc.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n / 2; i++) {
fill(matrix, i, a, b, c, d, n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
private static void fill(int[][] matrix, int i,
int a, int b, int c, int d, int n) {
matrix[i][i] = a;
for (int j = i + 1; j < n - 1 - i; j++) {
matrix[i][j] = matrix[i][j - 1] + 1;
}
matrix[i][n - 1 - i] = b;
for (int j = i + 1; j < n - 1 - i; j++) {
matrix[j][n-1-i] = matrix[j-1][n-1-i]+1;
}
matrix[n - 1 - i][n - 1 - i] = c;
for (int j = n - 2 - i; j >= i + 1; j--) {
matrix[n-1-i][j] = matrix[n-1-i][j+1]+1;
}
matrix[n - 1 - i][i] = d;
for (int j = n - i - 2; j >= i + 1; j--) {
matrix[j][i] = matrix[j + 1][i] + 1;
}
}
}