How to declare 2D integer array in C#? - c#

I am at beginner level of programming, and I would like to know how to declare a 2-dimension array in C#. I did look it up on Google but I couldn't find a solution.
Please answer this question at my level.
Thanks

you can do it like this. See details here
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

2D integer array
Declaration
int[,] array = new int[4, 2];
Initialization
int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Complete explanation with example :http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };
// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };

i think you have not searched google....
follow below link to see tutorial
http://www.tutorialspoint.com/csharp/csharp_multi_dimensional_arrays.htm
int [,] a = int [3,4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};

Use MSDN for Micrsoft technologies, it is well documented http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
It ranked #1 in google search for me when writing: 2d integer array c#
This page might also provide useful information: What are the differences between a multidimensional array and an array of arrays in C#?

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
The sum2D() Method
private double sum2D(double[,] ar)
{
double sum = 0.0;
foreach (double d in ar)
sum += d;
return sum;
}

Related

Zero out subarrays if sums don't match

Given two Lists of integer arrays of the form:
genData = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4}, {1,2,3,4}};
orgData = {{1,2,3,4}, {1,2,3,4}, {2,4,6,8}, {1,2,3,4}};
I'd like to determine if the sum of two subarrays at the same index in both lists don't match. If the sums match, do nothing. If the sums don't match, convert every integer in both subarrays into a 0.
For example, in the two lists above the subarrays at index 2 have a non-matching sum (10 vs 20). I'd like to convert the lists to
genData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
I'm trying to first create a list if sums by trying
var genDataSum = genDataList.ForEach(x => x.Sum());
But obviously, that's throwing up errors..."Cannot assign void to an implicitly typed value".
Any help or guidance will be greatly appreciated.
You need to use select to get the sum. list.foreach works like normal for loop.
List<int[]> genData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 }
};
List<int[]> orgData = new List<int[]>
{
new int[] { 1, 2, 3, 4 },
new int[] { 1, 2, 3, 4 },
new int[] { 2, 4, 6, 8 },
new int[] { 1, 2, 3, 4 }
};
var sumsGenData = genData.Select(a => a.Sum()).ToList();
var sumsOrgData = orgData.Select(a => a.Sum()).ToList();
for (int i = 0; i < sumsGenData.Count; i++)
{
if (sumsGenData[i] != sumsOrgData[i])
{
orgData[i] = new int[] { 0, 0, 0, 0 };
}
}
ForEach doesn't return anything. Use Select.
var orgData = { {1,2,3,4}, {1,2,3,4}, {0,0,0,0}, {1,2,3,4} };
var sums = orgData.Select( a => a.Sum() );

How can I initialize this strange array?

How to initialize this rectangular array of ragged arrays?
int[,][] =
{
// something
}
Same as Mohamed with different variables so you can see the results better :
int[,][] abc =
{
{new int[]{100,101,102}, new int[]{110,111,112}},
{new int[]{200,201,202}, new int[]{210,211,212}}
};
You have an 2 dimensional array of arrays. So you should initialize them like this:
int[,][] inputs = new int[,][]
{
{ new int[]{ 1, 2 }, new int[]{ 3, 4 }, new int[]{ 5, 6 }, new int[]{ 7, 8 } },
{ new int[]{ 1, 2 }, new int[]{ 3, 4 }, new int[]{ 5, 6 }, new int[]{ 7, 8 } }
};
This would be an example...
int[,][] x = new int[1, 1][];
x[0, 0] = new[] { 1, 2, 3 };
I think I found out.
int [,][] arr =
{
{
new int[] {1,3,5},
new int[] {4,6}
},
{
new int[] {1},
new int[] {0,0,0,0,0}
},
{
new int[] {6,6,6,6},
new int[] {1,2,3,4}
}
}

How to get the row or columns of two dimensional array

I am wondering how to get the row of the two dimensional array like
int[3,3] a = ****
I tried a[0], but it failed. anyway to get the rwo array?
Two-dimensional array is not array of arrays. If you want to get 'row', then you need to get all items from array which have same value of some dimension. E.g. if you are getting all values from first dimension:
int[,] array = new int[4, 3] {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
for(int i = 0; i <= array.GetUpperBound(1); i++)
Console.WriteLine(array[0,i]); // getting all items from first dimension
You can put all these items to array:
int rowIndex = 0;
int[] row = Enumerable.Range(0, array.GetUpperBound(1) + 1)
.Select(i => array[1, i])
.ToArray();
Another option will be using jagged-array instead of two-dimensional array:
int[][] array = new []{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
new[] { 10, 11, 12, 13, 14, 15 }
};
It's an array of arrays. But note that inner arrays can have different size. Getting some 'row' will look like
int[] row = array[0];

Declare multi dimensional array

I have this array that I need it to be convert from JS to C#:
var allwinning = new Array(
["000", "001", "002"],
["000", "010", "020"],
["000", "011", "022"],
["000", "100", "200"],
["000", "101", "202"],
["000", "110", "220"],
["001", "002", "003"],
["001", "011", "021"])
The array has to be this way because at one point of the game I will have to compare and match element by element to see if you match the combo to decide if you win.
Should I convert it to List<string> or to ArrayList?
// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };
// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };

Copy only some portion of values from a Two dimentional array

How can i copy only a small portion of two dimensional array to another two dimensional array
int[,] a = new int[3, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
int[,] b = new int[3, 2];
I want array b to hold values like { { 2, 3 }, { 6, 7 }, { 10, 11 } }
Thanks
There you go:
var b = a.Select(_ => _.Skip(1).Take(2).ToArray()).ToArray();

Categories