Question

In: Computer Science

Write a C# console program that fills the right to left diagonal of a square matrix...

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 **

Solutions

Expert Solution

`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.


Related Solutions

(C++) Write a program that randomly fills in 0s and 1s into a 5-by-5 matrix,prints the...
(C++) Write a program that randomly fills in 0s and 1s into a 5-by-5 matrix,prints the matrix,and finds which row and column with more 1s and display that row and column number.            
Give an example of a square matrix A such that all the diagonal entries are positive...
Give an example of a square matrix A such that all the diagonal entries are positive but A is not positive definite
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Write in c++ as simple as possible In a right triangle, the square of the length...
Write in c++ as simple as possible In a right triangle, the square of the length on one side is equal to the sum of the squares of the lengths of the other two sides.  Write a program that prompts the user to enter the length of three sides of the tringle (as doubles) and the outputs a message indication whether the triangle is a right triangle.  You should split this into two functions (but only one .cpp file).  One function is main()...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT