How to pass matrix to function as parameter in C#

1 Answer

0 votes
using System;

class Program
{
    static void print(int[,] matrix) {
        int rows = matrix.GetLength(0);  
        int cols = matrix.GetLength(1);  
          
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                Console.Write(matrix[i,j] + " ");
            }
            Console.WriteLine();
        }
    }
    static void Main() {
        int[,] matrix = { {1, 2, 3},
                          {4, 5, 6},
                          {7, 8, 9} };
  
        print(matrix);
    }
}





/*
run:
  
1 2 3 
4 5 6 
7 8 9 
  
*/

 



answered Aug 28, 2021 by avibootz
...