c# - Loop through dictionary + multiplying all the values -


i have dictionary (dict), contains string (key) , double (value). want make kind of math:

x = (a * b * ... * n) / ((a * b * ... * n) + ((1 - a) * (1 - b) * ... * (1-n))) 

where a, b, ..., n value dictionary.

foreach (var number in dict) {     = number.value;      x = (a) / (a * (1 - a)); } 

here go:

                dictionary<int, int> dict = new dictionary<int, int>();                 dict.add(1, 10);                 dict.add(2, 20);                 dict.add(3, 30);                 dict.add(4, 40);                  double coe1 = 1;                 double series = 1;                 foreach (var number in dict)                 {                     coe1 *= number.value;                     series *= (1 - number.value);                 }                  double x = coe1 / (coe1 + series); 

and if need intermediate results:

                foreach (var number in dict)                 {                     coe1 *= number.value;                     series *= (1 - number.value);                     console.writeline(coe1 / (coe1 + series));                 } 

Comments