9

I dont't have any code for this, but I do want to know how I could do this. I use visual studio 2010 C# if that matters.

Thanks

Jason

kannoli
  • 328
  • 2
  • 5
  • 13
  • 2
    since it is a two dimensional you will have to loop in two dimensions .... i.e. you will need two loops - one inside other. – Guanxi Jun 07 '14 at 05:44
  • 1
    Possible duplicate of [Printing 2D array in matrix format](http://stackoverflow.com/questions/12826760/printing-2d-array-in-matrix-format) – Jeff B Jan 18 '17 at 13:36
  • 1
    FYI, this is the 2nd result on google for "C# print 2d array" ;) – Paul Totzke Feb 04 '17 at 01:51

12 Answers12

40
    public static void Print2DArray<T>(T[,] matrix)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write(matrix[i,j] + "\t");
            }
            Console.WriteLine();
        }
    }
Rezo Megrelidze
  • 2,877
  • 1
  • 26
  • 29
6

you can print it all on one line

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Console.WriteLine(String.Join(" ", array2D.Cast<int>()));

output

1 2 3 4 5 6 7 8
JJS
  • 6,431
  • 1
  • 54
  • 70
2

You should read MSDN:Using foreach with Arrays

int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
// Or use the short form:
// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };

foreach (int i in numbers2D)
{
    System.Console.Write("{0} ", i);
}

// Output: 9 99 3 33 5 55

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
ale
  • 10,012
  • 5
  • 40
  • 49
  • 5
    +1. Did not realize one can iterate over multidimensional array with single `foreach`, also produces almost useless output... – Alexei Levenkov Jun 07 '14 at 06:05
1
//The following are three ways to print any 2d arrays to console:
int[,] twoDArray = new int[3, 4]{ {2,5,55,44},{10,45,5,22 },{ 67,34,56,77} };
                    Console.WriteLine("Array Code Out Method:1");
                    int rows = twoDArray.GetLength(0); // 0 is first dimension, 1 is 2nd 
                                                       //dimension of 2d array 
                    int cols = twoDArray.GetLength(1);
                    for (int i = 0; i < rows; i++)
                    {
                        for (int j = 0; j < cols; j++)
                        {
                            Console.Write("\t" + twoDArray[i, j]);
                            //output: 2       5       55      44      10      45      5       22      67      34      56      77
                        }
                    }

                    Console.WriteLine("Array Code Out Method: 2");
                    for (int x = 0; x <= twoDArray.GetUpperBound(0); x++)
                    {
                        for (int y = 0; y <= twoDArray.GetUpperBound(1); y++)
                        {
                            Console.Write("\t"+ twoDArray[x,y]);
                            //output: 2       5       55      44      10      45      5       22      67      34      56      77
                        }
                    }

                    Console.WriteLine("Array Code Out Method:3");
                    foreach (int items in twoDArray)
                    {
                        Console.Write(""+ "\t" +items);
                        //output:  2       5       55      44      10      45      5       22      67      34      56      77
                    }

                    //string example
                    string[,] friendNames = new string[,] { {"Rosy","Amy"},
                                                      {"Peter","Albert"}
                                                    };
                    foreach (string str in friendNames)
                    {
                        Console.WriteLine(str);
                    }
0

The following is an example...

static void Main()
{
    // A. 2D array of strings.
    string[,] a = new string[,]
    {
        {"ant", "aunt"},
        {"Sam", "Samantha"},
        {"clozapine", "quetiapine"},
        {"flomax", "volmax"},
        {"toradol", "tramadol"}
    };

    // B. Get the upper bound to loop.
    for (int i = 0; i <= a.GetUpperBound(0); i++)
    {
        string s1 = a[i, 0]; // ant, Sam, clozapine...
        string s2 = a[i, 1]; // aunt, Samantha, quetiapine...
        Console.WriteLine("{0}, {1}", s1, s2);
    }

    Console.WriteLine();
}
Tim
  • 28,212
  • 8
  • 63
  • 76
Adel
  • 1,468
  • 15
  • 18
0
int[,] matrix = new int[2, 2] { {2, 2}, {1, 1} };

for (int i = 0; i < matrix.GetLength(0); i++)
{
    for (int k = 0; k < matrix.GetLength(1); k++ )
    {
        //put a single value
        Console.Write(matrix[i,k]);
    }
    //next row
    Console.WriteLine();
}
Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
0
private int[,] MirrorH(int[,] matrix)
    {
        int[,] MirrorHorizintal = new int[4, 4];
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j ++)
            {
                MirrorHorizintal[i, j] = matrix[i, 3 - j];
            }
        }
        return MirrorHorizintal;
    }
0

Try like this..

        int[,] matrix = new int[3, 3]
        {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9},
        };

        int rowLength = matrix.GetLength(0);
        int colLength = matrix.GetLength(1);

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


        Console.Read();
Mário Tomé
  • 667
  • 1
  • 5
  • 12
0

Below is multiple solution to how to implement multidimentional array, I think it is pretty straight forward in c#.

using System;
using System.Collections.Generic;

namespace DataStructure
{
    class Program : SortedZeros
    {
        static void Main(string[] args)
        {
            // Two-Dimensional Array
            int[,] numbers2D = new int[3, 2] 
            { 
                { 9, 99 }, 
                { 3, 33 }, 
                { 5, 55 }
            };

            // 3 * 3
            int[,] dataTest2D = new int[3, 3] 
            { 
                {3, 5, 7}, 
                {4, 3, 8},
                {9, 6, 9},
            };

            // A similar array with string elements.
            int[,] matrix = new int[4, 4]
            {
                 {1, 2, 3, 6},
                 {4, 5, 6, 4},
                 {7, 8, 9, 6},
                 {7, 8, 9, 2},
            };

            int rowLength = matrix.GetLength(0);
            int colLength = matrix.GetLength(0);

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

            // using foreach to print out the 2 * 2 in one straightline
            foreach (int i in numbers2D)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine();

            Console.WriteLine();
            for (int i = 0; i < dataTest2D.GetLength(0); i++)
            {
                for (int j = 0; j < dataTest2D.GetLength(1); j++)
                {
                    Console.Write(string.Format("{0} ", dataTest2D[i, j]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
        }

    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

Explanation:

1. create an array
2. Create 2 for loops (one inside the other)
3. In "j" loop, pass matrix (roomString in our case) as parameter.
4. Add a "+" symbol (concatenate) it with a " " (empty space)
5. In "i" loop, have a "cw tab tab" --> Console.WriteLine();


Code:

 string[,] roomString = new string[4, 4]
        {
            {"0","0","0","0" },
            {"0","0","0","0" },
            {"0","0","0","0" },
            {"0","0","0","0" }

        };


        for (int i = 0; i < roomString.GetLength(0); i++)
        {
            for (int j = 0; j < roomString.GetLength(1); j++)
            {
                Console.Write(roomString[i,j]+" ");
            }
            Console.WriteLine();
        }
Its_707_not_LOL
  • 13
  • 1
  • 2
  • 7
0

Helper function to get any row:

  public static int[] GetRow(int[,] mat, int rowNumber) 
    => Enumerable.Range(0, mat.GetLength(1))
            .Select(i => mat[rowNumber, i])
            .ToArray();

Print row values in new line:

int[,] mat = new int[2,2];
for (int i =0; i < mat.GetLength(0); i++) 
{
  Console.WriteLine(string.Join(",", GetRow(mat, i)));
}
Vivek
  • 104
  • 3
-1

Do it like this:

static public void Print2DArray(int[][] A)
{
    foreach (int[] row in A)
    {
        foreach (int element in row)
        {
              Console.Write(element.ToString() + " ");
        }
        Console.WriteLine();
    }
}
Ricky
  • 2,323
  • 6
  • 22
  • 22
  • 6
    `int[][]` is not a two-dimensional array (it's a "jagged array"). `int[,]` is a two-dimensional array –  Jun 07 '14 at 05:54