How to print 2D array in matrix format with C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            long[,] arr = new long[3, 4] { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 } };

            int rows = arr.GetLength(0);
            int cols = arr.GetLength(1);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write(string.Format("{0} ", arr[i, j]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
        }
    }
}


/*
run:
 
1 1 1 1
 
2 2 2 2
 
3 3 3 3
 
*/


 



answered Apr 29, 2017 by avibootz

Related questions

2 answers 226 views
1 answer 206 views
1 answer 136 views
136 views asked Apr 13, 2018 by avibootz
1 answer 142 views
142 views asked Apr 13, 2018 by avibootz
1 answer 140 views
1 answer 127 views
127 views asked Apr 13, 2018 by avibootz
...