Store split values from user response in C# [closed] - c#

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 6 years ago.
Improve this question
I'm trying to create a grade calculator but I'm completely unsure how to compile the code to do so.
So far I've got the ability to split a user's response, but now I need to know how to take those splits and use them as separate values in order to create an average. I'm completely clueless how to achieve this and I've been searching for 2 days now on the internet with no luck.
Console.WriteLine ("User response seperated by commas goes here.");
string response = Console.ReadLine ();
Char delimiter = ',';
string[] splitResponses = response.Split (delimiter);

I need to know how to take those splits and use them as separate
values in order to create an average.
Not sure what you mean by take those splits and use them as separate
values, result is an array you could elements using index like splitResponseses[0]
To calculate the average you need to convert them to ints (or respective types), and calculate average.
string[] splitResponses = response.Split (delimiter); // Split string
int sum=0;
foreach(string s in splitResponses)
{
var valueInt = int.Parse(s); // convert string to an int.
sum+= valueInt;
}
double average = (double)sum/splitResponses.Length;
Another simple solution using Linq extensions.
int[] splitResponses = response.Split (delimiter) // Split string
.Select(int.Parse) // Convert To int value;
.ToArray();
Now you could calculate average using
splitResponses.Average(); // Average
splitResponses.Sum(); // Sum

Related

make dictionary that will loop every 9 numbers. C# [closed]

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 10 months ago.
Improve this question
Im new to programming and sorry if I cant explain properly. I am trying to iterate through a list that has items in it in multiples of 9. So the list can have 9,18,27.. items.
I have the code working for it to read when there is 9 using this dictionary. But I would like it work for any amount in multiples of 9.
var alphabets = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"}
};
So for example if there was 18 items it would like this dictionary to have this function.
var alphabets2 = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"},
{10,"A3"},{11,"B3"},{12,"C3"},{13"D3"},
{14,"E3"},{15,"F3"}, {16,"G3"},{17,"H3"},
{18,"I3"}
};
Thank you
As #DiplomacyNotWar commented, it sounds as if you need to input int value to convert to a correlating string value which is uniformly based on multiples of 9. If this is the case, I agree with #DiplomacyNotWar that you don't need to store anything but create a function to output the needed string value based on an int value. Here is a function that will output the pattern in your examples.
// value should be 0
string ConvertIntToSpecificString(int value)
{
// this will give you an int value 0-8
var modValue = (value - 1) % 9;
// The unicode for 'A' is 65
var firstCharValue = (char)(65 + modValue);
// This will return a whole number giving the iteration count. FE: 19 / 9 = 2
// Adding 2 to fit the pattern stated in the examples.
var secondValue = ( value / 9 ) + 2 ;
return $"{firstCharValue}{secondValue}";
}

How can I split a mixed string in c# [closed]

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 3 years ago.
Improve this question
Here is sample input and output.
I have strings like as following.
I want to increment string last digit by 1
AVAP001P001 output AVAP001P002
CD009 output CD010
Here's a quick solution that you can work with. You might want to make it more robust, but I went ahead and added applicable comments to describe what is being done.
static void Main(string[] args)
{
var s = "CBC004DS009";
// get the very last index of the character that is not a number
var lastNonNumeric = s.LastOrDefault(c => !char.IsDigit(c));
if (lastNonNumeric != '\x0000')
{
var numericStart = s.LastIndexOf(lastNonNumeric);
// grab the number chunk from the string based on the last character found
var numericValueString = s.Substring(numericStart + 1, s.Length - numericStart - 1);
// convert that number so we can increment accordingly
if (int.TryParse(numericValueString, out var newValue))
{
newValue += 1;
// create the new string without the number chunk at the end
var newString = s.Substring(0, s.Length - numericValueString.Length);
// append the newly increment number to the end of the string, and pad
// accordingly based on the original number scheme
newString += newValue.ToString().PadLeft(numericValueString.Length, '0');
Console.WriteLine(newString);
}
}
}

what is the most efficient data structure for matching specific numbers to ranges of percents? [closed]

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 5 years ago.
Improve this question
In my program, I want to be able to match certain numbers to ranges of percentages, like: 0 would be the match of 0%, 1 would be the match of less then 10%, 2 would be the match of 10%-20%... and so forth. What is the most efficient data structure/method to do it ?
I would like to perform it in c#.
A Dictionary for this purpose could be a decent solution. The keys of the Dictionary would be the numbers and the values could be Tuples with the corresponding min and max percentages. If you want to learn the range for a number you could retrieve it's range in O(1).
You could define it as:
var numbersPercentageRanges = new Dictionary<int, Tuple<double, double>>
{
{ 0, Tuple.Create(0,0) },
{ 1, Tuple.Create(0.1,0.2)}
};
and you could retrieve the corresponding range as:
if(numbersPercentageRanges.TryGetValue(1, out var range))
{
var min = range.Item1;
var max = range.Item2;
}

Understanding this C# code, I mean only the code, not the theory of Entropy [closed]

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 6 years ago.
Improve this question
I finally found a code possibly working Shannon Entropy calculation, but as I don't fully understand C# at all, could someone help me fully grasp it please? I mean purely understand the code, NOT what it is doing. I do understand Delphi, if you ask.
public static double ShannonEntropy(string s)
{
var map = new Dictionary<char, int>();
foreach (char c in s)
{
if (!map.ContainsKey(c))
map.Add(c, 1);
else
map[c] += 1;
}
double result = 0.0;
int len = s.Length;
foreach (var item in map)
{
var frequency = (double)item.Value / len;
result -= frequency * (Math.Log(frequency) / Math.Log(2));
}
return result;
}
You have a function called ShannonEntropy which takes a string s and returns something of type double (basically a floating point number).
You instantiate a map (dictionary) of characters -> integers and call it map.
Loop over every character in the string s. During that loop check to see if the character is in our map (map). If it is not, map that character to 1. If it is already in the map, increment the value. At the end we'll have a map of each character to a count of how often it appears in s.
Declare a double name result and set it 0.
Declare an integer called len which is the length of the input string.
Loop over every character in our map. During the loop we're going to compute the frequency of how often it appears.
We declare a variable called frequency and set it to the count (the value we stored in the map) divided by the length of the string - so the frequency gets set to the percentage of each character that appears in s every time we loop.
Now we take the frequency (the percentage the given character appeared) and multiply it by the log (base 2) of the frequency, take the product and subtract it from result (which was initially 0).
Once we've finished looping we return the result.
Var is initializing variable map , new dictionary has added a dictionary to the var . dictionary gives the string or a char value an integer Value, let's say that red = 1 , blue = 2 , green = 3 etc . I think foreach runs the loop for as long as there there are char in string .
You must know if( ) it compiles the code only if the statement inside is true or false as in this case.math.log is a method . then at end we return result . I am not very familiar with c# I am more familiar with c and Java .

sum digits from a very long number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assume I let user input number : 1298743257884834...(long as user need)
Let program tell how many digit are they ?
then give result by SUM them = ?
By using LINQ
string input = // get input from console, textbox, ...
int inputLength = input.Length; // get number of digits, although you don't need it here
int sum = input.Sum(c => int.Parse(c.ToString())); // summarize
Hint: use the BigInteger structure found in System.Numerics.
First of all you can know the number of digits in this way:
string digits = TextBox1.Text;
long digitsnumber = digits.Length;
Then to sum each number, you need to loop in your string like an array of char, in this way, and cast the car value to an integer with the GetNumericValue method of System.Char:
int sum = 0;
foreach (var c in digits)
{
if (Char.IsNumber(c))
{
sum += (int)Char.GetNumericValue(c);
}
}
Sum will be your result.

Categories

Resources