0

I'm trying to put numbers on a 2d array by Randomizing the numbers. this is what i got so far but i keep getting 15 and i want it to go through each element in the array not one.

static Random random = new Random();
    static void Main(string[] args)
    {

        int[,] array = new int[3, 5];
        FillArray(array);
        PrintArray(array);
    }

    public static void FillArray(int [ , ] arr)
    {
        for (int c = 0; c < arr.GetLength(1); c++)
        {
            for (int r = 0; r < arr.GetLength(0); r++)
            {
                arr[r, c] = random.Next(15, 96);
            }
        }
    }

    public static void PrintArray(int [,] arr)
    {
        WriteLine(arr.Length);
    }
3
  • 1
    What do you think WriteLine(arr.Length); does? Please read How to Ask and take the tour Commented Apr 22, 2018 at 3:09
  • The Length of the array is 15. That's why you see 15. If you want to print each element in an array, you have to write code to print each element in an array. Commented Apr 22, 2018 at 3:09
  • This question should not be closed - it is not "a problem that can no longer be reproduced or a simple typographical error". This is a perfectly valid question. The OP is just a newbie who needs some simple help. Commented Apr 22, 2018 at 4:56

1 Answer 1

1

Random is working as expected, though I think you might be confused about what Array.Length does

However, you can modify your PrintArray method to show your results

public static void PrintArray(int [,] arr)
{
    for (int c = 0; c < arr.GetLength(1); c++)
    {
        for (int r = 0; r < arr.GetLength(0); r++)
        {
             Console.Write(arr[r, c] + " ");
        }
        Console.WriteLine();
    }
}

Demo Here


Array.Length Property

Gets the total number of elements in all the dimensions of the Array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.