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
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 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.
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...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT