0

So I'm pretty new to C++ but i think im gettting the hang of it a bit.

As part of an excersize, I have to take an input text file and apply this in a "shortest distance algorithm" where ultimatly I want to output all the shortest distances and routes but i haven't gotten that far yet. I have used the Floyd Warshall algorithm. For now my question is, how do i replace a self written int array by a text input. the input array is just numbers but actually represents distances between nodes. The test array that im using now only has 3 nodes, but i want to be able to expand it to a much larger node amout, say 100.

example test matrix:

0 1234567 100

1234567 0 400

100 400 0

Should be read as:

           node1      node2       node3
  node 1    0          999999     100
  node 2   999999       0         400
  node 3    100        400         0 

The large numbers: 999999 represents a distance that is too large too count as a edge.

As of now my code looks something like this:

#include<stdio.h>

// Number of vertices
#define V 3

// Define 999999 as a distance that is too large to represent a edge connection
#define TooLarge 999999

// The print function
void printSolution(int dist[][V]);

        // Distance algorithm
    void Distance (int distgraph[][V])
    {
        // output matrix that will have the shortest distance for every vertice
        int dist[V][V], i, j, k;

        // initial values for shortest distance are based on shortest paths.
        for (i = 0; i < V; i++)
            for (j = 0; j < V; j++)
                dist[i][j] = distgraph[i][j];

        // Add all vertices to the set of intermediate vertices.
             for (k = 0; k < V; k++)
        {
            // use all vertices as seperate source
            for (i = 0; i < V; i++)
        {
            // use all vertices as destination for the earlier determined source

            for (j = 0; j < V; j++)
            {
                // If vertex k is on the shortest path from i to j, then update the value of dist[i][j]
                if (dist[i][k] + dist[k][j] < dist[i][j])
                    dist[i][j] = dist[i][k] + dist[k][j];
            }
        }
    }

    // Print the shortest distance matrix
    printSolution(dist);
}

// The print function
void printSolution(int dist[][V])
{
    printf ("Shortest distance matrix \n");
    for (int i = 0; i < V; i++)
    {
        for (int j = 0; j < V; j++)
        {
            if (dist[i][j] == 999999)
                printf("%7s", "TooLarge");
            else
                printf ("%7d", dist[i][j]);
        }
        printf("\n");
    }
}

// driver program to test above function
int main()
{
      int distgraph[V][V] = { {0, 1234567, 100},
                              {1234567, 0, 400},
                              {100, 400, 0,},
                            };

    // Print the solution
    Distance(distgraph);
    return 0;
}

Hopefully someone can help me, I have the feeling im just forgetting something stupid. I have tried to inport the textfile using this type of code:

    using namespace std;

double distances [3][3];

int main () {
  int x, y;
  ifstream in("citytest.txt");

  if (!in) {
    cout << "Cannot open file.\n";
    return 0;
  }

  for (y = 0; y < 3; y++) {
    for (x = 0; x < 3; x++) {
      in >> distances[x][y];
    }
  }
    cout << distances[3][3] << " " << endl;
      in.close();

Which i know works, but only inports a predetermind part of the matrix whereas i want to input the entire array. (the cout function is just there to test if the correct distances were given as an input)

5
  • You want to know how to read numbers from a file? Commented Nov 7, 2015 at 17:56
  • I want to know how to input numbers from a file as a 2D array, and get it to work for my program Commented Nov 7, 2015 at 18:01
  • Did you read the link? Commented Nov 7, 2015 at 18:04
  • No, i have now though. that should work,. but isnt very ifficient if i have to assign 100 variables right? Commented Nov 7, 2015 at 18:22
  • there are more answers, also some that explain how to store the valus in an array/vector Commented Nov 7, 2015 at 18:38

1 Answer 1

1

You cannot efficiently allocate the container unless you know big the workload in your external data file is.

Thus:

  • tokenize the first line of your file and take the dimension N from that
  • allocate your container accordingly
  • then consume the rest of the file and put the data into the container; maybe throw if a row's length doesn't match N, or if there are not N rows.

You may consider that

  • representing a graph by a full adjacency matrix is a debatable concept; it's space-inefficient and time-inefficient for sparse graphs
  • a 2D c-array is not the only possible representation of a matrix; you may consider a flat std container and implement a slice-style access on it
  • last not least you may want to have a look at boost::graph
Sign up to request clarification or add additional context in comments.

1 Comment

alright thanks, i'll take a look at that. If i still have questions ill let you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.