using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Linearsearch2 { class Program { static void Main(string[] args) { var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99 }; //Sets up the array var targetvalue = 77; //Establishes what number the search will attempt to find. var targetpos = -1; //Establishes the position in the array of the target. var targetnumber = 0; //Establishes the counter for the number of times the target appears. bool found = false; //Decides wether to change the number or use a counter method. var foundpositions = new int[] { }; //Establishes an array which will hold the positions of located items for (var i = 1; i < array.Length; i++) { if (found == true && array[i] == targetvalue) { targetnumber = targetnumber + 1; } if (found == false && array[i] == targetvalue) //If the target value has not been found yet { foundpositions.Add(i); //This is the line i need help with. I dont know how to add a value to an array properly. found = true; } } if (targetpos != -1){ //If the target number was found Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome. } else //If the target number was not found { Console.WriteLine("The number " + targetvalue + " did not appear in this array."); // Prints the final outcome. } } } } var foundpositions = new int[] { }; var foundpositions = new List(); var targetpos = -1; if (targetpos != -1){ //If the target number was found Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome. } public static int LinearSearch(int[] items, int target) { if (items == null) throw new ArgumentNullException("argument items has null reference"); if (items.Length == 0) return -1; // return -1 if the item is not found for (int i = 0; i < items.Length; i++) if (items[i] == target) return i; return -1; // return -1 if the item is not found } private static void FindTargets() { var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99, 1, 31, 10, 9, 420, -5, 77, 420, 300, 99 }; //Sets up the array int target = 77; List foundTargets = GetPositions(target, array); StringBuilder sb = new StringBuilder(); sb.Append("There are " + foundTargets.Count + " int(s) that match " + target + Environment.NewLine); sb.Append("The array indexs for the target ints " + target + " are: "); int count = 0; foreach (int curInt in foundTargets) { sb.Append(curInt); if (count < foundTargets.Count - 1) sb.Append(", "); count++; } Console.WriteLine(sb.ToString()); } private static List GetPositions(int target, int[] intArray) { List foundTargets = new List(); for (int i = 0; i < intArray.Length; i++) { if (intArray[i] == target) { foundTargets.Add(i); } } return foundTargets; }