Output is different on a linux server C++ [closed]

  adjacency-matrix, c++, graph, linux

Hello everyone, a weird issue is occurring with me. I am making a
program that will read a file (Vertex file) that has one word per line
and will construct an adjacency matrix representation of a graph.
there are 5757 words in the file. Then in another file (Edge File) has
the edges between the words in the format word1,word2. Each word is
only 5 letters long. Then I use separate chaining for a hash function
that receives a word, and then we get the index. So the issue here is,
I am using CodeBlocks on my windows machine, and the following code
works properly:

    #include <iostream>
    #include <string>
    #include "WordNetwork.h"
    #include "Stack.h"
    #include "Queue.h"
    WordNetwork :: WordNetwork ( const string vertexFile , const string edgeFile )
    {
        matrix = NULL;
        hashTable = NULL;
    
        int lineIndex = 0;
    
        string curWord;
        string firstWord;
        string secondWord;
    
        ifstream fileVertex ( vertexFile.c_str() );
        ifstream fileEdge ( edgeFile.c_str() );
    
        hashTable = new LinkedList [5749]; // size of hash table 5749, closest prime to 5757
        matrix = new bool* [5757];
    
        for ( int i = 0; i < 5757; i++ )
        {
            matrix [i] = new bool [5757];
        }
    
        for ( int i = 0; i < 5757; i++ )
        {
            for ( int j = 0; j < 5757; j++ )
            {
                matrix [i][j] = false;
            }
        }
    
        while ( getline ( fileVertex, curWord ) )
        {
            wordArray [ lineIndex ] = curWord;
            lineIndex++;
        }
    
        for ( int i = 0; i < 5757; i++ )
        {
            curWord = wordArray [i];
            int curWordHash = hashFunction ( curWord );
            hashTable [ curWordHash ].addWord ( curWord, i );
        }
    
        while( getline( fileEdge, firstWord ) )
        {
            secondWord = firstWord.substr (6);
            firstWord = firstWord.substr (0, 5);
    
            int firstWordIndex = -1;
            int secondWordIndex = -1;
    
            int firstWordHash = hashFunction ( firstWord );
            int secondWordHash = hashFunction ( secondWord );
    
            for ( int i = 0; i < hashTable [firstWordHash].getSize (); i++ )
            {
                StringNode* curNode = hashTable[firstWordHash].getWord (i);
                string curNodeWord = curNode -> word;
                int curNodeIndex = curNode -> index;
    
                if ( curNodeWord == firstWord )
                {
                    firstWordIndex = curNodeIndex;
                }
            }
    
            for ( int i = 0; i < hashTable [secondWordHash].getSize (); i++ )
            {
                StringNode* curNode = hashTable[secondWordHash].getWord (i);
                string curNodeWord = curNode -> word;
                int curNodeIndex = curNode -> index;
    
                if ( curNodeWord == secondWord )
                {
                    secondWordIndex = curNodeIndex;
                }
            }
    
            cout << "First Index: " << firstWordIndex << ", Second Index: " << secondWordIndex << endl;
    
            matrix [firstWordIndex] [secondWordIndex] = true; // Because Graph is Undirected
            matrix [secondWordIndex] [firstWordIndex] = true; // The edges are mirrored
        }
    }

Here is the .h class:


    #ifndef WORDNETWORK_H_INCLUDED
    #define WORDNETWORK_H_INCLUDED
    
    #include <iostream>
    #include <fstream>
    #include "LinkedList.h"
    using namespace std;
    class WordNetwork
    {
    public:
        WordNetwork ( const string vertexFile , const string edgeFile );
        ~WordNetwork ();
        void listNeighbors ( const string word );
        void listNeighbors ( const string word , const int distance );
        void listConnectedComponents ();
        void findShortestPath ( const string word1 , const string word2 );
    
    
    private:
        bool isThereAdjacent ( int row );
        int hashFunction ( string aString );
        int findWordIndex ( const string aWord );
        bool isTherePath ( int source, int destination );
        bool** matrix;
        LinkedList* hashTable;
        string wordArray [5757];
    };
    
    
    #endif // WORDNETWORK_H_INCLUDED

And the output of the code on my windows machine is this: [Windows
Machine Code Output][1]

But when I run the same code on a Linux server, I get a different
output: [Linux Server Code Output][2]. The version of the Linux server
is this:

NAME="Oracle Linux Server" VERSION="7.9" ID="ol" ID_LIKE="fedora"
VARIANT="Server" VARIANT_ID="server" VERSION_ID="7.9"
PRETTY_NAME="Oracle Linux Server 7.9" ANSI_COLOR="0;31"

ORACLE_BUGZILLA_PRODUCT="Oracle Linux 7"
ORACLE_BUGZILLA_PRODUCT_VERSION=7.9 ORACLE_SUPPORT_PRODUCT="Oracle
Linux" ORACLE_SUPPORT_PRODUCT_VERSION=7.9

I compile my code using this makefile:

OBJS    = main.o WordNetwork.o LinkedList.o StringNode.o Stack.o Queue.o
SOURCE  = main.cpp WordNetwork.cpp LinkedList.cpp StringNode.cpp Stack.cpp Queue.cpp
HEADER  = WordNetwork.h LinkedList.h StringNode.h Stack.h Queue.h
OUT     = hw4
CC       = g++
FLAGS    = -g -c -Wall
LFLAGS   =

all: $(OBJS)
        $(CC) -g $(OBJS) -o $(OUT) $(LFLAGS)

main.o: main.cpp
        $(CC) $(FLAGS) main.cpp

WordNetwork.o: WordNetwork.cpp
        $(CC) $(FLAGS) WordNetwork.cpp

LinkedList.o: LinkedList.cpp
        $(CC) $(FLAGS) LinkedList.cpp

StringNode.o: StringNode.cpp
        $(CC) $(FLAGS) StringNode.cpp

Stack.o: Stack.cpp
        $(CC) $(FLAGS) Stack.cpp

Queue.o: Queue.cpp
        $(CC) $(FLAGS) Queue.cpp


clean:
        rm -f $(OBJS) $(OUT)

What could be the reason for this difference in outputs, I have been
scratching my head over it for a couple of days

[1]: https://i.stack.imgur.com/8ttyl.png [2]:
https://i.stack.imgur.com/QAuJ7.png

The 2 files can be found through this link: https://easyupload.io/m/bs1cbh

Source: Windows Questions C++

LEAVE A COMMENT