How to add List's all values? [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 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}");

Related

C# Console Application, how to make all results equals totalResult? [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
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 };

Find the mode in a list of doubles C#? [closed]

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);

How to write arrays to tab delimited text file? [closed]

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 7 years ago.
Improve this question
I have two arrays -
int[] array1 = { 1, 2};
int[] array2 = { 6, 7};
I want to write them in text file delimited by tab.
Final Result -
1 6
2 7
Please suggest how to do this?
Assuming both arrays are of the same length:
File.WriteAllLines(filename,
Enumerable.Range(0, arr1.Length)
.Select(i => string.Format("{0}\t{1}", arr1[i], arr2[i]));
You could do something like this:
public void writeFile(int[] array){
using (streamWriter sw = new StreamWriter(filename, true){
for(int i = 0; i < array.Length; i++){
sw.WriteLine(array[i] + "\t");
}
}
}
Then you would just call the write method like this:
writeFile(array1);
writeFile(array2);

How to call my int array class to ad my dropdownlist [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 8 years ago.
Improve this question
public static int[] ddDuration()
{
int[] ddarray = new int[] { 1, 2, 3, 4, 5 };
return ddarray;
}
How to make in c# to call my int array to who any page in my DropDownList
//and I am try to show in my DropDownList
//like that some thing
Duration dm = new Duration();
//dm.ddDuration;
//dropdduration.item.AddRang(dm.ddDuration);
If I got this right, you're trying to add an array of integers as items to a DropDownList.
var collection = ddDuration();
int len = collection.length;
for(int i=0; i<len; i++){
DropDownList1.Items.Insert(i, new ListItem(collection[i].toString(), ""));
}
Insert's first parameter is the position (index) in the Items collection and the second parameter takes a ListItem; of which constructor can be build upon its content (string in this case).

How to numerically sort an array of strings? [closed]

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 8 years ago.
Improve this question
I have an array of strings that has a number at the beginning, followed by a name. How can I sort the array numerically; ie
40 Mike, 25 Tyson, 9 Jackson, 5 Phillip, 3 Mitchell
as opposed to
9 Jackson, 5 Phillip, 40 Mike, 3 Mitchell, 25 Tyson
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] items = {"10", "1", "30", "-5"};
Array.Sort(items, (x, y) =>
{
int xNum = int.Parse(x);
int yNum = int.Parse(y);
return Comparer<int>.Default.Compare(xNum, yNum);
});
foreach(var item in items)
Console.WriteLine(item);
}
}
var sorted = yourArray.OrderBy(x => int.Parse(x.Split()[0])).ToArray();
You can use SortedDictionary data structure to obtain an array sorted.

Categories

Resources