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
How do I make the last result equal the first, second, third, and fourth result combined?
If all results are 10 points except the last one, then the last one should be 40 points. The last result is the total result.
int[] result = new int[5] { 0, 0, 0, 0, 0 };
Here you go
int[] result = { 1, 2, 3, 4, 0 };
result[result.Length - 1] = result.Take(result.Length - 1).Sum(); // 1 + 2 + 3 + 4 = 10
//result = { 1, 2, 3, 4, 10 };
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 4 months ago.
Improve this question
I am new here and in C# and I dont't know how to figure out that example..Printing numbers 1 to 100 that way "100, 1, 2, 99, 98, 3, 4, 97,....52, 49, 50, 51". I have a course work and should write it befoure 25.10..Thank you all!
for (int i = 1; i < 51; i+=2)
{
Console.WriteLine(101-i);
Console.WriteLine(i);
Console.WriteLine(1+i);
Console.WriteLine(100-i);
}
EDIT: just i want to add that i use (1+i) // (100-i) because if I use i-- or i++ inside the writeline, it will execute the ++ or -- after the print message
By using a lower and a upper variable and a while-loop, you can do it stright forward without any calculations:
int lower = 1, upper = 100;
while (lower < upper) {
Console.WriteLine(upper--);
Console.WriteLine(lower++);
Console.WriteLine(lower++);
Console.WriteLine(upper--);
}
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 9 months ago.
Improve this question
of course I can use for() {}
but is there any function which can add all values in array?
if do you know, please tell me.
thank you.
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[a.Length];
a.CopyTo(b, 0);
b.ToList().ForEach(c=>{
System.Console.WriteLine(c);
});
using System.Linq;
then...
var array = new double[] { 1.1, 2.2, 3.3 };
var sum = array.Sum(value=>value);
Console.WriteLine($"Sum is {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 6 years ago.
Improve this question
I'm making a program using C# that calculates measures of variation and measures of center in statistics. So the user inputs a list of numbers and it adds the numbers to a list. I want the program to be able to find the mode (If there is one) of the list and then return it. I also want it to return the modes separated by commas if there is multiple modes.
Wikipedia Mode Link
The following should work, it really is quite trivial.
(It could be optimized probably ¯\_(ツ)_/¯ )
var items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 2, 6, 2, 8, 8 };
var grouped = items.GroupBy(i => i).Select(i => new { value = i.Key, count = i.Count() });
var maxCount = grouped.Max(g => g.count);
var mode = grouped.Where(g => g.count == maxCount);
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 7 years ago.
Improve this question
I am new to LinQ and I want to get all the data from a range of numbers when I pass a parameter. For an example, the range is 1-100 and I pass in 40, it should get all the data from 1-100 but not only 40. Any ideas?
Thanks
You can use Enumerable.Range which will give you range of numbers. You can use Except method to remove the numbers that you dont want.
private static int[] GetRange(int start,int inclusiveEnd,int[] except)
{
return Enumerable.Range(start, inclusiveEnd - start + 1).Except(except).ToArray();
}
Then call it in this way.
var x = GetRange(1, 100, new[] {40});
Use filtering (i.e. Where statement):
var result = Enumerable.Range(1,100).Where(x=>x != 40).ToList()
List<int> list = new List<int>();
var result = list.Where(i=> i >= 1 && i <= 100 && i != 40);
Assuming, that there is a function, that transforms a number from given range into some data:
Enumerable.Range(1, 100).Where(_ => _ != 40).Select(_ => GetData(_));
Use Except extension method From MSDN
List<int> oldList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int parameter = 2;
List<int> newList = oldList.Except(new int[] { parameter }).ToList<int>();
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 the following code:
var result = AllSequences(1, 10, 3);
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end - size - start + 2)
from seq in AllSequences(i, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
Result:
1,1,1
1,1,2
1,1,3
1,1,4
....
2,2,2
2,2,3
2,2,4
But before coming to this sequence, wish you were like this:
2,1,1
2,1,2
2,1,3
2,1,4
.....
I'm having trouble generating sequence is, I'm using LINQ to gain performance in this loop
I'm a bit confused by your question but if you want the sequence to run through each digit from 1 to 10, for all permutations try something like this.
I'm not advocating the use of LINQ here, just trying to make as few changes to your code as possible.
public static IEnumerable<List<int>> AllSequences(int start, int end, int size)
{
if (size == 0)
return Enumerable.Repeat<List<int>>(new List<int>(), 1);
return from i in Enumerable.Range(start, end)
from seq in AllSequences(start, end, size - 1)
select new List<int> { i }.Concat(seq).ToList();
}
Results will be
1, 1, 1
1, 1, 2
... etc
1, 1, 10
1, 2, 1
1, 2, 2
... etc
1, 10, 8
1, 10, 9
1, 10, 10
2, 1, 1
2, 1, 2
.... etc
10, 10, 10