6

I'm trying to build an application in c#.net

Here I am having two single-dimensional arrays of same size For example, I have matrices M and N like the below structure:

       M[0] M[1] M[2] M[3] M[4]
 N[0]   
 N[1]
 N[2]
 N[3]
 N[4]

Here I have assigned my values of M[0].... & N[0]...... to them so that I get a matrix like:

    5     6     4     8
4

8

7

2

Note: I made this values to generate dynamically. I have succeeded till this step.

But I like to store the values in another array (maybe a jagged array or something else) in 2x2 matrix in this format:

      A[0]  A[1]
 B[0]  5     4       (this is the values of M[0] and N[0])

 B[1]  6     4       (this is the values of M[1] and N[0])

 ..............
 B[4]  5     8       (this is the values of M[0] and N[1])

When the first row of N[0] is completed it has to continue with the next row. I just need some for how to implement this in C#??

Vivek Dragon
  • 2,218
  • 4
  • 27
  • 48
  • 2-D arrays: http://www.dotnetperls.com/2d-array or multidimensional arrays: http://msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx – Boomer Jan 07 '13 at 06:21
  • @Boomer thanks man but i ready read all those articles but not got any ideas how to store **dynamically** – Vivek Dragon Jan 07 '13 at 06:25
  • 1
    wtf? do you want to do? its unclear. – Nahum Jan 07 '13 at 06:40
  • Here is my understanding of your question . Do a cartesian product of 2 single-dimensional arrays. Is that right? i.e. if array 1 has elements 1,2,3 and array2 has elements 4,5,6 - you need a 2-dimensional array with values `{{1,4}, {1,5}, {1,6}, {2,4}, {2,5}, {2,6}, {3,4}, {3,5}, {3,6}}`. If yes, look at LINQ and cartesian product related questions. Of course, you can write a loop inside a loop to achieve this. – shahkalpesh Jan 07 '13 at 07:20
  • @shahkalpesh yes thats the exact function – Vivek Dragon Jan 07 '13 at 07:21
  • Here if using LINQ - http://stackoverflow.com/questions/4073713/is-there-a-good-linq-way-to-do-a-cartesian-product – shahkalpesh Jan 07 '13 at 07:21

3 Answers3

2

For Storing dynamically you should know the basics of 2d and 3d

Refer here

2-D arrays: dotnetperls.com/2d-array

multidimensional arrays: msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx

1

You can't late-assign values to arrays. I would recommend that you use List<List<int>>, here is an example:

List<List<int>> val = new List<List<int>>();
List<int> M = new List<int>() { 1, 2, 3, 4, 5 };
List<int> N = new List<int>() { 5, 4, 3, 2, 1 };

foreach (int m in M)
{
    foreach (int n in N)
    {
        val.Add(new List<int> { m, n });
    }
}
Boomer
  • 1,468
  • 1
  • 15
  • 19
1

stackoverflow.com/questions/594853/dynamic-array-in-c-sharp Checkout thread above. Or check source below.

msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx

8bitcat
  • 2,206
  • 5
  • 30
  • 59