简体   繁体   English

C ++顺序搜索未找到最后一个元素

[英]C++ Sequential search not finding the last element

So I have an input file. 所以我有一个输入文件。 It consists of 40 numbers. 它由40个数字组成。 The first 20 numbers are input into an array (I've checked this, they're actually there). 前20个数字被输入到数组中(我已经检查过了,实际上它们在那里)。 I then close and re-open the input file. 然后关闭并重新打开输入文件。 I compare the first 20 numbers in the input file against my array using sequential search. 我使用顺序搜索将输入文件中的前20个数字与数组进行比较。 This means they should all be successful. 这意味着他们都应该成功。 I then compare the next 20 numbers against the numbers in my array, they should all be unsuccessful searches. 然后,我将接下来的20个数字与数组中的数字进行比较,它们都应该是不成功的搜索。 My array is unsorted at this point. 我的数组此时未排序。

The problem I'm running into is that the last number for successful is never found using sequential. 我遇到的问题是,使用序号永远找不到成功的最后一个数字。 I'm not sure how to fix this. 我不确定如何解决此问题。

Here is the sequential search function: 这是顺序搜索功能:

length = 19;

void Search::sequential(ItemType item, bool& found)
{ 
  int place = 0;
  while (place < length && item != list[place])
    place++;
  found = (place < length); 
}

And here is my successful/unsuccessful loops 这是我成功/失败的循环

outFile << "\n\n ************Sequential Successful ********** \n";
outFile << endl << "ID" << endl;

inFile >> num;
for(int i=0; i<=length && inFile; i++)
{
  search.sequential(num, found);
  if (found)
    outFile << num << endl; 

  inFile >> num;
} 


//sequential unsuccessful
outFile << "\n\n ************Sequential unsuccessful ********** \n";
outFile << endl << "ID" << endl;

for(int i=0; i<=length && inFile; i++)
{
  search.sequential(num, found);
  if (!found)
    outFile << num << endl; 

  inFile >> num;
}

However, my output is: 但是,我的输出是:

 ************Sequential Successful ********** 

 ID
 1111
 3352
 4567
 5678
 6789
 7890
 8901
 9012
 1223
 2113
 8546
 2374
 4723
 9573
 3284
 7474
 8594
 3589
 5858
 //THERE SHOULD BE 1925 HERE BUT THERE ISN'T

  ************Sequential unsuccessful ********** 

 ID
 9456
 3584
 2222
 4319
 4477
 5710
 5497
 1502
 1599
 1504
 1506
 9943
 8833
 9944
 6678
 5555
 5660
 9911
 6130
 1613

If I remove the "if (found)" statement everything works perfectly, but how do I get around this without removing that? 如果我删除“ if(found)”语句,那么一切将正常运行,但是如何在不删除该语句的情况下解决这个问题?

Thanks in advance 提前致谢

---------------edit--------------- - - - - - - - -编辑 - - - - - - - -

Okay, when I changed length to 20 it still didn't seem to work. 好的,当我将长度更改为20时,它似乎仍然不起作用。 I'm so lost. 我迷路了

Here is where I create the array 这是我创建数组的地方

inFile >> num;
for (int i=0; i<length && inFile; i++)
{
  search.addToList(num);
  inFile >> num;
}

and here is the addToList function 这是addToList函数

 void Search::addToList(ItemType num)
 {
   if (index < length)  //ive tried taking out this statement just to see if it makes a difference and it didn't
   {
     list[index] = num;
     index++;
   }
 }

I initialize index to 0 in the constructor 我在构造函数中将索引初始化为0

This is how I declare the array 这就是我声明数组的方式

    ItemType list[length]; 

IT WORKS!!!! 有用!!!! Thank you all SO much! 非常感谢大家! I really appreciate it so much. 我真的很感激。

There are 2 solutions : length should get 20 as value 解决方案有2种:长度应为20

length = 20;

or use "<=" instead of "<" (in this case "length" should be named "lastIndex") 使用“ <=”代替“ <”(在这种情况下,“ length”应命名为“ lastIndex”)

void Search::sequential(ItemType item, bool& found) 
{  
  int index = 0; 
  while (index <= length && item != list[index]) 
    index++; 
  found = (index <= length);  
} 

INDEX SEQUENTIAL SEARCH USING C 使用C进行索引顺序搜索

this code works for all the cases ie if we are finding last element in an array this code will work... 该代码适用于所有情况,即,如果我们在数组中找到最后一个元素,该代码将起作用...

#include<stdio.h>
void main()
{
  int d[100],kin[20],pin[20],temp,k,i,j=0,n,n1=0,start,end;
  printf("Enter the number of elements:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&d[i]);
  printf("Enter the number to be searched:");
  scanf("%d",&k);
  for(i=0;i<n;i+=3)
  {
    kin[n1]=d[i];
    pin[n1]=i;
    n1++;
  }
  if(k < kin[0])
  {
    printf("element not found");
    exit(0);
  }
  else
  {
    for(i=1;i<=n1;i++)
      if(k < kin[i] )
      {
        start=pin[i-1];
        end=pin[i];
        break;
      }
      else
      {
        start=n1;
        end=n-1;
      }
  }
  for(i=start;i<=end;i++)
  {
    if(k==d[i])
    {
      j=1;
      break;
    }
  }
  if(j==1)
    printf("element found at position %d",i);
  else
    printf("element not found");
}

看一下您的搜索功能,当您尝试找到第20个数字时,索引将具有什么值?

If you've got 20 numbers, then why do you set length to 19? 如果您有20个数字,那么为什么将长度设置为19? That's very counter-intuitive. 这是非常违反直觉的。

Classic Off-By-One issue. 经典的一次性发行。 See @Kipotlov's answer for code corrections. 有关代码更正,请参见@Kipotlov的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM