-3

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.

hridayns
  • 697
  • 8
  • 16
  • 1
    Have you tried to use `x` instead of `size` in `countInv` call? Your array is full of garbage and the algorithms are written not very well to handle arbitrary values (like infinite loop in `mergeCountInv`). – Valeri Atamaniouk Jul 09 '15 at 13:43
  • Use `while (fin >> i)`, not `while (!fin.eof())`. See [here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Jul 09 '15 at 14:33
  • @ValeriAtamaniouk: Thanks. I'm using x as the size now. – hridayns Jul 10 '15 at 10:40
  • @molbdnilo: I changed that too. It works great! – hridayns Jul 10 '15 at 10:41

1 Answers1

0

The recursion never terminates if right is greater than 1, as your base case for the recursion is right == 1 while you have one recursive call that passes right unmodified.

That is, countInv(x,y,l,r) calls countInv(x,y,amidpoint,r), which calls countInv(x,y,anothermidpoint,r), and so on, until you're stuck in countInv(x,y,r-1,r) forever.

Exactly how to fix it depends on what the code is supposed to do, but you most likely want a base case that involves left as well as right.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thanks! Atleast it stopped giving me that error now. Now I'm just getting the wrong output. I'm getting 3 instead of 13. I'm just starting off with algorithms, so I'm not very proficient. – hridayns Jul 10 '15 at 03:20