Minimal Number in array, C# [duplicate] - c#

This question already has answers here:
c# get the min value from array
(3 answers)
Closed 2 years ago.
I'm getting from the user 20 Numbers and I need to print back the array AND ALSO lowest, minimal number and its position in the array. I am wondering if my code is correct. Can you tell any typos?
P.S., it's my first week of coding, so I am kind of a "Noob", which means that my code will be pretty bad.
Links to similar problems will be appreciated as well! :)
Console.WriteLine("Hello! Enter your very important Numbers!");
int[] Numb = new int[20];
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Enter Numbers here!");
Numb[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine(Numb);
Console.WriteLine(Math.Min(Numb));
Printscreen with my error(s):

To find out Minimum number from an array you need to use .Min() function from System.Linq
Returns the minimum value in a sequence of values.
using System.Linq;
...
Console.WriteLine(Numb.Min()); //Instead of Math.Min(Numb);
.NET Fiddle

Related

C# dictionary size is a prime number [duplicate]

This question already has answers here:
Why .Net dictionaries resize to prime numbers?
(3 answers)
.NET ConcurrentDictionary initial capacity set to arbitrary prime number rather than expected capacity in MSDN example documentation. Why?
(1 answer)
Closed 4 months ago.
I was reading the source code of the dictionary and came across the resize method. I saw the formula temp = (currentSize * 2), currentSize = GetNextPrimalNumber(temp). Why we must to set primal number as a dictionary size? https://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,440
Can somebody explain why we should use primal numbers as a size of dictionary? Thanks a lot !

Find String Array in other String Array [duplicate]

This question already has answers here:
Finding a subsequence in longer sequence
(8 answers)
Closed 1 year ago.
I want find a string array in other string array. How I do that?
Example:
string[] a = {"a","b","c","d","e","f","g","c","d","e"}
string[] b = {"d","e"}
How find b in a, I need get the index of all instances.
I would suggest you search a bit of the documentation to understand more of the language but anyway I'll try to explain what you can do
You can do a for loop going through the a array and inside you do a for loop going through the b array, if a[index] = b[index2] you break out of the a loop and put the index in a new array that you initialized before
If you need more help than that say but I recommend going through more documentation and a few videos and learn a bit more the basics

How do i determinate if a sentence has digits/numbers, and then assign those numbers to variables? [duplicate]

This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 7 years ago.
I already tried to determinate the digits in a sentence using 'isDigit', but this gives me a 'bool' output. I need an 'int' output.
What i want to do is, say, i have the sentence "cheese23"; "2" and "3" will be put in their own variable, so i can add/subtract/multiply/ etc them.
(x=2,y=3;)
help will be hugely appreciated (self-teaching beginner here)
Do:
int[] intArray = "Cheese23".Where(Char.IsDigit).Select(c => int.Parse(c.ToString())).ToArray();
This extracts the numbers in the string in the order and creates an array out of it.
Then you can do intArray[0] to get 2 and intArray[1] to get 3.
Search up LINQ to see how those chain of methods did it.

Force two decimal places in C# [duplicate]

This question already has answers here:
Leave only two decimal places after the dot
(13 answers)
Closed 8 years ago.
I've this problem and can't find a solution.
This is super easy and i don't know why can't i find a solution.
Problem:
if a value returns for example "16.60", in c# i'll read "16.6", but i need 0 as well, because of paypal API, wich only accepts a value with no decimal numbers, or if it has to have decimal numbers the minimum and maximum must be 2.
So how can i make this?
i've tried this:
string value_f = "16,6";
decimal value_f_d = decimal.Parse(value_f);
value_f_d = (decimal)Math.Round(value_f_d, 2);
value_f = value_f_d.ToString("#.##");
value_f = value_f.Replace(',', '.');
i want this output: 16.60, but gives this: 16.6
string output = value_f_d.ToString("#.00", CultureInfo.InvariantCulture);
(using System.Globalization in your using declarations at the top)

How can I generate random dates within a range? [duplicate]

This question already has answers here:
Random DateTime between range - not unified output
(5 answers)
Closed 6 years ago.
I am automating an application where records can be opened between specified date ranges. I know that I can create an array that randomly picks a number in my array, such as:
DateTime.Now.AddDays(90).ToString("MM/dd/yyyy")
However, I would like to know if there is a better way to go about this. Please help! Thanks.
void Main()
{
Console.WriteLine(DateTime.UtcNow.AddDays(new Random().Next(90)));
}
This will add a random amount of days to the start date, essentially ending up with a random date.
Assuming that you want the dates to be in the range of 90 days of the date of generation. Then, you can try this:
int seed = (int)DateTime.Now.Ticks;
int days = seed % 90;
DateTime.Now.AddDays(days).ToString("MM/dd/yyyy");
You can, of course, refactor this. I was verbose for the sake of clarity. You can also change the integer value of 90 to any integer you want to be the upper-bound of your range.
Hope this helps.

Categories