Finding the values that Match In 2 Arrays

I'm doing a insertion sort to find the same values but placed in two different arrays with different element types. I also want to output this into an output file. Calculating how many keywords.txt values match the unsorted_dictionary.txt values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  /**

Name: William Lee
Compiler: GNU GCC Compiler
Assignment #2

*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

void insertionSort(string[], string[], int, int);
void showArray(string[], string[], int, int);
void binarySearch(const int[], int, int);
void output(ofstream, string[], int);

int main(){

    ifstream infile1, infile2;
    ofstream outfile;
    infile1.open("keywords.txt");
    infile2.open("unsorted_dictionary.txt");
    outfile.open("output.txt");
    const int SIZE1 = 84;
    const int SIZE2 = 16000;
    string unsorted_dictionary[SIZE1], keywords[SIZE2];
    int index1 = 0, index2 = 0;

if (infile1 && infile2){
    while(!infile1.eof() && !infile2.eof()){
        infile1 >> keywords[index1];
        infile2 >> unsorted_dictionary[index2];
        index1++;
        index2++;
        insertionSort(keywords, unsorted_dictionary, index1, index2);
        output(outfile, unsorted_dictionary, index2);
        outfile.close();
        return 0;
    }
    cout << "Values Assigned In The Arrays!";
}
else
    cout << false;
    return 0;
}

void insertionSort(string arr1[], string arr2[], int ind1, int ind2){
    {
    int i, j, k, l;
    string tmp1, tmp2;
      for (i = 1; i < ind1; i++) {
            j = i;
            while (j > 0 && arr1[j - 1] > arr1[j]) {
                  tmp1 = arr1[j];
                  arr1[j] = arr1[j - 1];
                  arr1[j - 1] = tmp1;
                  j--;
            }
      }
      for (k = 1; i < ind2; k++) {
            l = k;
            while (l > 0 && arr1[l - 1] > arr1[l]) {
                  tmp2 = arr1[l];
                  arr1[l] = arr1[l - 1];
                  arr1[l - 1] = tmp2;
                  l--;
            }
      }
    }

}

void output(ofstream outputting, string array[], int length = 0){
    outputting << "Keyword not found: " << array[length] << endl;
    length++;
    outputting << "Number of Keywords not Found == 20";
}

void binarySearch(const int array[], int numElems, int value){
   int first = 0,
   last = numElems - 1,
    middle,
    position = -1;
    bool found = false;

    while (!found && first <= last){
        middle = (first + last) / 2;
        if(array[middle] == value){
            found = true;
            position = middle;
        }
        else if (array[middle] > value)
            last = middle - 1;
        else
            first = middle +1;
    }
}



Keyword.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
for
if
nullptr
break
int
long
sizeof
return
short
else
friend
const
static_cast
switch
not
auto
extern
and
double
case
enum
false
and_eq
float
explicit
decltype
bitand
continue
dynamic_cast
goto
mutable
new
bitor
inline
bool
catch
namespace
char
compl
asm
const_cast
not_eq
operator
class
or_eq
private
export
public
typedef
protected
typeid
register
static
reinterpret_cast
template
this
struct
throw
true
try
default
delete
do
typename
union
unsigned
using
virtual
void
volatile
wchar_t
while
xor
xor_eq
alignas
alignof
char16_t
char32_t
constexpr
signed
noexcept
static_assert
thread_local
or


I could provide the unsorted_dictionary.txt file, but it has over 16,000 values.
The code doesn't compile because an ofstream cannot be passed by value. It must be passed by reference.

Thus lines 19 and 77 become as follows. Note the ampersand & character:
1
2
void output(ofstream &, string[], int);
void output(ofstream & outputting, string array[], int length = 0){


The rest of the code, well in main() I think you try to do too much in one big chunk in the middle. Break it down into a series of individual steps.

1. open the dictionary file.
2. check it is open. if not output a message and exit.
3. read the dictionary file into the array.
4. open the keyword file.
5. check it is open. if not output a message and exit.
6. read the keyword file into the array.
7. sort the dictionary array.
7a . It isn't necessary to sort the keywords - you can if you want (optional).
8. loop through the keyword array.
    search the dictionary array for the word.
    if it is found, take one action.
    else take some other action.


(I need to re-read the details for what should be done with missing or found keywords).

Function binarySearch() should not be declared as void. It needs to let the calling program know whether or not the item was found. It will do that by returning an integer which is the position of the item if found, or -1 if not found.


While I'm here, it's worth stating: don't use eof() as the condition in a while loop. Instead put the file input operation (example: infile1 >> keywords[index1] ) inside the loop condition instead. See this thread for an example:
http://www.cplusplus.com/forum/beginner/222607/#msg1020980

Topic archived. No new replies allowed.