First and last two numbers of an integer windows phone - c#

How to get the first two numbers of an integer and the last two in c#.
For example:
int complete = 1935;
how to make,
int firtTwo = 19;
and
int secondTwo = 35;
Please help.

Is it always a four-digit positive value? If so, I'd go for:
int hundreds = complete / 100;
int tensAndUnits = complete % 100;
If it's any arbitrary number, so you'd want "935" to be "93" and "35" you might as well use string operations:
string text = complete.ToString();
int first = int.Parse(text.Substring(0, 2));
int last = int.Parse(text.Substring(text.Length - 2, 2));
(Note that this will blow up for single-digit values...)
It would help if you could say what this is meant to be for though.

Related

C# - How to find the average from the numbers given by the textbox

I am pretty new on the programming field. I started learning C# and I have a problem.
I need to find the average of the given numbers from textboxes.
int wage1 = int.Parse(textBox_wage1.Text);
int wage2 = int.Parse(textBox_wage2.Text);
int wage3 = int.Parse(textBox_wage3.Text);
int wage4 = int.Parse(textBox_wage4.Text);
int wage5 = int.Parse(textBox_wage5.Text);
int wage6 = int.Parse(textBox_wage6.Text);
int wage7 = int.Parse(textBox_wage7.Text);
var average = new int[] { wage1, wage2, wage3, wage4, wage5, wage6, wage7 };
double AverageBruto = Queryable.Average(average.AsQueryable());
lbl_AverageBruto.Text = "Average: " + AverageBruto;`
This works perfectly fine if the user has filled all 7 textboxes, but if they didn't fill at least one of them, it causes an error. can someone help me figure out how I can find the average if they fill only 3 or 4 of the textboxes? Thank you.
You likely don't want to be using an array for this, because if null values are represented by zeros then the average will be off. The wage ints should probably be in an array (The textBox_wages as well but it's not necessary for this example) so you can iterate through them, check for null values, and only add those that aren't null to a list you'll take the average from. Here's one way you could approach it:
int[] wages = new int[8];
int wages[0] = int.Parse(textBox_wage1.Text);
int wages[1] = int.Parse(textBox_wage2.Text);
int wages[2] = int.Parse(textBox_wage3.Text);
int wages[3] = int.Parse(textBox_wage4.Text);
int wages[5] = int.Parse(textBox_wage5.Text);
int wages[6] = int.Parse(textBox_wage6.Text);
int wages[7] = int.Parse(textBox_wage7.Text);
var AverageBruto = new List<int>();
foreach (int x in wages) if (x != null) AverageBruto.Add(i);
AverageBruto = AverageBruto.average();
lbl_AverageBruto.Text = "Average: " + AverageBruto;
You'll have to use System.Linq for this method though.
Let's organize the TextBoxes and then query with a help of Linq:
double AverageBruto = new TextBox[] {
textBox_wage1,
textBox_wage2,
textBox_wage3,
textBox_wage4,
textBox_wage5,
textBox_wage6,
textBox_wage7,
}
.Where(box => !string.IsNullOrWhiteSpace(box.Text))
.Average(box => int.Parse(box.Text));
lbl_AverageBruto.Text = $"Average: {AverageBruto}";
Here, with a help of Where we choose only TextBoxes which have some Text

How to take digits from two different numbers and form a new one

I have the following problem here:My input is several lines of 2 digit numbers and I need to make a new number using the second digit of the first number and the first of the next one.
Example:
int linesOfNumbers = Convert.ToInt32(Console.ReadLine());
for(int i = 0,i<linesOfNumbers,i++)
{
int numbers = Conver.ToInt32(Console.ReadLine());
//that's for reading the input
}
I know how to separate the numbers into digits.My question is how to merge them.
For example if your input is 12 and 21 the output should be 22.
I like oRole's answer, but I think they're missing a couple things with the example input that you provided in your comment. I'll also point out some of the errors in the code that you have.
First off, if you're only given the input 12,23,34,45, then you don't need to call Console.ReadLine within your for loop. You've already gotten the input, you don't need to get any more (from what you've described).
Secondly, unless you're doing mathematical operations, there is no need to store numerical data as ints, keep it as a string, especially in this case. (What I mean is that you don't store Zip Codes in a database as a number, you store it as a string.)
Now, onto the code. You had the right way to get your data:
var listOfNumbers = Console.ReadLine();
At that point, listOfNumbers is equal to "12,23,34,45". If you iterate on that variable as a string, you'll be taking each individual character, including the commas. To get each of the numbers to operate on, you'll need to use string.Split.
var numbers = listOfNumbers.Split(',');
This turns that list into four different two character numbers (in string form). Now, you can iterate over them, but you don't need to worry about converting them to numbers as you're operating on the characters in each string. Also, you'll need a results collection to put everything into.
var results = new List<string>();
// Instead of the regular "i < numbers.Length", we want to skip the last.
for (var i = 0; i < numbers.Length - 1; i++)
{
var first = numbers[i];
var second = numbers[i + 1]; // This is why we skip the last.
results.Add(first[1] + second[0]);
}
Now your results is a collection of the numbers "22", "33", and "44". To get those back into a single string, you can use the helper method string.Join.
Console.WriteLine(string.Join(",", results));
You could use the string-method .Substring(..) to achieve what you want.
If you want to keep int-conversion in combination with user input, you could do:
int numA = 23;
int numB = 34;
int resultAB = Convert.ToInt16(numA.ToString().Substring(1, 1) + numB.ToString().Substring(0, 1));
Another option would be to take the users input as string values and to convert them afterwards like that:
string numC = "12";
string numD = "21";
int resultCD = Convert.ToInt16(numC.Substring(1, 1) + numD.Substring(0, 1));
I hope this code snippet will help you combining your numbers. The modulo operator (%) means: 53 / 10 = 5 Rest 3
This example shows the computation of the numbers 34 and 12
int firstNumber = 34 - (34 % 10) // firstNumber = 30
int secondNumber = 12 % 10; // secondNumber = 2
int combined = firstNumber + secondNumber; // combined = 32
EDIT (added reading and ouput code):
boolean reading = true;
List<int> numbers = new ArrayList();
while(reading)
{
try
{
int number = Convert.ToInt32(Console.ReadLine());
if (number > 9 && number < 100) numbers.Add(number);
else reading = false; // leave reading process if no 2-digit-number
}
catch (Exception ex)
{
// leave reading process by typing a character instead of a number;
reading = false;
}
}
if (numbers.Count() > 1)
{
List<int> combined = new ArrayList();
for (int i = 1; i <= numbers.Count(); i++)
{
combined.Add((numbers[i-1] % 10) + (numbers[i] - (numbers[i] % 10)));
}
//Logging output:
foreach (int combination in combined) Console.WriteLine(combination);
}
As you mention, if you already have both numbers, and they are always valid two digit integers, following code should work for you.
var num1 = 12;
var num2 = 22;
var result = (num2 / 10)*10 + (num1 % 10);
num2/10 returns the first digit of second number, and num1 % 10 returns the second digit of the first number.
The % and / signs are your savior.
If you want the 'ones' digit of a number (lets call it X), simply do X%10 - the remainder will be whatever number is in the 'ones' digit. (23%10=3)
If, instead, the number is two digits and you want the 'tens' digit, divide it by ten. (19/10=1).
To merge them, multiply the number you want to be in the 'tens' digit by ten, and add the other number to it (2*10+2=22)
There are other solutions like substring, etc and many one have already given it above. I am giving the solution VIA LINQ, note that this isn't efficient and it's recommended only for learning purpose here
int numA = 12;
int numB = 21 ;
string secondPartofNumA = numA.ToString().Select(q => new string(q,1)).ToArray()[1]; // first digit
string firstPartofNumB = numB.ToString().Select(q => new string(q,1)).ToArray()[0]; // second digit
string resultAsString = secondPartofNumA + firstPartofNumB;
int resultAsInt = Convert.ToInt32(resultAsString);
Console.WriteLine(resultAsString);
Console.WriteLine(resultAsInt);

How to get count of numbers in int and how to split a number without making a string

I have a number like 601511616
If all number's length is multiple of 3, how can a split the number into an array without making a string
Also, how can I count numbers in the int without making a string?
Edit: Is there a way to simply split the number, knowing it's always in a multiple of 3... good output should look like this: {616,511,601}
You can use i % 10 in order to get the last digit of integer.
Then, you can use division by 10 for removing the last digit.
1234567 % 10 = 7
1234567 / 10 = 123456
Here is the code sample:
int value = 601511616;
List<int> digits = new List<int>();
while (value > 0)
{
digits.Add(value % 10);
value /= 10;
}
// digits is [6,1,6,1,1,5,1,0,6] now
digits.Reverse(); // Values has been inserted from least significant to the most
// digits is [6,0,1,5,1,1,6,1,6] now
Console.WriteLine("Count of digits: {0}", digits.Count); // Outputs "9"
for (int i = 0; i < digits.Count; i++) // Outputs "601,511,616"
{
Console.Write("{0}", digits[i]);
if (i > 0 && i % 3 == 0) Console.Write(","); // Insert comma after every 3 digits
}
IDEOne working demonstration of List and division approach.
Actually, if you don't need to split it up but only need to output in 3-digit groups, then there is a very convenient and proper way to do this with formatting.
It will work as well :)
int value = 601511616;
Console.WriteLine("{0:N0}", value); // 601,511,616
Console.WriteLine("{0:N2}", value); // 601,511,616.00
IDEOne working demonstration of formatting approach.
I can't understand your question regarding how to split a number into an array without making a string - sorry. But I can understand the question about getting the count of numbers in an int.
Here's your answer to that question.
Math.Floor(Math.Log10(601511616) + 1) = 9
Edit:
Here's the answer to your first question..
var n = 601511616;
var nArray = new int[3];
for (int i = 0, numMod = n; i < 3; numMod /= 1000, i++)
nArray[i] = numMod%1000;
Please keep in mind there's no safety in this operation.
Edit#3
Still not perfect, but a better example.
var n = 601511616;
var nLength = (int)Math.Floor(Math.Log10(n) + 1)/ 3;
var nArray = new int[nLength];
for (int i = 0, numMod = n; i < nLength; numMod /= 1000, i++)
nArray[i] = numMod%1000;
Edit#3:
IDEOne example http://ideone.com/SSz3Ni
the output is exactly as the edit approved by the poster suggested.
{ 616, 511, 601 }
Using Log10 to calculate the number of digits is easy, but it involves floating-point operations which is very slow and sometimes incorrect due to rounding errors. You can use this way without calculating the value size first. It doesn't care if the number of digits is a multiple of 3 or not.
int value = 601511616;
List<int> list = new List<int>();
while (value > 0) // main part to split the number
{
int t = value % 1000;
value /= 1000;
list.Add(t);
}
// Convert back to an array only if it's necessary, otherwise use List<T> directly
int[] splitted = list.ToArray();
This will store the splitted numbers in reverse order, i.e. 601511616 will become {616, 511, 601}. If you want the numbers in original order, simply iterate the array backwards. Alternatively use Array.Reverse or a Stack
Since you already know they are in multiples of 3, you can just use the extracting each digit method but use 1000 instead of 10. Here is the example
a = 601511616
b = []
while(a):
b.append(a%1000)
a = a//1000
print(b)
#[616, 511, 601]

Split Double Value from textbox into multiple array

can someone show me how i can split value of double like this:-
654782.690
Can i split the value into this =
int i[0] = 65
int i[1] = 4
int i[2] = 7
int i[3] = 8
double i[4] = 2.690
Appreciate for the answer. Thank you in advance.
EDITED***
I have the UTM coordinate value in my xml. I want to make the chart and the value for the coordinate is fall into the grid. Lets say i have the zone area of x1 (578462.254), y1 (648523.124) & x2 (578481.452), y2 (648474.156). In my xml, i have a datapoint which fall under this coordinate. What i would like to do is to make the chart grid with the first two digit is become the zone area min and max(x,y).
I hope my clarification can explain what i want to do with those data. Thank you very much.
I got the answer here:-
decimal dValue = 654782.690;
string sValue = dValue.ToString();
decimal[] dArray = new decimal[5];
dArray[0] = Convert.ToDecimal(sValue.Substring(0, 2));
dArray[1] = Convert.ToDecimal(sValue.Substring(2, 1));
dArray[2] = Convert.ToDecimal(sValue.Substring(3, 1));
dArray[3] = Convert.ToDecimal(sValue.Substring(4, 1));
dArray[4] = Convert.ToDecimal(sValue.Substring(5));
Its worked now!!! Thank you for your support to solve my problem. :)
Use decimal instead of double if you are not storing scientific value.
decimal dValue = 654782.690M;
string sValue = dValue.ToString();
deciaml[] dArray = new decimal[5];
dArray[0] = Convert.ToDecimal(sValue.Substring(0,2));
dArray[1] = Convert.ToDecimal(sValue.Substring(2,1));
dArray[2] = Convert.ToDecimal(sValue.Substring(3,1));
dArray[3] = Convert.ToDecimal(sValue.Substring(4,1));
dArray[4] = Convert.ToDecimal(sValue.Substring(5));
To use the value of array index of 0 to 3 use Convert.ToInt32() to convert in Int datatype.
int iValue = Convert.ToInt32(dArray[0]);
Above example is not compiled it may give error. Take this example as a logic.

Calculating the percentage of uniqueness

I've figured out how to compare 2 text boxes and count how many words are the same in each one and how many are different but I was wondering instead of showing exactly how many are the same and how many are different could I calculate the percentage of difference between them?
For those who I confused, if i wrote this entire message again under this post and changed a few words and maybe removed some I could get feedback from the program telling me the difference is 14% or so...
You can get words by using string.Split:
String[] words1 = Textbox1.Text.Split();
String[] words2 = Textbox2.Text.Split();
You can use Linq to compare both:
var sameWordsInBoth = words1.Intersect(words2);
int count1 = sameWordsInBoth.Count();
var inTxt1ButNotInTxt2 = words1.Except(words2);
int count2 = inTxt1ButNotInTxt2.Count();
var inTxt2ButNotInTxt1 = words2.Except(words1);
int count3 = inTxt2ButNotInTxt1.Count();
To get the percentage you just have to count the total words(f.e. words1.Length) and compare that to the other counts.
For example:
double sameWordPercentage = (1.0 * count1 / words1.Length) * 100;
Since you said you already have the amount of unique words, and dont need explanation on how to get them:
double UniqueWordCount = 71;
double TotalWordCount = 231;
double Percentile = (UniqueWordCount/TotalWordCount)*100;
Answer : 30.7359.... %
If you want to round it
Math.Round(Percentile,2)
Answer : 30.74 %

Categories