2016-04-24 11 views
0

2つの別々のアレイから読み込み、LSTestのアレイの値がLSStandardの配列に含まれているかどうかを確認することが主な目的です。アレイからアレイへのリニアサーチ

LSTest [j] == LSTest [i]の場合、結果は と表示されます。

LSTest [j]が関数内でLSStandard [i]を検出する回数が複数あることがあります。例えば、LSTTest [j]は9であり、LSStandard [1]とLSStandard [10] ]

LSTestは50個の整数です。 LSStandardは100の整数です。あなたが適切linearSearch関数を宣言しているが、がそれを定義していないため、コンパイルされません通常の状況で

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

}

+2

あなたの質問は? –

+0

FYIでは、一般的なコーディングガイドラインでは、マクロと定数(少なくともマクロ)のすべてのCAPS名を予約しています。変数名と関数名は、先頭のアンダースコア(コンパイラによって予約されている可能性があります)を除いて、他の組み合わせでなければなりません。 –

答えて

0

これは私がこれまで持っているコードです。また、へのポインタを使用すると、LSTArray[]ではなく、呼び出すことができます。

はとしてlinearSearchを定義します。

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

としてそれを呼び出す:あなたはまた、LSS<something>[Length]にバッファオーバーフローの多くを持つことになります

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

。配列のサイズを超えています。

+0

エラーが発生しました: 関数 "std :: basic_ifstream <_Elem、_Traits> :: basic_ifstream(const std :: basic_ifstream <_Elem、_Traits> :: _ Myt&)[_Elem = char、_Traits = std: :char_traits ] "(" c:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ VC \ include \ fstream "の行825で宣言されている)は参照できません - 削除された関数\t ConsoleApplication78 –

+0

参照できないifstream関数 –

+0

Ifstreamは 'char'を' int'ではなく 'char'を読み込みます。 'char c'でそれを保存し、' int'として配列にキャストしてみてください。それでも、オーバーフローには常に注意してください。 –