-2

I have to populate array of tuples from array:

My array is = [1,0.004,5,0.03]

It should be moved to array of tuples Tuple<int,double>

(1, 0.004), (5, 0.03)

I m workin with c#. Could you please help me?

ale
  • 61
  • 1
  • 2
  • 14

3 Answers3

3
var tupleList = new List<Tuple<int,double>>();
for(int i = 0; i < array.Length; i += 2)
{tupleList.Add(new Tuple<int,double>((int)array[i], (double)array[i+1]));}
Andrei Neculai
  • 472
  • 2
  • 5
  • 21
0

Play with "%" to know if your number is pair or not during a loop for

CheapD AKA Ju
  • 713
  • 2
  • 7
  • 21
0
var arr = new[] { 1, 0.004, 5, 0.03 };
var arr1 = arr.Where((_, i) => i % 2 == 0);
var arr2 = arr.Where((_, i) => i % 2 == 1);
var result = arr1.Zip(arr2, (a, b) => Tuple.Create((int)a, b));
kwingho
  • 422
  • 2
  • 4