How to cycle through 2d array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Twigger

    How to cycle through 2d array

    Hi,

    I'm trying to write a little program that stores 3 instances of lottery results in a 2d array. My aim is to have each of the separate lottery results printed on a separate line, like this:

    Result 1: 1 2 3 4 5 6 7
    Result 2: 1 2 3 4 5 6 7
    Result 3: 1 2 3 4 5 6 7

    I've got the below code:

    ---------------------------------

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

    for (int i = 0; i < 6; i++)
    {
    Console.Write(r esultsArray[0, i]);
    }

    ---------------------------------

    I'm struggling to figure out how to implement loops to achieve the desired output. Can anyone help? As it stands I have the first set printed, but not the other two. As you can probably tell I'm new to c#.

    Cheers

    Neil
  • Anton Zinchenko
    New Member
    • Sep 2010
    • 16

    #2
    Code:
                for (int i = 0; i < resultsArray.Rank + 1; i++)
                {
                    for (int j = 0; j < resultsArray.Length / (resultsArray.Rank + 1); j++)
                    {
                        Console.Write(resultsArray[i, j]);
                    }
                    Console.WriteLine();
                }

    Comment

    Working...