I'm working on a homework assignment, for which I must do an insertion sort on an array of terms (term[]), while keeping each element of a corresponding array (freq[]) associated with the term.
The function breaks somewhere in the string library I think. I was thinking my code attempts to write to an array location that wasn't valid?
I receive the following error message
Unhandled exception at 0x59bbc0ed (msvcr100d.dll) in HW3.exe: 0xC0000005: Access violation reading location 0x00000001.
void insertionSort(string term[], int freq[], int size){
int i = 0, j = 0;
string c = " "; /* temporary variable to store term*/
int x = 0; /* temporary variable to store freq*/
for(i=1;i<size;i++){
c = term[i]; /*store the examined element*/
x = freq[i];
j=i-1;
while ((c < term[j]) && (j >= 0)){ /*test if c is smaller than the element below it*/
term[j+1] = term[j];
freq[j+1] = freq[j];
j--;
}
term[j+1] = c;
freq[j+1] = x;
}
}
Are you sure the error is in that function, rather than elsewhere in the program? i may have missed something, but i just put together a 'quick and dirty' program to test the above code and it seemed to work without problem.
The entire length of code isn't very long, I'll post it here.
When I tested each other function, the code ran fine. Out of the three functions "read", "swap", and "insertionSort", "insertionSort" is the only one that results in this error.
Most likely the error occurs in function read(). How many words do you have in the file "input.txt", is it more than MAX?
I'd suggest this, which checks that no more than MAX items are read. It also avoids looping on the condition eof() which is an error-prone approach and generally to be avoided:
1 2 3 4 5 6 7 8 9 10 11 12 13
void read(string term[])
{
string s = " ";
int j = 0;
ifstream fin("input.txt");
while (j < MAX && fin >> s )
{
term[j] = s;
j++;
}
fin.close();
}
I'm still getting the same error however. I tried increasing the size of the const int MAX, as well as double checking my input.txt file. Neither of those has stopped the error.
For the sake of information, my txt file is this:
one
two
three
four
five
And the details present when the code stops are as follows:
Thanks for the reply. It's getting a bit late where I live, so i have to leave this for now. Looks like you have Visual C++, I'm using a different compiler, but if there's an error it should still show up and so far I've not seen it.
I think I found it. Possibly my compilers (two different ones) optimised the code and didn't hit the error.
Line 36 is tricky:
while ((c < term[j]) && (j >= 0)){
The while loop has two conditions, c < term[j] and j >= 0
What happens when j reaches -1 ? The body of the loop should not be executed. But neither should the element term[-1] be accessed. As I understand things, if the first condition is false there is no need for the program to evaluate the second, as the whole condition must be false. I'd suggest re-writing it like this:
while ((j >= 0) && (c < term[j])){
That should mean the program doesn't attempt to access the array with a negative subscript.
Hey that worked perfectly! That makes sense too, I always thought of that condition as one statement, so I thought it would never attempt to access term[-1]. Thanks for your help!