Advertisement
danzylrabago

Matrix01

Mar 25th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1.  
  2. public class SolutionClass{
  3.  
  4. public void matrix(int n)
  5.  
  6. {
  7.  
  8. int[,] matrix = new int[n, n];
  9.  
  10. int i,j;
  11.  
  12. for(i=0; i<n; i++) { //assign values to the matrix
  13.  
  14. for(j=0; j<n; j++) {
  15.  
  16.  
  17. if(i==j) {
  18.  
  19. matrix[i,j]=0; //if row=column=> fill the matrix with 0
  20.  
  21. }
  22.  
  23.  
  24. else if(i>j) {
  25.  
  26. matrix[i,j]=-1;//if row>columns=> fill matrix with -1
  27.  
  28. } else {
  29.  
  30. matrix[i,j]=1; //if row<columns=> fill matrix with 1
  31.  
  32. }
  33. }
  34.  
  35. }
  36.  
  37. for(i=0; i<n; i++) { //print the array
  38.  
  39. for(j=0; j<n; j++) {
  40.  
  41. Console.Write(matrix[i,j] + " ");
  42.  
  43. }
  44.  
  45. Console.WriteLine();
  46.  
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement