Array for 4 dimension initializing - c#

An array of 3 dimensions initializes like this.
int[,,] arr = new int[2,3,3] {{{1,2,3},{4,5,6},{7,8,9}}, {{1,2,3},{4,5,6},{7,8,9}}};
How do you initialize this in 4 dimensions array or more?
int[,,,] arr = new int[3,6,5,2] // how to initialize as above?

If you want an example just like yours, it would be like this:
int[, , ,] a = new int[2, 3, 3, 3] {
{
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
},
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
},
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
}
},
{
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
},
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
},
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
}
}
};

Related

Understanding multidimensional array

I am trying to understand the multidimensional array. I can't find a proper article to understand what the each block denotes to.
for 1D, we know that int[] = single row of elements
for 2D, int[,] = the first index denotes total rows and the second one denotes total columns
for 3D, int[,,] = I read from quora, that first index is row, second is col and the third one can be denoted to a page. So x pages with r rows and c columns. So if x is 2, r is 2 and c is 2, then there will be a total of 2 square matrices if I am correct.
for 4D, int[,,,] = He said, a row, column and a row of page of elements (visualise it as a row of cubes) which I cannot understand and there is no answer for 5D and beyond.
Quora answer link: https://qr.ae/pvKcLb
Next I found this article https://www.mathworks.com/matlabcentral/answers/250488-how-a-4d-and-5d-matrix-or-array-look-like-can-we-say-that-a-4d-array-is-another-for-of-3d-or-reshap#answer_197039 which says as below
A vector is an array of scalars.
A 2D matrix is an array of vectors.
A 3D array is an array of 2D matrices.
A 4D array is an array of 3D arrays.
A 5D array is an array of 4D arrays.
Etc.
So, since his answer looked quiet understandable, I tried coding instead of visualizing and I ended up with the below snippet!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PracticeCSharp.CSharp
{
class CSL10
{
public void MultiDArrExample()
{
int[] _1D = new int[] { 1, 2, 3, 4, 5 };
int[,] _2D = new int[,]
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
};
int[,,] _3D = new int[,,]
{
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
},
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
}
};
int[,,,] _4D = new int[,,,]
{
{
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
},
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
}
},
{
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
},
{
{ 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 }
}
}
};
//My head started spinning.. and cant go further..
}
}
}
Can you help to understand this multidimensional array? and what are some real world use cases and what are the alternatives for this multi-D-arrays?
Thanks in advance!
We live in a 3D world, so anything beyond these 3 dimensions becomes difficult for us humans to visualise and imagine.
But this is no problem for a computer. Which can support an almost endless number of dimensions (depending on memory, processing power, etc.)
However, it does mean that it is impossible to find a "real world" example of a 4/5/21 dimensional array. Because there simply aren't any in our world!
See also this article, which explains a bit more and has some more links:
https://www.preposterousuniverse.com/blog/2009/03/30/why-cant-we-visualize-more-than-three-dimensions/
I was trying to understand what each part of an array indicates in order to understand the multi-dimensional array. I understood it then
I just wanted to understand the way of denoting the different portions of an array. And thanks to Caius Jard I understood how it works and I am able to successfully do an object initialization on the multi-dimensional array. I've done upto 6D and it goes on. I am adding the code to the answer, for any one who might need that to understand how the MultiD-Array works
Here is the object initialization code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PracticeCSharp.CSharp
{
class CSL10
{
public void MultiDArrExample()
{
//1 dimensional
//Line
int[] _1D = new int[] { 5, 5 };
//2 dimensional
//Paragraph
int[,] _2D = new int[,]
{
{5, 5 },
{5, 5 }
};
//3 dimensional
//A page with paragraphs
int[,,] _3D = new int[1, 2, 2]
{
{
{5, 5 },
{5, 5 }
}
};
//3 dimensional
//A book with 2 pages and 2,2 paragraphs in each page
int[,,,] _4D = new int[1, 2, 2, 2]
{
//Book 1
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
}
};
//5 dimensional
//A library with 2 books with 2 pages and 2,2 paragraphs in each page
int[,,,,] _5D = new int[1, 2, 2, 2, 2]
{
//Library
{
//Book 1
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
},
//Book 2
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
}
}
};
//6 dimensional
//A town with 2 libraries with 2 books each with 2 pages in each book and 2,2 paragraphs in each page
int[,,,,,] _6D = new int[1, 2, 2, 2, 2, 2]
{
//town
{
//Library 1
{
//Book 1
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
},
//Book 2
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
}
},
//Library 2
{
//Book 1
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
},
//Book 2
{
//Page 1
{
//Para
{5, 5 },
{5, 5 }
},
//Page 2
{
//Para
{5, 5 },
{5, 5 }
}
}
}
}
};
}
}
}
Then Iterating a 6D array
//Iterating a 6D array
int totalTowns = 1;
int totalLibrariesInEachTown = 2;
int totalBooksInEachLibrary = 2;
int totalPagesInEachBook = 2;
int totalRowsInEachPage = 2;
int totalColsInEachPage = 2;
int count = 1;
for (int town = 0; town < totalTowns; town++)
{
for (int library = 0; library < totalLibrariesInEachTown; library++)
{
for (int book = 0; book < totalBooksInEachLibrary; book++)
{
for (int page = 0; page < totalPagesInEachBook; page++)
{
for (int row = 0; row < totalRowsInEachPage; row++)
{
for (int col = 0; col < totalColsInEachPage; col++)
{
Console.WriteLine($"Count: {count++} Reading array location " +
$"[{town},{library},{book},{page},{row},{col}] = " +
_6D[town, library, book, page, row, col]);
}
}
}
}
}
}

(C# Jagged Arrays) Howcome my jagged array counts normally, and not in elements?

In the code below, I am confused about why the line int[,][] jaggedArr = new int [ 2, 3 ] [];
requires to be counted normally, and not in elements like coders would practice.
I initially input it as int[,][] jaggedArr = new int [ 1, 2 ] [];, but wouldnt work.
Can someone please explain why this is so?
class JaggedArrays
{
static void Main (string[] args)
{
int[,][] jaggedArr = new int[2, 3][];
jaggedArr[0, 0] = new int[] { 0, 1, 2, 3, 4 };
jaggedArr[0, 1] = new int[] { 5, 6, 7, 8 };
jaggedArr[0, 2] = new int[] { 9, 10, 11, 12, 13, 14};
jaggedArr[1, 0] = new int[] { 15, 16, 17, 18, 19 };
jaggedArr[1, 1] = new int[] { 20, 21, 22, 23, 24 };
jaggedArr[1, 2] = new int[] { 25, 26, 27 };
Console.WriteLine(jaggedArr[0, 2][2]);
Console.WriteLine(jaggedArr.Length);
Console.ReadLine();
}
}

Distinct values 2d array

I have a 2D array as follows:
long[,] arr = new long[4, 4] {{ 5, 0, 0, 0 },
{ 8, 1, 1, 1 },
{ 0, 3, 0, 6 },
{ 1, 1, 1, 1 }};
How I can display distinct values form this array?
You can use Enumerable.Distinct method:
var uniqueValues = arr.Cast<long>().Distinct();

Get distinct Combinations of numbers using LINQ

The below code returns all distinct combinations based on the logic that 1,2,3 = 3,2,1 = 2,3,1, so it only returns 1 instance of that set of numbers.
However, I want to change that logic so that it returns ALL instances of all number sets.
What do I need to do to the LINQ query below "GetPowerSet" in order to make that happen?
public void GetPowersets()
{
List<int> ints = new List<int>()
{
1,2,2,3,3
};
var results = GetPowerSet(ints);
List<String> combinations = new List<String>();
foreach (var result in results)
{
StringBuilder sb = new StringBuilder();
foreach (var intValue in result.OrderBy(x => x))
{
sb.Append(intValue + ",");
}
combinations.Add(sb.ToString());
}
string c1 = string.Join("|", combinations.ToArray()).Replace(",|", "|");
//c1 = "|1|2|1,2|2|1,2|2,2|1,2,2|3|1,3|2,3|1,2,3|2,3|1,2,3|2,2,3|1,2,2,3|3|1,3|2,3|1,2,3|2,3|1,2,3|2,2,3|1,2,2,3|3,3|1,3,3|2,3,3|1,2,3,3|2,3,3|1,2,3,3|2,2,3,3|1,2,2,3,3,"
}
public IEnumerable<IEnumerable<int>> GetPowerSet(List<int> list)
{
return from m in Enumerable.Range(0, 1 << list.Count)
select
from i in Enumerable.Range(0, list.Count)
where (m & (1 << i)) != 0
select list[i];
}
This is the end result I am trying to achieve: (no duplicate rows of combinations: duplicate = 3,2,1 and 3,2,1 are the same thing. but 1,2,3 and 3,2,1 are NOT the same thing and both should be in the end result)
1
2
3
1,2
1,3
2,1
2,3
2,2
3,1
3,2
3,3
1,2,3
1,2,2
1,3,2
1,3,3
2,1,3
2,1,2
2,3,1
2,3,2
2,3,3
2,2,1
2,2,3
3,1,2
3,1,3
3,2,1
3,2,2
3,2,3
3,3,1
3,3,2
1,2,3,2
1,2,3,3
1,2,2,3
1,3,2,2
1,3,2,3
1,3,3,2
2,1,3,2
2,1,3,3
2,1,2,3
2,3,1,2
2,3,1,3
2,3,2,1
2,3,2,3
2,3,3,1
2,3,3,2
2,2,1,3
2,2,3,1
2,2,3,3
3,1,2,2
3,1,2,3
3,1,3,2
3,2,1,2
3,2,1,3
3,2,2,1
3,2,2,3
3,2,3,1
3,2,3,2
3,3,1,2
3,3,2,1
3,3,2,2
1,2,3,2,3
1,2,3,3,2
1,2,2,3,3
1,3,2,2,3
1,3,2,3,2
1,3,3,2,2
2,1,3,2,3
2,1,3,3,2
2,1,2,3,3
2,3,1,2,3
2,3,1,3,2
2,3,2,1,3
2,3,2,3,1
2,3,3,1,2
2,3,3,2,1
2,2,1,3,3
2,2,3,1,3
2,2,3,3,1
3,1,2,2,3
3,1,2,3,2
3,1,3,2,2
3,2,1,2,3
3,2,1,3,2
3,2,2,1,3
3,2,2,3,1
3,2,3,1,2
3,2,3,2,1
3,3,1,2,2
3,3,2,1,2
3,3,2,2,1
The "foreach" way of doing this, which tends to cause a "Out Of Memory Exception" once the number set gets too large (I anticipate LINQ shouldn't have this problem) is below. This works as I want it to, returning the result set I want. But it is slow and has performance issues. I'm also open to suggestions on how to make it better.
public List<List<int>> GetAllCombinationsOfAllSizes(List<int> ints)
{
List<List<int>> returnResult = new List<List<int>>();
var distinctInts = ints.Distinct().ToList();
for (int j = 0; j < distinctInts.Count(); j++)
{
var number = distinctInts[j];
var newList = new List<int>();
newList.Add(number);
returnResult.Add(newList);
var listMinusOneObject = ints.Select(x => x).ToList();
listMinusOneObject.Remove(listMinusOneObject.Where(x => x == number).First());
if (listMinusOneObject.Count() > 0)
{
_GetAllCombinationsOfAllSizes(listMinusOneObject, newList, ref returnResult);
}
}
return returnResult;
}
public void _GetAllCombinationsOfAllSizes(List<int> ints, List<int> growingList, ref List<List<int>> returnResult)
{
var distinctInts = ints.Distinct().ToList();
for (int j = 0; j < distinctInts.Count(); j++)
{
var number = distinctInts[j];
var newList = growingList.ToList();
newList.Add(number);
returnResult.Add(newList);
var listMinusOneObject = ints.Select(x => x).ToList();
listMinusOneObject.Remove(listMinusOneObject.Where(x => x == number).First());
if (listMinusOneObject.Count() > 0)
{
_GetAllCombinationsOfAllSizes(listMinusOneObject, newList, ref returnResult);
}
}
}
The ANSWER I am looking for is: how to achieve this result set I want , but using LINQ and C# to do it, in a way that is faster and more efficient than the current "foreach" way I have posted?
NEW UPDATE (removed old code, performance is better than OP's code, yielded output)
static IEnumerable<int[]> EnumeratePermutations2(int[] ints)
{
Dictionary<int, int> intCounts = ints.GroupBy(n => n)
.ToDictionary(g => g.Key, g => g.Count());
int[] distincts = intCounts.Keys.ToArray();
foreach (int[] permutation in EnumeratePermutations2(new int[0], intCounts, distincts))
yield return permutation;
}
static IEnumerable<int[]> EnumeratePermutations2(int[] prefix, Dictionary<int, int> intCounts, int[] distincts)
{
foreach (int n in distincts)
{
int[] newPrefix = new int[prefix.Length + 1];
Array.Copy(prefix, newPrefix, prefix.Length);
newPrefix[prefix.Length] = n;
yield return newPrefix;
intCounts[n]--;
int[] newDistincts = intCounts[n] > 0
? distincts
: distincts.Where(x => x != n).ToArray();
foreach (int[] permutation in EnumeratePermutations2(newPrefix, intCounts, newDistincts))
yield return permutation;
intCounts[n]++;
}
}
I didn't touch your GetPowerSet, but instead created a SetComparer to filter the repetitions.
public class SetComparer : IEqualityComparer<IEnumerable<int>>
{
public bool Equals(IEnumerable<int> x, IEnumerable<int> y)
{
return Object.ReferenceEquals(x, y) || (x != null && y != null && x.SequenceEqual(y));
}
public int GetHashCode(IEnumerable<int> set)
{
if (set == null) return 0;
//if you only want one of these 1,2,3 vs 3,2,1
//plug .OrderBy(x => x) before the Aggregate
return set.Aggregate(19, (s,i) => s * 31 + i);
}
}
Just chain .Distinct(new SetComparer()) to the results or at the end of your GetPowerSet select statement.
Based on the question.
Your solution does return all result sets including duplicates. Your solutions does not exclude duplicates. Your Example output does exclude some duplicates but you list 89 sets of data.
There should only be 64 with duplicates as I understand how combinations work which is 2 ^ List.Count() = 2 ^ 6 = 64 combinations
Revised Answer
I believe this is close to what you want. I created a short solution but I think it can be re factored and enhanced for speed. The following Link has some good Set Classes which I think should be used: http://www.codeproject.com/Articles/23391/Set-Collections-for-C
The other thing I would use is the TPL Library which will allow you to speed up the processing. Link: http://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx
My Result Set resulted in 190 sets. With the following code it took about 1.5 minutes to run.
Main Program
void Main()
{
var setList = new List<int>() {1,1,2,3,3,3};
var setSize = setList.Count();
var basePowerSet = PowerSet.Generate(setList);
var results = PowerSet.PS;
// Results generated in 1 Minute 23 seconds with no errors.
var sortedSets = new SortedSet<string>();
foreach( var item in results)
{
sortedSets.Add(item.ToString2());
}
foreach( var item in sortedSets)
{
Console.WriteLine(item);
}
}
PowerSet Library
public static class PowerSet
{
// List with no Exact Duplicates but with Permutations
public static List<List<int>> PS = new List<List<int>>();
// This Method Generates the power set with No Exact Duplicates
// and stores the values into the Property PS.
public static List<List<int>> Generate(List<int> setList)
{
// Generate Base Data to use for final results
var setSize = setList.Count();
var basePowerSet = from m in Enumerable.Range(0, 1 << setSize)
select
from i in Enumerable.Range(0, setSize)
where (m & (1 << i)) != 0
select setList[i];
// Temporary Result Set with Duplicates
var results = new List<List<int>>();
// Step thru each set and generate list of Permutations for each
// Power Set generated above.
foreach( var item in basePowerSet )
{
var size = item.Count();
var positions = from m in Enumerable.Range(0, size)
select m;
var lItem = item.ToList();
// If the set has 2 or more elements in the set then generate Permutations
switch(size)
{
case 0:
case 1:
break;
default:
// Permutations generated from Linq Extension defined
// in Method Permute()
var posList = positions.Permute().ToList();
// remove first item which is a duplicate.
posList.RemoveAt(0);
// Generate new Lists based on all possiable
// combinations of the data in the set.
var x = new List<List<int>>();
foreach(var p in posList)
{
var y = new List<int>();
foreach(var v in p)
{
//v.Dump("xxxx");
y.Add(lItem[v]);
}
x.Add(y);
// Add New Permutation but
// Do not add a duplicate set.
AddNonDuplicate(x);
}
break;
}
// Add to Temp Results;
results.Add(item.ToList());
// Remove Duplicates
AddNonDuplicate(results);
}
return results;
}
// Custom Method used to compare values in a set to the
// Final Result Set named PS.
public static void AddNonDuplicate(List<List<int>> list )
{
//list.Dump();
if(list.Count() == 0)
return;
foreach(var item in list)
{
bool found = false;
var mySize = PS.Count();
if(mySize <= 0)
PS.Add(item);
else
foreach(var psItem in PS)
{
if( item.ToString2() == psItem.ToString2() )
found = true;
}
if(!found)
PS.Add(item);
}
}
}
Extension Library
// My Extension Methods
public static class MyExt
{
public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> list)
{
if (list.Count() == 1)
return new List<IEnumerable<T>> { list };
return list
.Select((a, i1) =>
Permute(list.Where((b, i2) => i2 != i1))
.Select(b => (new List<T> { a }).Union(b)))
.SelectMany(c => c);
}
public static string ToString2<T>(this List<T> list)
{
StringBuilder results = new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, 3, 1, 2, 3, 1 }
{ 3, 3, 1, 3 }
{ 3, 3, 1, 3, 1 }
{ 3, 3, 1, 3, 1, 2 }
{ 3, 3, 1, 3, 2 }
{ 3, 3, 1, 3, 2, 1 }
{ 3, 3, 2 }
{ 3, 3, 2, 1 }
{ 3, 3, 2, 1, 1 }
{ 3, 3, 2, 1, 1, 3 }
{ 3, 3, 2, 1, 3 }
{ 3, 3, 2, 1, 3, 1 }
{ 3, 3, 2, 3 }
{ 3, 3, 2, 3, 1 }
{ 3, 3, 2, 3, 1, 1 }
{ 3, 3, 3 }
{ 3, 3, 3, 1 }
{ 3, 3, 3, 1, 1 }
{ 3, 3, 3, 1, 1, 2 }
{ 3, 3, 3, 1, 2 }
{ 3, 3, 3, 1, 2, 1 }
{ 3, 3, 3, 2 }
{ 3, 3, 3, 2, 1 }
{ 3, 3, 3, 2, 1, 1 }

error Method name expected

private void button1_Click(object sender, EventArgs e)
{
int[] ml = new int[10] ( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
MessageBox.Show(Convert.ToString(ml.Length), "Length");
}
I get this error can someone tell me what I am doing wrong
Error 1 Method name expected C:\Users\Admin\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 21 24 WindowsFormsApplication1
int[] ml = new int[10] ( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
Should be
int[] ml = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
In C# array values are surrounded by { and } and not parens. Switch to using them in the array declaration and the error will go away.
int[] ml = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Categories

Resources