-2

I am trying to fit a 36 character string array into a 6x6 multidimensional string array, but am having a lot of trouble finding anything on how I would do this.

This is the string I have:

public static readonly string[] Supported = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

And I want to fit it into this array, but programmatically:

string[,] grid = new string[,] 
        {
            { "0", "1", "2", "3", "4", "5" },
            { "6", "7", "8", "9", "a", "b" },
            { "c", "d", "e", "f", "g", "h" },
            { "i", "j", "k", "l", "m", "n" },
            { "o", "p", "q", "r", "s", "t" },
            { "u", "v", "w", "x", "y", "z" }
        };

Is it realistically possible/simple to do this?

4 Answers4

3

Try something like this:

int width = 6;
string[,] grid = new string[width,width];
for(int i = 0; i < Supported.Length; i++)
{
    grid[i / width, i % width] = Supported[i];
}

As @DragAndDrop pointed out, int width = 6 only works for the specific example you gave. I left this as a variable, rather than hard coding 6 in the grid initialization to show that these values could be adjusted (and to leave room if the matrix isn't going to be square). You should figure out a way to find the values (i.e. MathSqrt(Supported.Length)). Also, there are no bounds checking on grid, be sure to include that when calculating values for grid's length and width.

Matt
  • 25,467
  • 18
  • 120
  • 187
eye_am_groot
  • 682
  • 6
  • 19
1

If you Math.Ceiling(Math.Sqrt(Supported.Length)), that'll give you the size of what your matrix should be (even or uneven matrix).

using System;

public class Program
{
    public static readonly string[] Supported = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

    public static void Main()
    {
        int matrixSize = (int)Math.Ceiling(Math.Sqrt(Supported.Length));
        string[,] matrix = new string[matrixSize, matrixSize];
        for (int i = 0; i < Supported.Length; i++)
        {
            matrix[i / matrixSize, i % matrixSize] = Supported[i];
        }

        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write("{0} ", matrix[i, j]);
            }
            Console.WriteLine();
        }
    }
}

Result:

0 1 2 3 4 5
6 7 8 9 a b
c d e f g h
i j k l m n
o p q r s t
u v w x y z

Took away the first element from Supported:

1 2 3 4 5 6
7 8 9 a b c
d e f g h i
j k l m n o
p q r s t u
v w x y z

Added "A" to the beginning of the original array

A 0 1 2 3 4 5
6 7 8 9 a b c
d e f g h i j
k l m n o p q
r s t u v w x
y z
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

I would do something like this:

class Program
{
    static void Main(string[] args)
    {
        string[] strsArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };

        string[,] strsMatrix = ConvertStrsArrayToStrsMatrix(strsArray, 6);
    }

    private static string[,] ConvertStrsArrayToStrsMatrix(string[] strsArray, int maxAmountRow)
    {
        int max = strsArray.Length / maxAmountRow;
        string[,] strsMatrix = new string[max, maxAmountRow];
        int counter = 0;
        for(int row = 0; row < max; row++)
        {
            for(int column = 0; column < maxAmountRow; column++)
            {
                strsMatrix[row, column] = strsArray[counter];
                counter++;
            }
        }

        return strsMatrix;
    }
}

If you want to support possible excesses:

class Program
{
    static void Main(string[] args)
    {
        string[] strsArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "mustafa" };

        string[,] strsMatrix = ConvertStrsArrayToStrsMatrix(strsArray, 6);
    }

    private static string[,] ConvertStrsArrayToStrsMatrix(string[] strsArray, int maxAmountRow)
    {
        int max = strsArray.Length / maxAmountRow;
        if(max * maxAmountRow < strsArray.Length)
        {
            max++;
        }
        string[,] strsMatrix = new string[max, maxAmountRow];
        int counter = 0;
        for(int row = 0; row < max; row++)
        {
            for(int column = 0; column < maxAmountRow; column++)
            {
                if (counter < strsArray.Length)
                {
                    strsMatrix[row, column] = strsArray[counter];
                    counter++;
                }
                else
                {
                    break;
                }
            }
        }

        return strsMatrix;
    }
}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

Method(1):

Following code can also be used to convert Supported array into 2 Dimensional Array and print the 2D Array.

    public static readonly string[] Supported = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
    static void Main(string[] args)
    {
        int noOfCols = 6;//your own choice to split w.r.t columns
        int noOfRows=Supported.Length/noOfCols;
        string[,] array2D=new string[noOfRows,noOfCols];
        int n = 0, m = 0;
        for(int i=0;i<Supported.Length;i++)
        {
            array2D[n, m] = Supported[i];
            m++;
            if((m=m%noOfCols)==0)
            {
                n++;
            }
        }
    }

Method(2): We can also use the multi-threading process to assign values parallel in 2D array from start to Mid-Point and Last to next of Mid-point of supported Array.But keep in mind, this mechanism will be used where we have large amount of data in single array or in list of array,so you can make its small chunks and can use them to fill by threads parallel as such taking advantage of Multi-Threading.But I'm using only two threads to fill that one by this new method.

Thread thread1 = new Thread(delegate(){
            FillFromZeroToMidIndex(array2D, Supported.Length / 2,noOfCols);
        });
        Thread thread2 = new Thread(delegate()
        {
            FillFromLastToMidUpIndex(array2D, (Supported.Length / 2) + 1, Supported.Length - 1, noOfCols,noOfRows);
        });
        thread1.Start();
        thread2.Start();
        while (thread1.IsAlive || thread2.IsAlive) { }

these are the two methods for thread1 and thread2

static void FillFromZeroToMidIndex(string[,] array2D,int midIndex,int noOfCols)
    {
        int n = 0, m = 0;
        for (int i = 0; i<=midIndex; i++)
        {
            array2D[n, m] = Supported[i];
            m++;
            if ((m = m % noOfCols) == 0)
            {
                n++;
            }
        }
    }
    static void FillFromLastToMidUpIndex(string[,] array2D, int midUpIndex, int lastIndex, int noOfCols, int noOfRows)
    {
        int n = noOfRows-1, m = noOfCols-1;
        for (int i = lastIndex; i >= midUpIndex; i--)
        {
            array2D[n, m] = Supported[i];
            m--;
            if (m<0)
            {
                m = noOfCols-1;
                n--;
            }
        }
    }

You can also make your own logic what you think is better for you choose that one what you want Stay blessed.

Following code will be used to print 2D-Array

        for(int i=0;i<noOfRows;i++)
        {
            for(int j=0;j<noOfCols;j++)
            {
                Console.Write(array2D[i,j]);
                if(j<noOfCols-1)
                {
                    Console.Write(",");
                }
            }
            Console.WriteLine();
        }

Result:

0,1,2,3,4,5
6,7,8,9,a,b
c,d,e,f,g,h
i,j,k,l,m,n
o,p,q,r,s,t
u,v,w,x,y,z
Arslan Ali
  • 450
  • 4
  • 12