C++顺序搜索未找到最后一个元素
所以我有一个输入文件。它由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有2个解决方案:
长度应为 20 作为值
或
使用“<=”代替“<” (在这种情况下,“length”应命名为“lastIndex”)
There are 2 solutions :
length should get 20 as value
or
use "<=" instead of "<" (in this case "length" should be named "lastIndex")
看看你的搜索函数,当你试图找到第 20 个数字时,索引的值是多少?
Look at your search function, what value will index have when you try to find the 20th number?
如果你有 20 个数字,那么为什么将长度设置为 19?这是非常违反直觉的。
If you've got 20 numbers, then why do you set length to 19? That's very counter-intuitive.
经典的离一问题。有关代码更正,请参阅@Kipotlov 的答案。
Classic Off-By-One issue. See @Kipotlov's answer for code corrections.
使用 C 进行索引顺序搜索
此代码适用于所有情况,即如果我们要查找数组中的最后一个元素
这段代码会起作用...
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...