In: Computer Science
Write a C# console program that fills the right to left diagonal
of a square matrix with zeros, the lower-right triangle with -1s,
and the upper left triangle with +1s. Let the user enter the size
of the matrix up to size 21. Use a constant and error check. Output
the formatted square matrix with appropriate values. Refer to the
sample output below.
Sample Run:
*** Start of Matrix ***
Enter the size of square (<= 21): 5
1 1 1 1 0
1 1 1 0 -1
1 1 0 -1 -1
1 0 -1 -1 -1
0 -1 -1 -1 -1
*** End of Matrix **
`Hey,
Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries
using System;
class MainClass {
public static void Main (string[] args) {
int size;
Console.Write("*** Start of Matrix ***\n\nEnter the size of square (<= 21): ");
size = Convert.ToInt32(Console.ReadLine());
if (size < 0 || size > 21) {
Console.WriteLine("Invalid size!!");
return;
}
int[,] arr = new int[size,size];
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
if (i + j == size-1) {
arr[i,j] = 0;
} else if (i + j < size-1) {
arr[i,j] = 1;
} else {
arr[i,j] = -1;
}
}
}
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
Console.Write(arr[i,j] + " ");
}
Console.WriteLine("");
}
}
}
Kindly revert for any queries
Thanks.