vanquish_vi
vanquish_vi

Reputation: 13

Create a table matrix with 2d arrays C#

I am trying to create a program where you ask the user for a number, Create an array that is as close to square as you can and fill it with 1 to the user's specified number. You have to use a 2d array. Here is an example of what it would look like:

user input: 16

output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

I have tried to do it by running it trough loops but it has not worked, I hope someone can guide me on how to do this

Upvotes: 0

Views: 2011

Answers (1)

Raphtaliyah
Raphtaliyah

Reputation: 207

First let's get the user input:

int num;
while(!int.TryParse(Console.ReadLine(), out num) || num < 1)
{
    Console.WriteLine("Invalid number!");
}

after this calculate the side of the square:

var sideLength = (int)Math.Ceiling(Math.Sqrt(num));

then create the 2D array:

var array = new int[sideLength, sideLength];

and write the numbers into it:

for (int row = 0; row < sideLength; row++)
{
    for (int column = 0; column < sideLength; column++)
    {
        var value = row * sideLength + (column + 1);
        if (value > num)
            break;
        array[row, column] = value;
    }
}

but don't write anything higher then the user input, this will leave them 0, which is great because you want to display it from 1 so you can use the 0 to detect the end in case the user input is not a square number.

Now let's prepare a string to format the numbers. If you want all numbers to be the exact same length you can format it with int.ToString("0") where the number of 0s will set the length. Let's create a string which will make all number the same length. The user input is the longest possible number so you can use that to create the string:

var format = new string('0', num.ToString().Length);

Now print the array:

for (int row = 0; row < sideLength; row++)
{
    for (int column = 0; column < sideLength; column++)
    {
        var value = array[row, column];
        if (value == 0)
        {
            Console.WriteLine("");
            goto breakBoth;
        }
        Console.Write($"{value.ToString(format)} ");
    }
    Console.WriteLine("");
}
breakBoth:

This will put the same number of numbers in a line and there will the same amount of rows unless it finds a 0, then it will stop and the last line won't be a full line, this happens if the input wasn't a square number.

Let's look at the outputs:

Input: 16

Output:

01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16

Input: 14

Output:

01 02 03 04
05 06 07 08
09 10 11 12
13 14

If you want to display the 0s if it's not a full line you can remove the 0 check from the print part.

Can this be optimized?

Yes! The array is useless, it just contains incrementing numbers. If 2d arrays are not a requirement you can completely remove it and make some modifications to the print function.

int num;
while(!int.TryParse(Console.ReadLine(), out num))
{
    Console.WriteLine("Invalid number!");
}


var sideLength = (int)Math.Ceiling(Math.Sqrt(num));
var format = new string('0', num.ToString().Length);
for (int row = 0; row < sideLength; row++)
{
    for (int column = 0; column < sideLength; column++)
    {
        var value = row * sideLength + (column + 1);
        if (value > num)
        {
            Console.WriteLine("");
            goto breakBoth;
        }
        Console.Write($"{value.ToString(format)} ");
    }
    Console.WriteLine("");
}
breakBoth:

This code does the same thing but without 2d arrays.

Upvotes: 1

Related Questions