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];
Related
How can I check if all the elements from one array are in another array? I have a 2d array with 3 arrays in it,and I want to check all of those 3 arrays if they have all the elements from allnumbers. array1=allnumbers ? array2=allnumbers ? array1=allnumber2 ? I need to return true if at least one contains all the elements from allnumbers. I have the code bellow,but I need it not to contain more than 3 control flow statements.
// int[,][] array = {array1, array2, array3}
static bool CheckLine(int[,][] array)
{
const int maxL = 9;
bool result = false;
int[] allnumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (var singlearray in array)
{
int[] arr = singlearray;
int p = 0;
foreach (var num in allnumbers)
{
foreach (var arraynumber in singlearray)
{
if (arraynumber == num)
{
p++;
}
}
if (p == maxL)
{
result = true;
break;
}
}
}
return result;
}
If the values in your array are unique, and you don't care about the order they're in, this is a job for HashSet. (In other words, if your arrays contain sets of numbers you can treat them as sets.) Here's the basic outline of comparing sets.
var allnumbersSet = new HashSet<int>(allnumbers);
var allnumbers2Set= new HashSet<int>(allnumbers2);
if (allnumbersSet.IsSupersetOf(allnumbers2Set)) {
/* everything in allnumbers2 is also in allnumbers1 */
}
The people who put together DotNet did a really good job creating and optimizing those collection classes; you can use them with confidence to get good performance.
It seems, that you have two-dimensional jagged array. You can simplify your code by using Except and check the difference between allnumbers array and single row at every loop iteration.
static bool CheckLine(int[,][] array)
{
int[] allnumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (var singlearray in array)
{
var diff = allnumbers.Except(singlearray);
if (!diff.Any())
{
return true;
}
}
return false;
}
If there is no elements in a difference, it'll mean that single item from source 2D array has all elements from allnumbers array.
Example of the usage
var array = new int[2, 2][];
array[0, 0] = new[] { 1, 2, 8 };
array[0, 1] = new[] { 3, 4, 5, 6 };
array[1, 1] = new[] { 3, 2, 1, 4, 5, 7, 6, 10, 9, 8 };
array[1, 0] = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
CheckLine(array);
The last two items satisfy the condition, execution will break and return true for { 3, 2, 1, 4, 5, 7, 6, 10, 9, 8 } array. Also don't forget to add using System.Linq directive at the top of file
Thank you for your help. I forgot to mention that I can use only "using System;"
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() );
Please how do I sort the following array (in C#) by the second column desc, then by the first column asc (and then insert the resulting rank in the third column which I guess might be a separate question).
I'm a beginner in C#, I have spent quite some time looking for an answer to this seemingly simple question, but I couldn't find any approach that would work for me. Any help is much appreciated, thanks.
int[,] myArray = new int[5, 4]
{
{1, 7, 0, 0} ,
{2, 12, 0, 0} ,
{3, 15, 0, 0} ,
{4, 7, 0, 0} ,
{5, 1, 0, 0}
};
In a multidimensional array, there isn’t really a concept of “rows”. All values are just in the same relation to another. That’s why sorting multidimensional arrays is somewhat tricky.
It gets easier when you use jagged arrays, basically arrays of arrays. Your array would look like this:
int[][] myJaggedArray = new int[][]
{
new int[] { 1, 7, 0, 0 },
new int[] { 2, 12, 0, 0 },
new int[] { 3, 15, 0, 0 },
new int[] { 4, 7, 0, 0 },
new int[] { 5, 1, 0, 0 },
};
You can also convert your multidimensional array into a jagged array, doing something like this:
int[][] myJaggedArray = new int[myArray.GetUpperBound(0) + 1][];
for(int i = 0; i < myJaggedArray.Length; i++)
myJaggedArray[i] = Enumerable.Range(0, myArray.GetUpperBound(1) + 1).Select(k => myArray[i,k]).ToArray();
Once you have a jagged array, sorting it is really easy using LINQ:
var result = myJaggedArray
.OrderBy(row => row[1])
.ThenBy(row => row[0])
.Select((row, idx) =>
{
row[2] = idx;
return row;
})
.ToArray();
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;
}
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();