Home > Net >  C Program Returning Differing Results in IDEs and differing results when running the same data
C Program Returning Differing Results in IDEs and differing results when running the same data

Time:04-21

I am trying to solve the following problem:

Farmer John has acquired a set of N (2 <= N <= 2,000) touchy cows who are conveniently numbered 1..N. They really hate being too close to other cows. A lot.

FJ has recorded the integer X_i,Y_i coordinates of every cow i (1 <= X_i <= 100,000; 1 <= Y_i <= 100,000).

Among all those cows, exactly two of them are closest together. FJ would like to spread them out a bit. Determine which two are closest together and print their cow id numbers (i) in numerical order.

                10 | . . . . . . . 3 . . . . .
                 9 | . 1 . . 2 . . . . . . . .
                 8 | . . . . . . . . . . . . .
                 7 | . . . . . . . . . . 4 . .
                 6 | . . . . . . 9 . . . . . .
                 5 | . 8 . . . . . . . . . . .
                 4 | . . . . . 7 . . . . . . .
                 3 | . . . . . . . . . 5 . . .
                 2 | . . . . . . . . . . . . .
                 1 | . . . . 6 . . . . . . . .
                 0 ---------------------------
                                       1 1 1 1
                   0 1 2 3 4 5 6 7 8 9 0 1 2 3

Quick visual inspection shows that cows 7 and 9 are closest together (the distance separating them is sqrt(11 22) = sqrt(5), so the output would be '7 9' on a single line (without quotes, of course).

Below is my coded attempt to solve the problem:

using namespace std;

int main() {
   int n; cin >> n;
   long long xcord[n],ycord[n];

   for (int i=0; i<n; i  ) {
       cin >> xcord[i];
       cin >> ycord[i];
   }

   long long shortest = 9999999999999;
   int a=-1, b=-1;
   for (int i=0; i<n; i  ) {
       for (int j=i 1; j<=n; j  ) {
           long long curDist = (xcord[i]-xcord[j])*(xcord[i]-xcord[j])   (ycord[i]-ycord[j])*(ycord[i]-ycord[j]);
           // cout << curDist << " ";
           if (curDist < shortest) {
               shortest = curDist;
               a=i 1, b=j 1;
           }
       }
   }
   cout << a << " " << b << endl;

}

Here is the test case I am running: 25 24804 7918 98983 95075 10819 48641 84481 33476 56724 20854 83193 17014 72997 5394 69263 33045 26810 75288 85442 47243 81678 82129 84199 35206 68212 77035 62113 87896 49538 1375 145 90953 58175 62546 73175 5853 7789 37961 18883 49418 78257 90342 2048 64282 49057 95081 89406 47329 9778 68104

When I run the test case using my code, I get the correct answer, 7 18. I am running the code using VSCode. When I try running the code using replit, I get a completely different answer despite the test case being the same and when I try running the same test case over and over in replit, the program returns differing answers. Do you know why I am getting this error? Thanks a lot!

CodePudding user response:

Right here you go out of bounds on your array

 for (int j = i   1; j <= n; j  ) 
 -----------------------^

that should be

for (int j = i   1; j < n; j  ) 

now gives 7 18

  • Related