Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重复值数组的线性搜索_C#_Algorithm_Search_Array Algorithms - Fatal编程技术网

C# 重复值数组的线性搜索

C# 重复值数组的线性搜索,c#,algorithm,search,array-algorithms,C#,Algorithm,Search,Array Algorithms,好的,我有一个算法,它对一个双精度数组进行线性搜索。当它找到元素时,它会给我数组中的位置。问题是,我不知道如何调整算法,使其考虑重复元素。我想它仍然显示的元素的位置,但我努力做到这一点。。。我也不确定步进计数器是否真的工作。如果有人能帮忙就太好了谢谢 int i = 0; double item = 0; int pos = 0; int steps = 0; Console.Write("Enter item to search in array : "); item = double.Pa

好的,我有一个算法,它对一个双精度数组进行线性搜索。当它找到元素时,它会给我数组中的位置。问题是,我不知道如何调整算法,使其考虑重复元素。我想它仍然显示的元素的位置,但我努力做到这一点。。。我也不确定步进计数器是否真的工作。如果有人能帮忙就太好了谢谢

int i = 0;
double item = 0;
int pos = 0;
int steps = 0;

Console.Write("Enter item to search in array : ");
item = double.Parse(Console.ReadLine());

//Loop to search element in array
for (i = 0; i < LowArr.Length; i++)
steps++

{
    if (item == LowArr[i])
    {
        pos = i + 1;
        break;
    }
}

if (pos == 0)
{
    Console.WriteLine("Item Not found in array");
    Console.WriteLine("Steps taken in Search: " + steps);
}
else
{
    Console.WriteLine("Position of item in array: " + pos);
    Console.WriteLine("Steps taken in Search: " + steps);
inti=0;
双项=0;
int pos=0;
int步数=0;
编写(“在数组中输入要搜索的项:”);
item=double.Parse(Console.ReadLine());
//循环以搜索数组中的元素
对于(i=0;i

如果在数组中多次找到该值,我希望它能告诉我位置。

如果找到该值,则只需打印位置:

bool found = false;
for (i = 0; i < LowArr.Length; i++) {
  if (item == LowArr[i]) {
    Console.WriteLine("Position of item in array: " + i);
    found = true;
  }
}
if (!found) {
  Console.WriteLine("Item Not found in array");
}
boolfound=false;
对于(i=0;i

我还添加了一个布尔标志,以便在未找到该项时添加消息。如果我们只查找第一个匹配项,则在找到该项时,您可能已从循环中断,并检查是否
I==LowArr.length
,以测试是否未找到该项。

如果找到该值,则只需打印位置:

bool found = false;
for (i = 0; i < LowArr.Length; i++) {
  if (item == LowArr[i]) {
    Console.WriteLine("Position of item in array: " + i);
    found = true;
  }
}
if (!found) {
  Console.WriteLine("Item Not found in array");
}
boolfound=false;
对于(i=0;i
我还添加了一个布尔标志,以便在未找到该项时可以添加一条消息。如果我们只查找第一个出现的项,那么在找到该项时,您可能已经从循环中断,并检查是否
I==LowArr.length
,以测试是否未找到该项