c# how to change array to array or arrays - c#

I have this code:
int[] ivrArray = { 1, 0, 0, 0};
int[] agentsArray = { 0, 2, 0, 0 };
int[] abandonedArray = { 0, 0, 3, 0};
int[] canceledArray = { 0, 0, 0, 4};
Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>()
{
{ "IVR", ivrArray },
{ "Agents", agentsArray },
{ "Abandoned", abandonedArray },
{ "Cancelled", canceledArray },
};
The output is
{
"IVR": [
1,
0,
0,
0
],
"Agents": [
0,
2,
0,
0
],
"Abandoned": [
0,
0,
3,
0
],
"Cancelled": [
0,
0,
0,
4
]
}
Is there anyway so the output will be like this:
"Cancelled":[
[0],
[0],
[0],
[4]
]
So each element is array of one element

You can re-project your arrays to a Dictionary<string, int[][]> like so:
var dictionary = new Dictionary<string, int[][]>
{
{"IVR", ivrArray.Select(_ => new[] {_}).ToArray()},
{"Agents", agentsArray.Select(_ => new[] {_}).ToArray()},
{"Abandoned", abandonedArray.Select(_ => new[] {_}).ToArray()},
{ "Cancelled",canceledArray.Select(_ => new[] {_}).ToArray()}
};
(and I guess if you didn't want the local vars, then
...
{"IVR", new [] { 1, 0, 0, 0 }.Select(_ => new[] {_}).ToArray()},
...

you can do like this using Jagged Arrays concept:
for example:
int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] { list1 , list2 , list3 , list4 };
and in dictionary:
Dictionary<string, int[][]> items= new Dictionary<string, int[][]>;
here is MSDN documentation:
http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Related

(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();
}
}

Arrange Array in C# Issue

I've an array sequence 20,40,60,10,30,50. I want to sort this sequence into the following order 60,40,50,20,30,10 in C#.
Any Help? Thanks in advance☺
Very Simple if you have an Array
int[] arr = { 1, 2, 3, 5, 9, 0, 2, 10 };
arr.OrderBy(a => a);
arr.Reverse();
in case of list
List<int> abc = new List<int> { 1, 2, 3, 5, 9, 0, 2, 10 };
abc.Sort();
abc.Reverse();
Just use OrderByDescending of LINQ:
var list = new[] {20, 40, 60, 10, 30, 50};
var newList = list.OrderByDescending(x => x);
Console.WriteLine(string.Join(",", newList)); //60,50,40,30,20,10
You can try this
int[] array = new int[] { 20, 40, 60, 10, 30, 50 };
Array.Sort<int>(array,
new Comparison<int>((element1, element2) => element1.CompareTo(element2)));
to reverse sort
element2.CompareTo(element1)

How can I separate a multi-dimension (2D) array by rows into multiple 1D arrays?

I am programming in C# and I currently have the 2D array below:
int[,] results = {
{ 4, 7, 9, 3, 8, 6, 4},
{ 4, 8, 6, 4, 8, 5, 6},
{ 7, 3, 9, 2, 2, 1, 8}
};
I'd like to create a loop or function which outputs 3 separate arrays, copying the values from each row.
e.g. output:
row1 = {4, 7, 9, 3, 8, 6, 4}
row2 = {4, 8, 6, 4, 8, 5, 6}
etc
I have been able to copy the values of each row into a separate string which is then written in the console with this line of code:
for (int a = 1; a < (rows+1); a++)
{
for (int b = 1; b < (columns+1); b++)
{
scores = scores + " " + results[(a-1), (b-1)];
}
scores = "";
}
you need a convertion from a 2D array into a jagged array
int[,] array = { { 4, 7, 9, 3, 8, 6, 4},
{ 4, 8, 6, 4, 8, 5, 6},
{ 7, 3, 9, 2, 2, 1, 8}
};
int[][] jagged = new int[array.GetLength(0)][];
for (int i = 0; i < array.GetLength(0); i++)
{
int[] row = new int[array.GetLength(1)];
for (int j = 0; j < array.GetLength(1); j++)
{
row[j] = array[i, j];
}
jagged[i] = row;
}
int[] row1 = jagged[0];
int[] row2 = jagged[1];
int[] row3 = jagged[2];
difference between multidimensional array and jagged array:
//multidimensional array
int[,] multi = { { 7, 2, 6, 1 }, { 3, 5, 4, 8 } };
//array of arrays (jagged array)
int[][] jagged = new int[][] { new int[] { 7, 2, 6, 1 }, new int[] { 3, 5, 4, 8 } };
LINQ variant:
var m0 = new int[,] { { 1, 2 }, { 3, 4 } };
var rows = Enumerable.Range(0, m0.GetLength(0)).Select(i => Enumerable.Range(0, m0.GetLength(1)).Select(j => m0[i, j]));
foreach (var r in rows) Console.WriteLine(string.Join(" ", r));
Console.ReadLine();
a 'smarter' variant (flattern an array and take by N values):
var rows = m0.Cast<int>().Select((v, i) => new { i = i / m0.GetLength(1), v }).GroupBy(e => e.i).Select(g => g.Select(e => e.v));
Just a piece of cake using JsonSoft as
var json = JsonConvert.SerializeObject(results);
int[][] arr = JsonConvert.DeserializeObject<int[][]>(json);
int[] arr1 = arr[0];
int[] arr2 = arr[1];
int[] arr3 = arr[2];

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();

C# Creating an array of arrays

I'm trying to create an array of arrays that will be using repeated data, something like below:
int[] list1 = new int[4] { 1, 2, 3, 4 };
int[] list2 = new int[4] { 5, 6, 7, 8 };
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[,] lists = new int[4, 4] { list1 , list2 , list3 , list4 };
I can't get it to work and so I'm wondering if I'm approaching this wrong.
What I'm attempting to do is create some sort of method to create a long list of the values so I can process them in a specific order, repeatedly. Something like,
int[,] lists = new int[90,4] { list1, list1, list3, list1, list2, (and so on)};
for (int i = 0; i < 90; ++i) {
doStuff(lists[i]);
}
and have the arrays passed to doStuff() in order. Am I going about this entirely wrong, or am I missing something for creating the array of arrays?
What you need to do is this:
int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] { list1 , list2 , list3 , list4 };
Another alternative would be to create a List<int[]> type:
List<int[]> data=new List<int[]>(){list1,list2,list3,list4};
The problem is that you are attempting to define the elements in lists to multiple lists (not multiple ints as is defined). You should be defining lists like this.
int[,] list = new int[4,4] {
{1,2,3,4},
{5,6,7,8},
{1,3,2,1},
{5,4,3,2}};
You could also do
int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[,] lists = new int[4,4] {
{list1[0],list1[1],list1[2],list1[3]},
{list2[0],list2[1],list2[2],list2[3]},
etc...};
I think you may be looking for Jagged Arrays, which are different from multi-dimensional arrays (as you are using in your example) in C#. Converting the arrays in your declarations to jagged arrays should make it work. However, you'll still need to use two loops to iterate over all the items in the 2D jagged array.
This loops vertically but might work for you.
int rtn = 0;
foreach(int[] L in lists){
for(int i = 0; i<L.Length;i++){
rtn = L[i];
//Do something with rtn
}
}
The following will give you an array in the iterator variable of foreach
int[][] ar =
{
new []{ 50, 50, 1 },
new []{ 80, 40, 2 },
new []{ 10, 60, 3 },
new []{ 51, 38, 4 },
new []{ 48, 38, 5 }
};

Categories

Resources