Kent Karlsson
Kent Karlsson

Reputation: 57

How to read and write data of a 2 dimensional array

I'm making a lotto array game where I'm supposed to write 10 different numbers in to the array and see if I get a bingo. I want it to be 2 dimensional and I have got most of the code right (I have tried it in 1D) but just as I change it to be 2D, I get a problem with the array (array[i]) and I don´t know why.

Here is the code

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace kents.lottospel
 {
     class Program
     {
         static void Main(string[] args)
         {

             Random rand = new Random();
             int randNum = rand.Next(1, 20);

             Console.WriteLine("skriv in nummer");
       
             for(var i= 0; i < 2; i++)
             {
                 for(var j= 0; j < 5; j++)
                 {
                     array[i, j] = int.Parse(Console.ReadLine());

                     if (array[i, j] == randNum) //and this is also 
 one problem (array[i])
                     {
                         Console.WriteLine($"Bing!\nDet rätta talet 
 var" + " " + randNum);
                         break;
                     }
                     else
                     {
                         Console.WriteLine($"tyvärr men du har inte 
 fått nån bingo denna gången");
                     }
                 }
             }
                 Console.WriteLine("skriv in lottoboll nummer" + " 
 " + i + ":");
                 


             Console.WriteLine($"boll nummer" + " " + randNum + " " 
 + "gav bingo");
             Console.WriteLine("slut på spelet.");

         }

     }

 }

Upvotes: 0

Views: 182

Answers (2)

John
John

Reputation: 3182

The whole point of a 2D array is that it has two dimensions. To access an element, you must specify the index in each dimension. That means that you can't use a single for loop to traverse it either. You generally use two nested for loops, e.g.

for (var i = 0; i <= myArray.GetUpperBound(0); i++)
{
    for (var j = 0; j <= myArray.GetUpperBound(1); j++)
    {
        // Use myArray[i, j] here.
    }
}

It's also up to you to make sure that you are consistent with regards to what's a "row" and what's a "column", as they are not explicit in a 2D array.

To be a bit more explicit, here's an example of setting each element in a 2D array and then getting each element:

var myArray = new long[10, 10];

for (var i = 0; i <= myArray.GetUpperBound(0); i++)
{
    for (var j = 0; j <= myArray.GetUpperBound(1); j++)
    {
        myArray[i, j] = DateTime.Now.Ticks;
    }
}

for (var i = 0; i <= myArray.GetUpperBound(0); i++)
{
    for (var j = 0; j <= myArray.GetUpperBound(1); j++)
    {
        Console.WriteLine("({0},{1}): {2}", i, j, myArray[i, j]);
    }
}

Upvotes: 0

lee-m
lee-m

Reputation: 2267

With a two dimensional array, you need two different values to index into it. If you only specify a single value for the index with a 2D jagged array for example, the result will be another array, not a single integer value:

//This will be an int[]
var subArray = array[0]

//This will be an int
var element = array[0][0]

When you are reading input values from the console, you are only specifying one index value for the array so that expression boils down to something like this:

int[] array = new array[5];
array = 42; //compiler error

To populate a 2D array, you will need to change your loop to be a nested one. The outer loop will iterate over the first dimension and the inner loop will iterate over the second dimension:

for(var i = 0; i < 2; i++)
{
    for(var j = 0; j < 5; j++)
    {
        array[i,j] = int.Parse(Console.ReadLine(...));
    }
}

Upvotes: 2

Related Questions