I have a simple C++ code, it is giving garbage values when executed in my friend's Windows 10 machine with Mingw g++. But it gives a correct output when executed in my Ubuntu(g++) and various other online compilers.
g++ --version
g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This is the code that I am executing - https://pastebin.com/zn0aEQe1.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int v;
cout << "\nEnter the number of vertices: ";
cin >> v;
int adj[v][v];
long dist[v][v];
int next[v][v];
for (auto i = 0; i < v; i++)
for (auto j = 0; j < v; j++)
cin >> adj[i][j];
for (auto i = 0; i < v; i++) {
for (auto j = 0; j < v; j++) {
if (i == j) {
dist[i][j] = 0;
next[i][j] = i;
}
else if (adj[i][j]) {
dist[i][j] = adj[i][j];
next[i][j] = j;
}
else {
dist[i][j] = INT_MAX;
next[i][j] = -1;
}
}
}
for (auto k = 0; k < v; k++)
for (auto i = 0; i < v; i++)
for (auto j = 0; j < v; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
next[i][j] = next[i][k];
}
}
cout << "\nThe Shortest Distances are: \n";
for (auto i = 0; i < v; i++) {
for (auto j = 0; j < v; j++) {
cout << dist[i][j] << " ";
}
cout << "\n";
}
cout << "\nThe Shortest Paths are: \n";
// for printing path
for (int i = 0; i < v; i++) {
for (int j = i + 1; j < v; j++) {
int u = i, v = j;
cout << u;
while (u != v) {
u = next[u][v];
cout << "->" << u;
}
cout << "\n";
}
}
}
I have attached the outputs from ubuntu g++(left) , Mingw g++ (black screen - right).
Is something wrong with the code I wrote or is there some issue with mingw?
EDIT:
The same code works in mingw g++ when I use long long for dist
instead of long.