2017-04-22 43 views
1

作爲我課程中編碼挑戰的一部分,我們必須生成代碼以提供10個不同的任務。線性搜索目標

在這個任務中,我的目標是制定一個線性搜索算法,用於搜索數組中的特定項目,並在找到時顯示其位置。

這是我當前的代碼:

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. 
      } 
     } 
    } 
} 

我需要與線31的幫助,與 foundpositions.Add(I)的問題;

我不知道該行爲數組正確添加值,這似乎是導致問題的原因。 (在這一行中,我試圖將搜索的當前位置添加到將在稍後顯示的數組中)

感謝您的幫助。此外,如果還有其他明顯的,明顯的錯誤,指出他們將不勝感激。

+0

你能解釋爲什麼你要檢查目標是否被發現?這似乎沒有必要。 – JohnG

回答

0

我需要幫助的問題是行31,與 foundpositions.Add(i);

數組是動態的,他們沒有一個add()方法。您可以改用List

替換此:

var foundpositions = new int[] { }; 

與此:

var foundpositions = new List<int>(); 

也,你似乎並沒有這樣做任何與此聲明的變量:

var targetpos = -1; 

從而控制意志從來沒有去這個if塊:

if (targetpos != -1){ //If the target number was found 
     Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome. 
} 

在這個任務中,我的目標是使 搜索數組中的特定項目,並顯示如果發現其 位置(S)的線性搜索算法。

作爲您的代碼目前看起來,似乎有幾個錯誤。但是,下面的例子可以幫助您開始:

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 
} 

然後簡單地調用LinearSearchmain方法傳遞所需的數據中,你是好去。

不要忘記將從LinearSearch返回的值賦值給一個變量,或者直接將其打印到控制檯。

0

我不知道你爲什麼要檢查目標號碼是否被發現。如果你想獲得與目標int相等的所有int的數組中的索引,那麼你可以簡單地遍歷數組,並將匹配放入一個ints列表中,然後返回這個列表。

這個返回列表的計數將告訴你有多少匹配的目標和列表將包含這些匹配的索引。似乎沒有任何理由檢查目標是否在循環訪問數組時發現。如果返回的列表爲空,則目標未找到。下面的代碼使用了這種方法。我希望我不會錯過任何東西。

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<int> 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<int> GetPositions(int target, int[] intArray) { 
    List<int> foundTargets = new List<int>(); 
    for (int i = 0; i < intArray.Length; i++) { 
    if (intArray[i] == target) { 
     foundTargets.Add(i); 
    } 
    } 
    return foundTargets; 
}