3D jagged Array - c#

Is there a way to access my three dimensional jagged array like this:
jaggedArray[1,2,3];
I got the following code snippets so far:
int[][] jaggedArray = new int[3][]
{
new int[] { 2, 3, 4, 5, 6, 7, 8 },
new int[] { -4, -3, -2, -1, 0, 1},
new int[] { 6, 7, 8, 9, 10 }
};
int[,,] dontWork = new int[,,] // expect 7 everywhere in the last dimension
{
{ { 2, 3, 4, 5, 6, 7, 8 } },
{ { -4, -3, -2, -1, 0, 1} },
{ { 6, 7, 8, 9, 10 } }
};

As for the first question, you're trying to access the 3rd element, of the 2nd element of the 1st element of the jagged array:
jaggedArray[1][2][3]
As for the error, a 3D array expects the same number of elements in each element of a dimension. Let's say, for simplicity's sake, that you have a 2D jagged array, a rough representation of what that looks like in memory would be:
First row -> 2, 3, 4, 5, 6, 7, 8
Second row -> -4, -3, -2, -1, 0, 1
Third row -> 6, 7, 8, 9, 10
You can see that each row is seen as a different array, and can therefore differ in size. A multidimensional array, however, does not have this property. It needs to be filled completely:
Column : 0 1 2 3 4 5 6
------------------------------------
First row : 2, 3, 4, 5, 6, 7, 8
Second row: -4, -3, -2, -1, 0, 1
Third row : 6, 7, 8, 9, 10
Your table is missing some cells, which makes no sense. You need to use the same number of elements per dimension.

You got the syntax for declaring 2D jagged array right, 3D jagged arrays are an extension of that. For example:
int[][][] jagged3d = new int[][][]
{
new int[][] { new int[] { 111, 112 }, new int[] { 121, 122, 123 } },
new int[][] { new int[] { 211 } }
}
But to access it, you need different syntax:
jagged3d[0][1][2]

Related

LINQ: Enumerable.Distinct is the order specified or guaranteed? [duplicate]

This question already has answers here:
Does Distinct() method keep original ordering of sequence intact?
(7 answers)
Closed 2 years ago.
This is the example for the usage of Distinct from https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<int> distinctAges = ages.Distinct();
Console.WriteLine("Distinct ages:");
foreach (int age in distinctAges)
{
Console.WriteLine(age);
}
/*
This code produces the following output:
Distinct ages:
21
46
55
17
*/
The algorithm seems to be very simple: Iterate the elements. If the current element is unknown, copy it to the result, otherwise discard it.
I want to know if all implementations are required to give this result, or could a future implementation as well output 55, 21, 17, 46?
From your link:
Remarks
The result sequence is unordered.
and later:
The Distinct<TSource>(IEnumerable<TSource>) method returns an unordered sequence that contains no duplicate values
Seems like it always give them back in the order you put them. I tried and these are the results:
IEnumerable pippo = new List { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 4, 3, 5, 2, 1, 2, 3, 5, 4, 2, 3, 4, 5, 1, 3, 5, 6, 7 };
pippo.Distinct()
Distinct
Iterator { 1, 2, 3, 4, 5, 6, 7 }
pippo.Distinct()
Distinct
Iterator { 1, 2, 3, 4, 5, 6, 7 }
pippo.Distinct()
Distinct
Iterator { 1, 2, 3, 4, 5, 6, 7 }
IEnumerable pippo = new List { 99, 12, 33 ,1,2,3,4,5,6,7,8,11,33,44,1,2,3,4,5,6,7,99};
pippo.Distinct()
Distinct
Iterator { 99, 12, 33, 1, 2, 3, 4, 5, 6, 7, 8, 11, 44 }
pippo.Distinct()
Distinct
Iterator { 99, 12, 33, 1, 2, 3, 4, 5, 6, 7, 8, 11, 44 }
pippo.Distinct()
Distinct
Iterator { 99, 12, 33, 1, 2, 3, 4, 5, 6, 7, 8, 11, 44 }
Even in different executions and instances I got the same result.

Get first element in each row of 2d array

How do you iterate through a 2d array and get the first element in each row? For example:
int[,] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
desired output: 1 4 7 10
Something like this should do the trick:
int[,] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };
for (int row = 0; row < array.GetLength(0); ++row)
{
Console.WriteLine(array[row, 0]);
}
Try it online

Addition of 2 Dimensional Array in C#

int[,] array2D = {{ 4, 7, 9, 3, 8, 6 },{ 4, 8, 6, 4, 8, 5 }};
how to add row 1 and 2
Possibly:
int[] sumRow = Enumerable.Range(0, array2D.GetLength(1))
.Select(i => array2D[0, i] + array2D[1, i]).ToArray();

Take the first five elements and the last five elements from an array by one query using LINQ

I have been recently asked by a co-worker: Is it possible just take the first five elements and the last five elements by one query from an array?
int[] someArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
What I've tried:
int[] someArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var firstFiveResults = someArray.Take(5);
var lastFiveResults = someArray.Skip(someArray.Count() - 5).Take(5);
var result = firstFiveResults;
result = result.Concat(lastFiveResults);
Is it possible to just take the first five elements and the last five elements by one query?
You can use a .Where method with lambda that accepts the element index as its second parameter:
int[] someArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
int[] newArray = someArray.Where((e, i) => i < 5 || i >= someArray.Length - 5).ToArray();
foreach (var item in newArray)
{
Console.WriteLine(item);
}
Output:
0, 1, 2, 3, 4, 14, 15, 16, 17, 18
A solution with ArraySegment<> (requires .NET 4.5 (2012) or later):
var result = new ArraySegment<int>(someArray, 0, 5)
.Concat(new ArraySegment<int>(someArray, someArray.Length - 5, 5));
And a solution with Enumerable.Range:
var result = Enumerable.Range(0, 5).Concat(Enumerable.Range(someArray.Length - 5, 5))
.Select(idx => someArray[idx]);
Both these solution avoid iterating through the "middle" of the array (indices 5 through 13).
In case you are not playing code puzzles with your co-workers, but just want to create a new array with your criteria, I wouldn't do this with queries at all, but use Array.copy.
There are three distinct cases to consider:
the source array has fewer than 5 items
the source array has 5 to 9 items
the source array has 10 or more items
The third one is the simple case, as the first and last 5 elements are distinct and well defined.
The other two require more thought. I'm going to assume you want the following, check those assumptions:
If the source array has fewer than 5 items, you will want to have an array of 2 * (array length) items, for example [1, 2, 3] becomes [1, 2, 3, 1, 2, 3]
If the source array has between 5 and 9 items, you will want to have an array of exactly 10 items, for example [1, 2, 3, 4, 5, 6] becomes [1, 2, 3, 4, 5, 2, 3, 4, 5, 6]
A demonstration program is
public static void Main()
{
Console.WriteLine(String.Join(", ", headandtail(new int[]{1, 2, 3})));
Console.WriteLine(String.Join(", ", headandtail(new int[]{1, 2, 3, 4, 5, 6})));
Console.WriteLine(String.Join(", ", headandtail(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})));
}
private static T[] headandtail<T>(T[] src) {
int runlen = Math.Min(src.Length, 5);
T[] result = new T[2 * runlen];
Array.Copy(src, 0, result, 0, runlen);
Array.Copy(src, src.Length - runlen, result, result.Length - runlen, runlen);
return result;
}
which runs in O(1);
If you are playing code puzzles with your co-workers, well all the fun is in the puzzle, isn't it?
It's trivial though.
src.Take(5).Concat(src.Reverse().Take(5).Reverse()).ToArray();
this runs in O(n).
Try this:
var result = someArray.Where((a, i) => i < 5 || i >= someArray.Length - 5);
This should work
someArray.Take(5).Concat(someArray.Skip(someArray.Count() - 5)).Take(5);
Try this:
int[] someArray = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
var firstFiveResults = someArray.Take(5);
var lastFiveResults = someArray.Reverse().Take(5).Reverse();
var result = firstFiveResults;
result = result.Concat(lastFiveResults);
The second Reverse() reorders the numbers so you won't get 18,17,16,15,14
Please try this:
var result = someArray.Take(5).Union(someArray.Skip(someArray.Count() - 5).Take(5));

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