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

发布于 2024-10-07 10:57:20 字数 2282 浏览 1 评论 0原文

所以我有一个输入文件。它由 40 个数字组成。前 20 个数字被输入到一个数组中(我已经检查过了,它们确实在那里)。然后我关闭并重新打开输入文件。我使用顺序搜索将输入文件中的前 20 个数字与我的数组进行比较。这意味着他们都应该成功。然后,我将接下来的 20 个数字与数组中的数字进行比较,它们应该都是不成功的搜索。我的数组此时尚未排序。

我遇到的问题是,使用顺序永远找不到成功的最后一个数字。我不知道如何解决这个问题。

这是顺序搜索函数:

length = 19;

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

这是我成功/不成功的循环

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

但是,我的输出是:

 ************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(found)”语句,一切都会正常工作,但是如何在不删除它的情况下解决这个问题?

提前致谢

---------------编辑----------------

好的,当我将长度更改为 20 时,它似乎仍然不起作用。我很失落。

这是我创建数组的地方

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

的 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++;
   }
 }

,这是我在构造函数中将索引初始化为 0

这就是我声明数组

    ItemType list[length]; 

的方式!非常感谢大家!我真的非常感激。

So I have an input file. It consists of 40 numbers. The first 20 numbers are input into an array (I've checked this, they're actually there). 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. 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. 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?

Thanks in advance

---------------edit---------------

Okay, when I changed length to 20 it still didn't seem to work. 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

 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

This is how I declare the array

    ItemType list[length]; 

IT WORKS!!!! Thank you all SO much! I really appreciate it so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

红衣飘飘貌似仙 2024-10-14 10:57:20

有2个解决方案:
长度应为 20 作为值

length = 20;


使用“<=”代替“<” (在这种情况下,“length”应命名为“lastIndex”)

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

There are 2 solutions :
length should get 20 as value

length = 20;

or
use "<=" instead of "<" (in this case "length" should be named "lastIndex")

void Search::sequential(ItemType item, bool& found) 
{  
  int index = 0; 
  while (index <= length && item != list[index]) 
    index++; 
  found = (index <= length);  
} 
ヤ经典坏疍 2024-10-14 10:57:20

看看你的搜索函数,当你试图找到第 20 个数字时,索引的值是多少?

Look at your search function, what value will index have when you try to find the 20th number?

柠北森屋 2024-10-14 10:57:20

如果你有 20 个数字,那么为什么将长度设置为 19?这是非常违反直觉的。

If you've got 20 numbers, then why do you set length to 19? That's very counter-intuitive.

紙鸢 2024-10-14 10:57:20

经典的离一问题。有关代码更正,请参阅@Kipotlov 的答案。

Classic Off-By-One issue. See @Kipotlov's answer for code corrections.

和我恋爱吧 2024-10-14 10:57:20

使用 C 进行索引顺序搜索

此代码适用于所有情况,即如果我们要查找数组中的最后一个元素
这段代码会起作用...

#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");
}

INDEX SEQUENTIAL SEARCH USING C

this code works for all the cases i.e 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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文