MrNeedHelpMan
MrNeedHelpMan

Reputation: 5

How do I print the number of a row in a two dimensional array?

I am writing in C# and am trying to number the rows in a printed array. I have no clue how to do it with how I printed the two dimensional array.

It is intended to write like so:

Row 0 # # # # # #

Row 1 # # # # # #

Row 2 # # # # # #

   int[,] array1 = new int[6, 6]
        {
            {10, 20, 10, 20, 21, 99 },
            {2, 27, 5, 45, 20, 13 },
            {17, 20, 20, 33, 33, 20 },
            {21, 35, 15, 54, 20, 37 },
            {31, 101, 25, 55, 26, 66 },
            {45, 20, 44, 12, 55, 98 }
        };

        int Length = array1.GetLength(0);
        int Height = array1.GetLength(1);
        Console.WriteLine("  Col 0  Col 1  Col 2  Col 3  Col 4  Col 5");
        for (int i = 0; i < Length; i++)
        {
            for (int j = 0; j < Height; j++)
            {
                Console.Write(string.Format("{0,6} ", array1[i, j]));
            }
            Console.Write("\n" + "\n");
        }

        Console.ReadKey();

Upvotes: 0

Views: 783

Answers (2)

CPerson
CPerson

Reputation: 1222

You simply can write to the console before printing the value of the array row.

   int[,] array1 = new int[6, 6]
        {
            {10, 20, 10, 20, 21, 99 },
            {2, 27, 5, 45, 20, 13 },
            {17, 20, 20, 33, 33, 20 },
            {21, 35, 15, 54, 20, 37 },
            {31, 101, 25, 55, 26, 66 },
            {45, 20, 44, 12, 55, 98 }
        };

        int Length = array1.GetLength(0);
        int Height = array1.GetLength(1);
        Console.WriteLine("  Col 0  Col 1  Col 2  Col 3  Col 4  Col 5");
        for (int i = 0; i < Length; i++)
        {
            Console.Write("Row {0} ", i); // Outside of the loop :)
            for (int j = 0; j < Height; j++)
            {
                Console.Write(string.Format("{0,6} ", array1[i, j]));
            }
            Console.Write("\n" + "\n");
        }

        Console.ReadKey();

Upvotes: 1

Sach
Sach

Reputation: 10393

You want to write the Row 0, Row 1 etc inside the first loop but outside the second loop, and you should use Console.Write(), not Console.WriteLine().

var array1 = new int[6, 6]
{
    {10, 20, 10, 20, 21, 99 },
    {2, 27, 5, 45, 20, 13 },
    {17, 20, 20, 33, 33, 20 },
    {21, 35, 15, 54, 20, 37 },
    {31, 101, 25, 55, 26, 66 },
    {45, 20, 44, 12, 55, 98 }
};

int Length = array1.GetLength(0);
int Height = array1.GetLength(1);
for (var i = 0; i < Length; i++)
{
    Console.Write("Row {0} : ", i);
    for (var j = 0; j < Height; j++)
    {
        Console.Write("{0,6} ", array1[i, j]);
    }
    Console.WriteLine();
}

Note the Console.WriteLine() at the end of each outer loop iteration which will take you to a new line before the next iteration.

Output

Row 0 : 10 20 10 20 21 99

Row 1 : 2 27 5 45 20 13

Row 2 : 17 20 20 33 33 20

Row 3 : 21 35 15 54 20 37

Row 4 : 31 101 25 55 26 66

Row 5 : 45 20 44 12 55 98

Upvotes: 0

Related Questions