作为我班级编码挑战的一部分,我们必须生成代码以提供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(ⅰ);
我不知道正确地向数组添加值的行,这似乎是导致问题的原因。 (在这一行中,我试图将搜索的当前位置添加到稍后将显示的数组中)
感谢您的帮助。此外,如果有任何其他明显的,明显的错误,指出他们将不胜感激。
答案 0 :(得分:0)
我需要帮助的问题是使用第31行 foundpositions.Add(ⅰ);
数组不是动态的,并且它们没有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.
}
在这项任务中,我的目标是制作一个线性搜索算法 搜索数组中的特定项目,并显示它 (如果找到的话。)
如您的代码目前所示,似乎有几个错误。但是,以下示例将帮助您入门:
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
}
然后只需调用LinearSearch
方法中的main
传递所需的数据,就可以了。
不要忘记将LinearSearch
的返回值分配给变量,或者只是将其打印到控制台。
答案 1 :(得分:0)
我不确定您为什么检查是否找到了目标号码。如果你想获得所有与目标int相等的整数的数组中的索引,那么你可以简单地遍历数组并将匹配放入一个int列表然后返回这个列表。
此返回列表的计数将告诉您与目标匹配的数量,列表将包含这些匹配的索引。似乎没有任何理由检查在循环数组时是否找到了目标。如果返回的列表为空,则找不到目标。下面的代码使用了这种方法。我希望我不会错过任何东西。
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;
}