阵列到阵列线性搜索

时间:2016-04-24 16:49:44

标签: c++ arrays search linear

我有一个作业,其主要目标是从两个独立的数组中读取,并确定LSTest的数组是否具有LSStandard数组中的值。

所以基本上,如果LSTest [j] == LSTest [i], 显示结果。

请注意,LSTest [j]可以多次在函数中找到LSStandard [i],例如LSTTest [j]可以是9,它可以在LSStandard [1]和LSStandard [10]中找到9

LSTest是50个整数。 LSStandard是100个整数。

这是我到目前为止的代码:

// ConsoleApplication78.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
using namespace std;

int linearSearch(ifstream, int[], int, int[], int);

// Program Main
int main()
{

ifstream FIN_LSS ("LSStandard.txt");
ifstream FIN_LST ("LSTest.txt");

int const LSSlength = 100;
int const LSTlength = 50;
int getSearchNumber;
// int exme;
int LSSArray[LSSlength];
int LSTArray[LSTlength];

if (!FIN_LSS.fail())
{

    cout << "'LSStandard.txt' opened successfully." << endl << endl;

    if (!FIN_LST.fail())
    {

        cout << "'LSTest.txt' opened successfully." << endl << endl;

        while (FIN_LSS >> LSSArray[LSSlength])
        {

            {
                int result = linearSearch(FIN_LST, LSTArray[], LSTlength,
                    LSSArray[], LSSlength);
                if (result >= 0)
                {
                    cout << "The number " << LSSArray[result] << "was found at:  " << result << "  index  " << endl;
                }

                else
                {
                    // cout << "Nothing was found" << endl;
                }
            }
        }
    }
}

return 0;
}

int linearSearch(ifstream FIN_LST, int LSTArray[], int LSTlength,
int LSSArray[], int LSSlength)
{
int i; // counter variable
int j; // counter variable

for (j = 0; j < LSTlength; j++)
{
    while (FIN_LST >> LSTArray[LSTlength])
    {
        for (i = 0; i < LSSlength; i++)
        {

            if (LSTArray[j] == LSSArray[i]) {
                return i;
            }
        }
    }
}
return -1;

}

1 个答案:

答案 0 :(得分:0)

在正常情况下,由于您正确地声明linearSearch函数,但是定义它,因此无法编译。此外,要调用它,请使用指向LSTArray的指针,而不是LSTArray[]

linearSearch定义为:

int linearSearch(ifstream FIN_LST, int* LSTArray, int LSTlength,
                 int* LSSArray, int LSSlength);

并将其命名为:

int result = linearSearch(FIN_LST, LSTArray, LSTlength,
                          LSSArray, LSSlength);

LSS<something>[Length]还会有很多缓冲区溢出;它超过了数组大小。

相关问题