I've tried searching for an answer on StackOverflow and googled multiple times. I'm not really sure what exactly is wrong with the code. When I run it on Eclipse Luna, it just says "solution.exe has stopped working". Can anyone help me find out what's wrong here? Or give me a way to find out for myself?
This is my code:
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
long mergeCountInv(long list[],long tmp[],long left,long mid,long right);
long countInv(long list[],long tmp[],long left,long right);
long mergeCountInv(long list[],long tmp[],long left,long mid,long right) {
long i = 0,j = mid + 1,k = 0;
long count = 0;
while(i < mid && j < right) {
if(list[i] <= list[j]) {
tmp[k++] = list[i++];
}
else if(list[i] > list[j]) {
tmp[k++] = list[j++];
count++;
}
}
while(i < mid) {
tmp[k++] = list[i++];
}
while(j < right) {
tmp[k++] = list[j++];
}
return count;
}
long countInv(long list[],long tmp[],long left,long right) {
long k = 0,mid;
if(right == 1) return 0;
else {
mid = (left + right)/2;
k = countInv(list,tmp,left,mid) + countInv(list,tmp,mid + 1,right) + mergeCountInv(list,tmp,left,mid,right);
}
return k;
}
int main() {
long list[100001];
long i,x=0;
///////////////FILE HANDLING////////////////////////////////
ifstream fin;
fin.open("IntegerArray.txt");
while (!fin.eof()) {
fin >> i;
list[x] = i;
x++;
}
fin.close();
////////////////////////////////////////////////////////////
long size = sizeof(list)/sizeof(long) - 1;
long *tmp = new long[size];
cout<<countInv(list,tmp,0,size);
delete []tmp;
return 0;
}
These are the contents of "IntegerArray.txt":
6
3
5
4
2
1
There are actually going to be 100000 integers in the file, but I'm trying it out with a smaller test case first. I would appreciate your help! Thanks.