简体   繁体   English

C ++查找线性搜索中最后一次出现的int

[英]C++ Finding the last occurrence of an int in a linear search

This week for homework I've been tasked with loading in a text file of 1,000 numbers and to do a linear search of a number entered in by a user. 本周的家庭作业我的任务是加载1000个数字的文本文件,并对用户输入的数字进行线性搜索。 I have the linear search part done, but I have to find and print the last occurrence of that integer. 我已完成线性搜索部分,但我必须找到并打印该整数的最后一次出现。 I figured it would be easiest to run the array from the end and print the last occurrence and break the loop. 我认为从最后运行数组并打印最后一次出现并打破循环是最容易的。 I've started the code, but am having some trouble at finding the last occurrence. 我已经启动了代码,但在查找最后一次出现时遇到了一些麻烦。

I know my second for loop to run the array backwards is wrong, I'm just not sure what about it is wrong. 我知道我的第二个for循环向后运行数组是错误的,我只是不确定它是错的。 Any help is appreciated! 任何帮助表示赞赏! Thank you! 谢谢!

#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

int main()
{
    ifstream input("A1.txt");
    int find;

    cout << "Enter a number to search for: ";
    cin >> find;

    if (input.is_open())
    {
        int linSearch[1000];

        for (int i = 0; i < 1000; i++)
        {
            input >> linSearch[i];

            for (i = 1000; i > 0; i--)
            {
                if (find == linSearch[i])
                {
                    cout << find << " is at position " << i << ". " << endl;
                }
            }
        }
    }

    _getch();
    return 0;
}
    for (int i = 0; i < 1000; i++)
    {
        input >> linSearch[i];

This is a good start. 这是一个好的开始。 You started a loop to read the 1000 numbers into your array. 您开始循环以将1000个数字读入阵列。

        for (i = 1000; i > 0; i--)

Don't you think this is a bit premature? 你不觉得这有点不成熟吗? You haven't yet finished the loop to read the 1000 numbers in the file, yet, and you're already searching the array, that hasn't been fully read yet. 你尚未完成循环读取文件中的1000个数字,但是,你已经在搜索尚未完全读取的数组。 There's a very technical term for this logical mistake: "putting the cart before the horse". 这个逻辑错误有一个非常技术性的术语:“把车放在马前”。 You need to finish the loop to read the 1000 numbers, first. 首先,您需要完成循环以读取1000个数字。 And only then you can execute this second loop. 只有这样你才能执行第二个循环。

        {
            if (find == linSearch[i])

Ok, now let's back up a bit. 好的,现在让我们稍微回顾一下。 You started the loop with i=1000 . 你用i=1000开始循环。 Now, right here, what is the very first value if i ? 现在,就在这里,如果i是第一个价值是什么? It is 1000. Don't you see a problem here? 这是1000.你不觉得这里有问题吗? The 1000 element array, "linSearch", as you know, contains values numbered 0 through 999. That's a 1000 elements total. 如你所知,1000元素数组“linSearch”包含编号为0到999的值。总共1000个元素。 With i starting off with a value of 1000, accessing the non-existent linSearch[1000] is undefined behavior, and a bug. i以值1000开始,访问不存在的linSearch [1000]是未定义的行为,并且是一个错误。

You could tweak the logic here, to get it right. 你可以在这里调整逻辑,以使其正确。 But it's not even necessary to do that. 但它甚至没有必要这样做。 You already have a perfectly working loop that reads the 1000 numbers from the file. 您已经有一个完美的循环,可以从文件中读取1000个数字。 And you know which number you want to search. 并且您知道要搜索的号码。

So, each time you read the next number from the file, if it's the number you're looking for, you just store its position. 因此,每次从文件中读取下一个数字时,如果它是您要查找的数字,您只需存储其位置即可。 So, when all is said and done, the last position that's stored in that variable will be the position of the last occurrence of the number you're searching for. 因此,当完成所有操作后,存储在该变量中的最后一个位置将是您要搜索的数字的最后一个位置。 Simple logic. 简单的逻辑。 All you have to do is also set a flag indicating that the number you were searching for has been found. 您所要做的就是设置一个标志,表示您找到了您要搜索的号码。

And once you come to the decision to do that, you will find that it's no longer even needed to have any kind of an array in the first place. 一旦你决定这样做,你会发现它不再需要首先拥有任何类型的阵列。 All you have to do is read the 1000 numbers from the file, one number at a time, check if each number is the one you're searching for, and if so, save its position. 您所要做的就是从文件中读取1000个数字,一次读取一个数字,检查每个数字是否是您要搜索的数字,如果是,请保存其位置。 Then, at the end of the loop, compare notes. 然后,在循环结束时,比较笔记。

Since it's homework, I should probably be at least a little vague, and I definitely shouldn't use code. 既然是家庭作业,我应该至少有点模糊,我绝对不应该使用代码。

You should not be nesting the 2nd loop within the first loop. 您不应该在第一个循环中嵌套第二个循环。 It should be at the same indentation level as, and after the closing bracket for, the first loop. 它应该与第一个循环的闭合括号处于相同的缩进级别。

Also, you shouldn't be searching back to 0 in almost all cases, but instead back to where you found the element in your linear search, or where you find it, and no further. 此外,您几乎不应该在所有情况下都回溯到0,而是返回到您在线性搜索中找到元素的位置,或者找到它的位置,而不是进一步。

And yes, pay attention to what Beta wrote. 是的,请注意Beta编写的内容。

Also, shouldn't you break out of the loop each time you find what you're looking for? 另外,每当你找到你想要的东西时,你不应该突破循环吗?

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

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