I am trying to use C# to do math, and in this case I need to repeat a string of code multiple times and then sum the results, the only thing that changes is one number that goes from 2 to 10. (I am aware python might be a better language to do math)
A thought that I had was doing a while loop, but I don't know how to use the while loop to do what I want.
static void Main()
{
double r = 5;
double n = 12;
double fact = 1;
double foct = 1;
double fuct = 1;
double p = 0.85;
{
for (double x = 1; x <= n; x )
{
fact *= x;
}
for (double y = 1; y <= r; y )
{
foct *= y;
}
for (double z = 1; z <= n - r; z )
{
fuct *= z;
}
}
Console.WriteLine((fact / (foct * fuct) * Math.Pow(p, r) * Math.Pow(1 - p, n - r)));
}
More specifically, I need to make the r
go from 2 to 10, and then sum up each result after, which I could do by just repeating the code and summing everything up, but I wonder if there is a way of automating it.
I am new to coding as you may be able to tell, so things might not be so obvious to me.
CodePudding user response:
Perhaps wrapping your function in a for
loop would work. You could then use the index of the loop to set your r
value:
for (int i = 2; i < 11; i )
{
double r = i;
double n = 12;
double fact = 1;
double foct = 1;
double fuct = 1;
double p = 0.85;
for (double x = 1; x <= n; x )
{
fact *= x;
}
for (double y = 1; y <= r; y )
{
foct *= y;
}
for (double z = 1; z <= n - r; z )
{
fuct *= z;
}
Console.WriteLine((fact / (foct * fuct) * Math.Pow(p, r) * Math.Pow(1 - p, n - r)));
}
If you wanted to use those values inside of the for
loop to create a sum, you would then need to employ the same tactics you were using with your other for
loops -- declare the variables outside of the loop:
double sum = 0;
for (int i = 2; i < 11; i )
{
double r = i;
double n = 12;
double fact = 1;
double foct = 1;
double fuct = 1;
double p = 0.85;
for (double x = 1; x <= n; x )
{
fact *= x;
}
for (double y = 1; y <= r; y )
{
foct *= y;
}
for (double z = 1; z <= n - r; z )
{
fuct *= z;
}
// capture the value here to add to sum, we can also print that value then
double value = (fact / (foct * fuct) * Math.Pow(p, r) * Math.Pow(1 - p, n - r));
sum = value;
Console.WriteLine(value);
}
Console.WriteLine("Sum: " sum);
CodePudding user response:
You could make it way shorter by using enumerable and linq I think. By example you could replace the first loop (for the factorial) with :
var fact = Enumerable.Range(1, 12).Aggregate(1, (accumulator, nextValue) => accumulator * nextValue);
Small explanation :
Enumerable.Range(1, 12)
Will create an enumerable containing value from 1 to 12.
.Aggregate(1, (accumulator, nextValue) => accumulator * nextValue);
Will aggregate all values in your enumerable into a single one. The initial value (the seed) will be 1 and for each value in the enumerable the aggregation function will apply (current value (aka accumulator) * next value in the enumerable).
Once you understand the full line you can simply reuse it with different values of course.
Then you can create a method for it to reuse easily :
public static int Factorial(int value)
{
return Enumerable.Range(1, value).Aggregate(1, (accumulator, nextValue) => accumulator * nextValue);
}