Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
So I'm trying to compute standard deviation in C# and I got the following code:
double M = 0.0;
double S = 0.0;
int period = Values.Length;
int k;
double tmpM;
double value;
for(int i = 0; i < period; i++){
tmpM = M;
value = Values[i];
k = i + 1;
M += (value - tmpM) / k;
S += (value - tmpM) * (value - M);
}
stdev = Math.Sqrt(S / (period - 1));
where Values is an array of data in which I'm trying to obtain the standard deviation
Do you think my code is correct or did I miss something?
Btw I was trying to implement this: http://www.johndcook.com/standard_deviation.html
It doesn't handle the case where period == 1 correctly. In that eventuality you will divide by zero.
Otherwise I believe your code to be correct.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm new to the programming world and I'm learning C#. I have an algorithm given to me in expression language and I need to do it with C# .NET Framework console application, but I'm not very good at it. Can you help?
Here's the algorithm I'm talking about;
Start
int toplam, carpım, karetoplam
toplam=0
carpım=1
Read N
6.Cycle I=1, N, 1
if(N%2==1)
toplam=toplam+I
carpım=carpım*I
else
karetoplam=karetoplam+(I*I)
if over
cycle over
Cw toplam
Cw carpım
Cw karetoplam
Done
I only understood the title of your post not the text ;)
Code is untested. Could be optimized to 1 for-loop in 1 method.
Product of oddnumber from 1 to n?
long GetProductsOfOddNumbers(int n) {
var product = 1L;
for (var i = 1; i <= n; i++) {
if (i % 2 == 0) continue;
product *= i;
}
return product;
}
Sum of squares of even numbers from 1 to n?
long GetSumOfSquaresOfEvenNumbers(int n) {
var sum = 0L;
for (var i = 1; i <= n; i++) {
if (i % 2 != 0) continue;
sum += (i * i);
}
return sum ;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I recently stumbled upon a weird bug in one of my programs.
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + 2 ^ (7 - i);
}
}
In the snippet above, if returnValue = 0, by the time it reaches a set bit at i = 7, the resulting output is 2, which makes no sense (2^0 should be 1). However if I replace the code with:
returnValue = returnValue + Convert.ToInt32(Math.Pow(2,(7 - i)));
It is evaluated correctly to returnValue = 1
The problem is solved by using Math.Pow(), but I would very much like to know why it happens in the initial code.
In C# (and many other languages) the ^ operator is the boolean logical XOR.
See this document for more information about boolean operators in C#.
In addition to the other answers, it is probably simplest if you just use the C# left-shift operator <<, assuming bitArray.Length is less than or equal to 8:
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + 1 << (7 - i);
}
}
If returnValue is a float type and the intention is to allow negative powers of 2, then a slight change yields:
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + ((7-i) >= 0) ? 1 << (7 - i) : 1.0 / ((1 << (7 - i));
}
}
although in this case Math.Pow(2, 7 - i) is much cleaner.
for (int i = 0; i < bitArray.Length; i++)
{
if (bitArray[i])
{
returnValue = returnValue + Math.Pow(2, 7 - i);
}
}
As #IshaySela and #Adomas have said, ^ is a bollean logical XOR.
But in-order to do what you want to do without using math.pow() you can just make an if condition to check if the power is equal ZERO or not.
if (bitArray[i]) {
if ((7 - i) == 0){
//returnValue += 1;
}
else{
//returnValue += 2 ^ (7 - i);
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a string array that I convert to double, I pass it into the following quicksort algorithm and get multiple errors saying I cannot onvert double to int despite having a double array. Left and right are 0 and arr.Length - 1 respectively.
public static double[] quick_Sort(double[] arr, double left, double right)
{
double i, j;
double pivot, temp;
i = left;
j = right;
pivot = arr[(left + right) / 2];
do
{
while ((arr[i] < pivot) && (i < right)) i++;
while ((pivot < arr[j]) && (j > left)) j--;
if (i <= j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
} while (i <= j);
if (left < j) quick_Sort(arr, left, j);
if (i < right) quick_Sort(arr, i, right);
return arr;
}
The problem is that you are using doubles for your array indexes -- which are supposed to be ints, e.g.
arr[i]
You need to use int values for your indexes, e.g.:
int i;
i = Convert.ToInt32(left);
Or just use int instead of double for your all your variables (depending on how your program works). I'm not sure what's best without more information.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have a problem to divide the money value to "0".
i build accounting software, with a shrinking asset.
example, i have a value 1000000.
i would divide it into 3.
1000000 / 3 = 333333,33333333333333333333333
the question is, i would to do this:
step 1 :1000000 - 333333,33333333333333333333333 =
666666,66666666666666666666667
step 2 :666666,66666666666666666666667 -
333333,33333333333333333333333 = 333333,33333333333333333333334
step 3 :333333,33333333333333333333334 -
333333,33333333333333333333333 =
0,00000000000000000000001
example code
decimal value = 100;
int divider = 3;
decimal x = value / divider;
for (int i = 0; i < divider; i++)
{
value = value - x;
}
the result, value = 0,000000000000000000000000001 .
yeah, that is a problem.
that a money value, i would divide to zero.
I've tried using math method like a "math.floor" at each step (on loop).
the result value is -2.
i know i can using "math.floor" method for the final result. but i don't, because i want to do step to step (on loop.)
what should i do?
thanks
You should consider multiplying your 1/3 by 1, 2, or 3 instead of subtracting the previous number. Something like this should work:
var originalAmount = 1000000d;
var thirdOfOriginal = originalAmount / 3;
Console.WriteLine("Original number ... {0}", originalAmount);
Console.WriteLine("Original - 1/3 .... {0}", (originalAmount - (thirdOfOriginal * 1)));
Console.WriteLine("Original - 2/3 .... {0}", (originalAmount - (thirdOfOriginal * 2)));
Console.WriteLine("Original - 3/3 .... {0}", (originalAmount - (thirdOfOriginal * 3)));
// Output
// Original number ... 1000000
// Original - 1/3 .... 666666.666666667
// Original - 2/3 .... 333333.333333333
// Original - 3/3 .... 0
This could be written more generically as:
private static List<decimal> GetAmounts(decimal originalNumber, int numberOfDivisions)
{
var amounts = new List<decimal>();
if (numberOfDivisions > 0)
{
var fraction = originalNumber / numberOfDivisions;
for (int i = 1; i <= numberOfDivisions; i++)
{
amounts.Add(originalNumber - (fraction * i));
}
}
return amounts;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have some algorithm written in c#:
int num3 = 260005;
string text = this.textBox1.Text;
int length = text.Length - 4;
int num5 = ((Convert.ToInt32(text.Substring(4, length)) - 0x7d1) / 2) - 0x7d1;
if (num3 == num5)
{
do somthing!!!
}
Can somebody explain how to get correct input for textBox1? I try some math but i stuck.
I see now. I was confused with 4 character that are trow away.
This code will match what you are looking for... This is because you are throwing away the leading FOUR characters of the text box and then doing a simple equation of ((x - 2001) / 2) - 2001 = 260005 therefore x = 526013:
int num3 = 260005;
string text = "XXXX526013";
int length = text.Length - 4;
int num5 = ((Convert.ToInt32(text.Substring(4, length)) - 0x7d1) / 2) - 0x7d1;
if (num3 == num5)
{
Console.WriteLine("Match!");
}
else
{
Console.WriteLine("No Match! {0}", num5);
}
You can simply reverse your algorithm:
string text = "asdf" + (((num3 + 0x7d1)*2) + 0x7d1);