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);
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I do multiplication in hand form?
by hand form I mean:
Take two numbers as input,
then output in a text box,
the multiplication steps as it would be done by a school student.
so for the inputs 819 and 1358 I wish to output:
8 1 9
1 3 5 8
x_____________
8 1 9 0 0 0
5 4 5 7 0 0
1 0 9 5 0
1 4 5 5 2
+__________________
1 4 7 0 2 0 2
I can of-course get the final answer though the multiplication operation: (a*b)
but that will not let me display the steps.
This should do what you require.
Then modify like following
List<double> lst = new List<double>();
string strInput2 = txtInput2.Text;
for (int i = 0; i < strInput2.Length; i++)
{
double dbl = Convert.ToDouble(txtInput1.Text) * Convert.ToDouble(strInput2[strInput2.Length - (i + 1)].ToString());
string zeros = new String('0', i);
lst.Add(Convert.ToDouble(dbl + zeros));
//richTextBoxResult.Text += lst[i] + Environment.NewLine;
}
for (int i = lst.Count - 1; i >= 0; i--)
{
richTextBoxResult.Text += lst[i] + Environment.NewLine;
}
richTextBoxResult.Text += "________________" + Environment.NewLine;
richTextBoxResult.Text += lst.Sum();
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 try Start the console and after i write 5 numbers is do exsepsion what wrong in code
what the console do is after I write 5 number for example 7,4,9,5,1 ---> 1,5,9,4,7
this what I wrote
static void Main(string[] args)
{
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}
Console.Read();
You missed two things. First - in order to swap two array items, you need to store item somewhere. Otherwise you just replace one item with other. Second - arrays have indexes from 0 to Length - 1. So you need to subtract 1 from second item index:
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}
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.