How to compare 2 character strings? - c#

I am making a typing game and I need to compare a generated string and input string character by character to and then calculate % accuracy.
But it seems am missing something because the function does no compare well.
Am I doing something wrong ?
private void Button_Click(object sender, RoutedEventArgs e)
{
int percentage = 0;
int mistakes = 0;
string input = TextBox1.Text;
string letter = TextBox2.Text;
char[] letters1 = letter.ToCharArray();
char[] input1 = input.ToCharArray();
for (int j = 0; j < input1.Length; j++)
{
if (input1[j] != letters1[j])
mistakes = mistakes +1;
else if (input1[j] == letters1[j])
mistakes = mistakes;
}
percentage = 100 - ((mistakes / letters1.Length) * 100);
Label2.Content = percentage;
}

You're performing integer arithmetic and losing the fractional portion of the result of your division. When you divide two integers, the result is another integer (and you lose the fractional part).
percentage = 100 - ((mistakes / letters1.Length) * 100);
Given your code above, and assuming letters1 is 30 characters long and there are 20 mistakes, then 20 / 30 (or any amount of mistakes from the total characters available, except when every character is wrong), will result in:
percentage = 100 - ((20 / 30) * 100);
// 100 - ((0) * 100) == 100
Converting one of those values to a double should retain the fractional portion of the division:
percentage = 100 - ((mistakes / Convert.ToDouble(letters1.Length) * 100);
// = 100 - ((20 / 30.0) * 100) == 100 - ((0.6666666) * 100) == 33.333333

Hope this works for you.
private void Button_Click(object sender, RoutedEventArgs e)
{
float percentage = 0;
float mistakes = 0;
string input = TextBox1.Text;
string letter = TextBox2.Text;
char[] letters1 = letter.ToCharArray();
char[] input1 = input.ToCharArray();
for (int j = 0; j < input1.Length; j++)
{
if (input1[j] != letters1[j])
{
mistakes++;
}
}
percentage = 100 - ((mistakes / letters1.Length) * 100);
Label2.Content = percentage.ToString();
}

Related

How to split an array into intervals with given width and check count how many times, the values have appeared in each intervals C#?

In my array, arr3 has 1000 numbers in it. I have to split this array into k subintervals of width differenceofMaxMin . How can I do that? Later I have to count how many times, the values in arr3 have matched to each interval. But I am stuck at creating intervals from array with a given width.
Any kind of help will be really appreciated!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double[] Statistics1 = new double[500];
double[] Statistics2 = new double[500];
double Alpha1;
double Alpha2;
double RV1;
double RV2;
Random random = new Random();
public double RandomDoubleInclusive() //We are using this method because random.NextDouble() method gives random number
//between 0 and 1 where 0 is inclusive and 1 is exclusive.
//Since the value of probability lies between 0 and 1, both inclusive that's why we need
//to use this method.
{
double d = 0.0;
int i = 0;
do
{
d = random.NextDouble();
i = random.Next(2);
}
while (i == 1 && d > 0);
return d + i;
}
private void label3_Click(object sender, EventArgs e)
{
}
int i,j;
private void button1_Click(object sender, EventArgs e)
{
int SampleSize = Convert.ToInt32(textBox3.Text);
for ( i = 0; i<500;)
{
for (j = 0; j < 500;)
{
Alpha1 = RandomDoubleInclusive();
Alpha2 = RandomDoubleInclusive();
double LnPart = Math.Log(Alpha1);
double part1 = (-2) * LnPart;
double part2 = 2 * 3.14159 * Alpha2;
double CosPart = Math.Cos(part2);
double SinPart = Math.Sin(part2);
RV1 = Math.Sqrt(part1) * CosPart;
Statistics1[i] = RV1;
RV2 = Math.Sqrt(part1) * SinPart;
Statistics2[j] = RV2;
i++;
j++;
}
}
var myList = new List<double>();
myList.AddRange(Statistics1);
myList.AddRange(Statistics2);
double[] arr3 = myList.ToArray();
double Max = arr3.Max();
double Min = arr3.Min();
double differenceofMaxMin = Max - Min; //calculating size of width of interval
double k;
k = Math.Log(SampleSize,2) + 1; //calculating number of subintervals
}
}
I'm not sure I fully understand what exactly you're trying to achieve, but I can certainly try to help you out with an example on how to split an array arr3 into k subintervals with (max) number of elements differenceofMaxMin
var arr3 = Enumerable.Range(0, 1000);
// given: the max number of elements
var differenceofMaxMin = 300;
// determine the number of subintervals
// note that the last subinterval may contain less than differenceofMaxMin elements
var k = (int)Math.Ceiling((double)arr3.Count() / differenceofMaxMin);
var arr3_split = Enumerable.Range(0, k)
.Select(i => arr3.Skip(i * differenceofMaxMin).Take(differenceofMaxMin));
Looking at your method to generate a random double from [0, 1], I think it's overkill since the likelihood to actually draw exactly 1.0 is extremely low.

How to divide a decimal number into rounded parts that add up to the original number?

All Decimal numbers are rounded to 2 digits when saved into application. I'm given a number totalAmount and asked to divide it into n equal parts(or close to equal).
Example :
Given : totalAmount = 421.9720; count = 2 (totalAmount saved into application is 421.97)
Expected : 210.99, 210.98 => sum = 421.97
Actual(with plain divide) : 210.9860 (210.99), 210.9860 (210.99) => sum = 412.98
My approach :
var totalAmount = 421.972m;
var count = 2;
var individualCharge = Math.Floor(totalAmount / count);
var leftOverAmount = totalAmount - (individualCharge * count);
for(var i = 0;i < count; i++) {
Console.WriteLine(individualCharge + leftOverAmount);
leftOverAmount = 0;
}
This gives (-211.97, -210)
public IEnumerable<decimal> GetDividedAmounts(decimal amount, int count)
{
var pennies = (int)(amount * 100) % count;
var baseAmount = Math.Floor((amount / count) * 100) / 100;
foreach (var _ in Enumerable.Range(1, count))
{
var offset = pennies-- > 0 ? 0.01m : 0m;
yield return baseAmount + offset;
}
}
Feel free to alter this if you want to get an array or an IEnumerable which is not deferred. I updated it to get the baseAmount to be the floor value so it isn't recalculated within the loop.
Basically you need to find the base amount and a total of all the leftover pennies. Then, simply add the pennies back one by one until you run out. Because the pennies are based on the modulus operator, they'll always be in the range of [0, count - 1], so you'll never have a final leftover penny.
You're introducing a few rounding errors here, then compounding them. This is a common problem with financial data, especially when you have to constrain your algorithm to only produce outputs with 2 decimal places. It's worse when dealing with actual money in countries where 1 cent/penny/whatever coins are no longer legal tender. At least when working with electronic money the rounding isn't as big an issue.
The naive approach of dividing the total by the count and rounding the results is, as you've already discovered, not going to work. What you need is some way to spread out the errors while varying the output amounts by no more than $0.01. No output value can be more than $0.01 from any other output value, and the total must be the truncated total value.
What you need is a way to distribute the error across the output values, with the smallest possible variation between the values in the result. The trick is to track your error and adjust the output down once the error is high enough. (This is basically how the Bresenham line-drawing algorithm figures out when to increase the y value, if that helps.)
Here's the generalized form, which is pretty quick:
public IEnumerable<decimal> RoundedDivide(decimal amount, int count)
{
int totalCents = (int)Math.Floor(100 * amount);
// work out the true division, integer portion and error values
float div = totalCents / (float)count;
int portion = (int)Math.Floor(div);
float stepError = div - portion;
float error = 0;
for (int i = 0; i < count; i++)
{
int value = portion;
// add in the step error and see if we need to add 1 to the output
error += stepError;
if (error > 0.5)
{
value++;
error -= 1;
}
// convert back to dollars and cents for outputput
yield return value / 100M;
}
}
I've tested it with count values from 1 through 100, all outputs sum to match the (floored) input value exactly.
Try to break it down to steps:
int decimals = 2;
int factor = (int)Math.Pow(10, decimals);
int count = 2;
decimal totalAmount = 421.97232m;
totalAmount = Math.Floor(totalAmount * factor) / factor; // 421.97, you may want round here, depends on your requirement.
int baseAmount = (int)(totalAmount * factor / count); // 42197 / 2 = 21098
int left = (int)(totalAmount * factor) % count; // 1
// Adding back the left for Mod operation
for (int i = 0; i < left; i++)
{
Console.WriteLine((decimal)(baseAmount + 1) / factor); // 21098 + 1 / 100 = 210.99
}
// The reset that does not needs adjust
for (int i = 0; i < count - left; i++)
{
Console.WriteLine((decimal)baseAmount / factor); // 21098 / 100 = 210.98
}

Calculating the correct length of string per line with Page X/Y

I got asked a question and now I am kicking myself for not being able to come up with the exact/correct result.
Imagine we have a function that splits a string into multiple lines but each line has to have x number of characters before we "split" to the new line:
private string[] GetPagedMessages(string input, int maxCharsPerLine) { ... }
For each line, we need to incorporate, at the end of the line "x/y" which is basically 1/4, 2/4 etc...
Now, the paging mechanism must also be part of the length restriction per line.
I have been overworked and overthinking and tripping up on things and this seems pretty straight forward but for the life of me, I cannot figure it out! What am I not "getting"?
What am I interested in? The calculation and some part of the logic but mainly the calculation of how many lines are required to split the input based on the max chars per line which also needs to include the x/y.
Remember: we can have more than a single digit for the x/y (i.e: not just 1/4 but also 10/17 or 99/200)
Samples:
input = "This is a long message"
maxCharsPerLine = 10
output:
This i 1/4 // << Max 10 chars
s a lo 2/4 // << Max 10 chars
ng mes 3/4 // << Max 10 chars
sage 4/4 // << Max 10 chars
Overall the logic is simple but its just the calculation that is throwing me off.
The idea: First, find how many digits is the number of lines:
(n = input.Length, maxCharsPerLine = 10)
if n <= 9*(10-4) ==> 1 digit
if n <= 9*(10-5) + 90*(10-6) ==> 2 digits
if n <= 9*(10-6) + 90*(10-7) + 900*(10-8) ==> 3 digits
if n <= 9*(10-7) + 90*(10-8) + 900*(10-9) + 9000*(10-10) ==> No solution
Then, subtract the spare number of lines. The solution:
private static int GetNumberOfLines(string input, int maxCharsPerLine)
{
int n = input.Length;
int x = maxCharsPerLine;
for (int i = 4; i < x; i++)
{
int j, sum = 0, d = 9, numberOfLines = 0;
for (j = i; j <= i + i - 4; j++)
{
if (x - j <= 0)
return -1; // No solution
sum += d * (x - j);
numberOfLines += d;
d *= 10;
}
if (n <= sum)
return numberOfLines - (sum - n) / (x - j + 1);
}
return -2; // Invalid
}
Usage:
private static string[] GetPagedMessages(string input, int maxCharsPerLine)
{
int numberOfLines = GetNumberOfLines(input, maxCharsPerLine);
if (numberOfLines < 0)
return null;
string[] result = new string[numberOfLines];
int spaceLeftForLine = maxCharsPerLine - numberOfLines.ToString().Length - 2; // Remove the chars of " x/y" except the incremental 'x'
int inputPosition = 0;
for (int line = 1; line < numberOfLines; line++)
{
int charsInLine = spaceLeftForLine - line.ToString().Length;
result[line - 1] = input.Substring(inputPosition, charsInLine) + $" {line}/{numberOfLines}";
inputPosition += charsInLine;
}
result[numberOfLines-1] = input.Substring(inputPosition) + $" {numberOfLines}/{numberOfLines}";
return result;
}
A naive approach is to start counting the line lengths minus the "pager"'s size, until the line count changes in size ("1/9" is shorter than "1/10", which is shorter than "11/20", and so on):
private static int[] GetLineLengths(string input, int maxCharsPerLine)
{
/* The "pager" (x/y) is at least 4 characters (including the preceding space) and at most ... 8?
* 7/9 (4)
* 1/10 (5)
* 42/69 (6)
* 3/123 (6)
* 42/420 (7)
* 999/999 (8)
*/
int charsRemaining = input.Length;
var lineLengths = new List<int>();
// Start with " 1/2", (1 + 1 + 2) = 4 length
var highestLineNumberLength = 1;
var lineNumber = 0;
do
{
lineNumber++;
var currentLineNumberLength = lineNumber.ToString().Length; // 1 = 1, 99 = 2, ...
if (currentLineNumberLength > highestLineNumberLength)
{
// Pager size changed, reset
highestLineNumberLength = currentLineNumberLength;
lineLengths.Clear();
lineNumber = 0;
charsRemaining = input.Length;
continue;
}
var pagerSize = currentLineNumberLength + highestLineNumberLength + 2;
var lineLength = maxCharsPerLine - pagerSize;
if (lineLength <= 0)
{
throw new ArgumentException($"Can't split input of size {input.Length} into chunks of size {maxCharsPerLine}");
}
lineLengths.Add(lineLength);
charsRemaining -= lineLength;
}
while (charsRemaining > 0);
return lineLengths.ToArray();
}
Usage:
private static string[] GetPagedMessages(string input, int maxCharsPerLine)
{
if (input.Length <= maxCharsPerLine)
{
// Assumption: no pager required for a message that takes one line
return new[] { input };
}
var lineLengths = GetLineLengths(input, maxCharsPerLine);
var result = new string[lineLengths.Length];
// Cut the input and append the pager
var previousIndex = 0;
for (var i = 0; i < lineLengths.Length; i++)
{
var lineLength = Math.Min(lineLengths[i], input.Length - previousIndex); // To cater for final line being shorter
result[i] = input.Substring(previousIndex, lineLength) + " " + (i + 1) + "/" + lineLengths.Length;
previousIndex += lineLength;
}
return result;
}
Prints, for example:
This 1/20
is a 2/20
long 3/20
strin 4/20
g tha 5/20
t wil 6/20
l spa 7/20
n mor 8/20
e tha 9/20
n te 10/20
n li 11/20
nes 12/20
beca 13/20
use 14/20
of i 15/20
ts e 16/20
norm 17/20
ous 18/20
leng 19/20
th 20/20

median c# wrong calculate

I have problem with the calculation of the median when I put 1,2, 3 my median is = 44 i don't know why
double wynik = 0;
string x1 = textBox1.Text;
string[] tab = x1.Split(',');
int n = tab.Length;
Array.Sort(tab);
if (n % 2 == 0)
{
double c = x1[(n / 2) -1];
double v = x1[(n / 2)];
wynik = (c + v) / 2;
}
else
wynik = x1[n / 2];
textBox2.Text = wynik.ToString();
That is because 44 is the ASCII value of ,. And in your string, using your current method now, the median is the comma character , value = 44
To get the median, consider of splitting the string by , and then convert each value to numeric data (like int) and then sort it and simply get the middle value among the sorted data..
double wynik = 0;
string x1 = textBox1.Text;
int[] tab = x1.Split(',').Select(x => Convert.ToInt32(x)).ToArray(); //this is the trick
int n = tab.Length;
Array.Sort(tab);
int median = tab[n/2]; //here is your median
Your problem is that you are calculating with characters instead of number.
So let's say your textBox1.Text is "1,2,3". Then x1[(n/2)-1] would point at the character '1', which has the double value of 48 or something.
You need to parse the strings into int using int.Parse:
int[] tab = x1.Split(',').Select(s => int.Parse(s)).ToArray();
And then use these values instead the string again:
if (n % 2 == 0)
{
double c = tab[(n / 2) -1]; // tab instead of x1!
double v = tab[(n / 2)]; // tab instead of x1!
wynik = (c + v) / 2;
}
else
wynik = tab[n / 2]; // tab instead of x1
static void Main(string[] args)
{
Console.WriteLine("Define Array Size");
int size = Convert.ToInt32(Console.ReadLine());
float reference = 0;
int[] newArray = new int[size];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = Convert.ToInt32(Console.ReadLine());
reference = reference + newArray[i];
}
float Median = reference / newArray.Length;
Console.WriteLine("The Median is ="+Median);
}

How to subtract two numbers and add the result to the third number in a sequence?

I am trying to write a C# program that takes an input value from user and then puts that value inside a sequence, then prints the result of every calculated number of the sequence inside a listBox. the sequence is like this:
S = 1 - X + X^2 / 2! - X^3 / 3! + X^4 / 4! - ...
First number that will be shown inside the listBox every time the program starts is 1. i am stuck at the part where i should subtract 1 from the user input (X) and then add the result to the third calculated number of the sequence. For example if user enters 2 as input, first output is 1, second output is -1 and then the program should add the result (-1) to the third calculated number of the sequence which is 2 and output should be 1. again subtract the result (1) from the fourth calculated number and again add the result to the fifth calculated number of the sequence and ...
so far i wrote this:
private void button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
listBox1.Items.Clear();
listBox1.Items.Add("1".ToString());
double numerator = 1, Fact = 1, firstNum = 1, result;
for (int i = 1; i <= 20; i++)
{
numerator = x * numerator;
Fact = Fact * i;
result = numerator / Fact;
firstNum = firstNum - result;
listBox1.Items.Add(firstNum.ToString());
}
But i can't figure out how to write the code for the parts of sequence that should add the result to the next calculated number. Above codes will only subtract the result from next number and prints the result, but now how to add the result of subtraction to the next number and prints it again to the listbox?
First number of sequence - Second number of sequence = Result (show in listbox)
Result + Third number of sequence = Result (show in listbox)
Result - Fourth number of sequence = Result (show in listbox)
Result + Fifth number of sequence = Result (show in listbox)
I am very sorry if i explained the problem bad, i tried to be as specific as possible. I know i'm missing a very tiny piece, but i can't figure out what is it, and sorry for the writing, english is not my native language.
Thanks in advance
You are almost on the right track. Just missing few things.
Here is the corrected version of your program --->
private void button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
listBox1.Items.Clear();
listBox1.Items.Add("1".ToString());
double numerator = 1, Fact = 1, firstNum = 1, result=0;
for (int i = 1; i <= 20; i++)
{
Fact = 1; // Reinitialising every time for new denomerator.
numerator = Math.Pow(x, i); // This is how power is calculated.
for (int j = 1; j<=i; j++) // One more inner loop is needed for Factorial.
Fact = Fact * j;
result = numerator / Fact;
firstNum = firstNum + (Math.Pow(-1, i) * result); // This is the main logic for alternate plus minus.
listBox1.Items.Add(firstNum.ToString());
}
}
I hope it solves your problem. :)
Maybe try to store the result of the subtraction(firstNum - result) to a variable and then add it to the next number?
Looks like the sequence formula is
f(i) = (-x)^i / i! for i in 0..N, 0! = 1
which can be produced as follows
private void button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
listBox1.Items.Clear();
double numerator = 1, denominator = 1;
int i = 0;
while(true)
{
var result = numerator / denominator;
listBox1.Items.Add(result.ToString());;
if (++i > 20) break;
numerator *= -x;
denominator *= i;
}
}
var seqLimit = Convert.ToInt32(Textbox1.Text);
var numberX = Convert.ToInt32(XTextBox.Text);
ListBox1.Items.Clear();
var result = 1.0;
for (var i = 1; i <= seqLimit; i++)
{
ListBox1.Items.Add(result);
if (i%2 == 0) result = result + (Math.Pow(numberX, i)/Factorial(i));
else result = result - (Math.Pow(numberX, i)/Factorial(i));
}
static int Factorial(int n)
{
if (n <= 1)
return 1;
int result = 1;
for (int i = 2; i <= n; i++)
{
result = result * i;
}
return result;
}
Try this, it would work.

Categories

Resources