I need to split an array of indeterminate size, at the midpoint, into two separate arrays.
The array is generated from a list of strings using ToArray().
public void AddToList () { bool loop = true; string a = ""; Console.WriteLine("Enter a string value and press enter to add it to the list"); while (loop == true) { a = Console.ReadLine(); if (a != "") { mylist.Add(a); } else { loop = false; } } } public void ReturnList() { string x = ""; foreach (string number in mylist) { x = x + number + " "; } Console.WriteLine(x); Console.ReadLine(); } } class SplitList { public string[] sTop; public string[] sBottom; public void Split(ref UList list) { string[] s = list.mylist.ToArray(); //split the array into top and bottom halfs } } static void Main(string[] args) { UList list = new UList(); SplitList split = new SplitList(); list.AddToList(); list.ReturnList(); split.Split(ref list); } }
}
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
You could use the following method to split an array into 2 separate arrays
public void Split<T>(T[] array, int index, out T[] first, out T[] second) { first = array.Take(index).ToArray(); second = array.Skip(index).ToArray(); } public void SplitMidPoint<T>(T[] array, out T[] first, out T[] second) { Split(array, array.Length / 2, out first, out second); }
Use a generic split method:
public static void Split<T>(T[] source, int index, out T[] first, out T[] last) { int len2 = source.Length - index; first = new T[index]; last = new T[len2]; Array.Copy(source, 0, first, 0, index); Array.Copy(source, index, last, 0, len2); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With