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.
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()...
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...
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++ program that inputs a time from the console. The time should be in...
Write a c++ program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits. Your program should then convert the time into a four-digit military time based on a 24-hour clock. Code: #include <iostream> #include <iomanip> #include <string> using namespace std; int main() { string input; cout << "Enter (HH:MM XX) format time: "; getline(cin, input); int h, m; bool am; int len =...
I need this written in C # ASAP Write a C# console program that continually asks...
I need this written in C # ASAP Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen.
wite a program in C to print a right and left pointing arrow pattern using '#'...
wite a program in C to print a right and left pointing arrow pattern using '#' symbol. Do so within 10 lines i.e upper half of the occupies 5 lines and lower half occupies other 5 lones. User will enter 'R' or'r' for printing right pointing arrow. 'l' or 'L' for left pointing arrow.'k'' for invalid option. You must use if-else and while loop for do this.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT