2017-04-22 9 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. 
      } 
     } 
    } 
} 

私は foundpositions.Add(I)と、ライン31であるとの助けが必要な問題。

配列に値を正しく追加する行がわからないため、これが問題の原因と思われます。 (この行では、後で表示される配列に検索の現在位置を追加しようとしています)

ありがとうございました。また、他に明白な目障りなエラーがある場合は、それらを指摘していただければ幸いです。

+0

ターゲットが見つかったかどうかを確認する理由を説明できますか?これは不要です。 – JohnG

答えて

0

私が助けを必要とする問題は、行31で、 foundpositions.Add(i);であります。

配列はないダイナミックであり、彼らはadd()メソッドを持っていません。代わりにListを使用することができます。

この置き換えます。また、あなたはこの宣言された変数で何を行っているようには見えません

var foundpositions = new List<int>(); 

var targetpos = -1; 

ので、意志を制御し、これと

var foundpositions = new int[] { }; 

決してこの内部に入る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. 
} 

は、私の目標は、 配列内の特定の項目を検索し、見つかった場合は、その 位置(複数可)を表示する線形探索アルゴリズムを作ることです。

現在のところ、いくつかのエラーがあるようです。ただし、以下の例では、あなたが始めるのに役立つでしょう:

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 
} 

を、単に必要なデータを渡すmainメソッド内LinearSearchを呼び出して、あなたが行ってもいいです。

戻り値をLinearSearchから変数に代入するか、単にコンソールに出力することを忘れないでください。

0

わかりませんターゲット番号が見つかっているかどうかを確認する理由は何ですか。ターゲットintと等しいすべてのintの配列のインデックスを取得する場合は、単純に配列をループし、一致をintのListに格納してからこのリストを返すことができます。

この返されたリストの数は、ターゲットと一致する数を示し、リストには一致するインデックスが含まれます。配列をループしている間にターゲットが見つかったかどうかを確認する理由はないようです。返されるリストが空の場合、ターゲットは見つかりませんでした。以下のコードはこのアプローチを使用しています。私は何かを逃していないことを望む。

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; 
} 
関連する問題