jrliv
6/10/2017 - 10:55 PM

Example of how to fill a multi-dimensional array with random numbers and print it using methods.

Example of how to fill a multi-dimensional array with random numbers and print it using methods.

public static void FillArray(int[,] matrix)  
{  
  Random ranNum = new Random();

  for (int row = 0; row < matrix.GetLength(0); row++)  
  {  
    for (int col = 0; col < matrix.GetLength(1); col++)  
    {  
      matrix[row, col] = ranNum.Next(0, 999);  
    }  
  }  
}

public static void PrintArray(int[,] matrix)
{
  for (int row = 0; row < matrix.GetLength(0); row++)
  {
    for (int col = 0; col < matrix.GetLength(1); col++)
    {
      Console.Write(" " + matrix[row, col]);
    }
    Console.WriteLine();
  }	
}

static void Main(string[] args)
{
  int[,] matrix = new int[4, 3]; 

  FillArray(matrix);
  PrintArray(matrix);
}

//  393 353 529
//  383 720 23
//  257 378 240
//  147 855 384