I've read about 2d dynamic arrays but I obviously haven't quite got my head around it as this program doesn't work. The program seems to lie in displaying the array. The input file is a text file with V and E on the first line with a 'tab indent' between them. The input vertices are on the next lines again tab indented with a new set on each line. On DevCpp it says there is a segmentation fault. Any help would be very much appreciated. thanks.
#include <iostream>
#include <fstream>
using namespace std;
#define maxV 100
#define unseen 0
typedef int Vertex;
class Graph {
private:
int V, E;
int**adj;
public:
Graph(char filename[]);
void display();
};
// constructor ask you for file name
Graph::Graph(char fname[]) {
Vertex u,v;
int j;
ifstream f;
f.open(fname, ios::in);
if(!f) {
cout << "\nError: Cannot open file\n";
return;
}
//Input number of vertices and edges
f >> V >> E;
int** adj = new int*[V];
for (int i=0;i<=V;i++)
{
adj[i]= new int[V];
}
for(int x=0;x<=V; ++x) // initially 0 array
{
for (int y=0;y<=V;++y)
adj[x][y] = 0;
}
// Set diagonal to 1
for(int z=0; z<=V; ++z)
adj[z][z]=1;
for (j =0;j<=E;++j)
{
f>>u>>v;
adj[u][v] = 1;
adj[v][u] = 1;
}
}
// This method displays the adjacency lists representation.
void Graph::display(){
int a,b,c;
for (a=0;a<=V;++a)
{
cout << a << " ";
}
cout << endl;
for (b=0;b<=V;++b)
{
开发者_Python百科 cout << b << "| ";
for (c=0;c<=V;++c)
{
cout<<adj[b][c]<<"| ";
}
cout<<endl;
}
}
int main()
{
char fname[20];
cout << "\nInput name of file with graph definition: ";
cin >> fname;
Graph g(fname);
g.display();
}
//Input number of vertices and edges
f >> V >> E;
// You're hiding your member variable in the following line, leading to an incorrect initialization
// int** adj = new int*[V];
adj = new int*[V];
for (int i=0;i<=V;i++)
{
adj[i]= new int[V];
}
I see two significant problems just in the code that initializes the data array. First, a loop like this
for (int i=0;i<=V;i++)
loops over one more element than actually exists in the array. The correct form of a loop if the array is V elements long is
for (int i=0;i<V;i++)
That's "less than" rather than "less than or equal".
Secondly, you allocate both the array of pointers to be V pointers long, and than the individual columns to be V elements long as well; but later you use the same array and expect it to be V x E in size. Altogether, then, I think the allocation code ought to be
int** adj = new int*[V];
for (int i=0;i<V;i++)
{
adj[i]= new int[E];
}
There are likely to be other errors elsewhere, but at least I've got you started.
I don't know which line is causing the segmentation fault but here are some things to look at:
for (j =0;j<=E;++j)
{
f>>u>>v;
adj[u][v] = 1;
adj[v][u] = 1;
}
Are u
and v
guaranteed to be less than V
? If not you could be writing outside the bounds of the matrix.
What happens when j == E
? You are trying to read a line past the last line in the file. You should be checking instead for j < E
. A better way still would be to ignore E
all together and just do this:
while(f >> u >> v)
{
adj[u][v] = 1;
adj[v][u] = 1;
}
More likely though the segmentation fault is here:
for (b=0;b<=V;++b)
{
cout<<(b+1)<<"| ";
for (c=0;c<=V;++c)
{
cout<<adj[b][c]<<"| ";
}
cout<<endl;
}
the for loop conditionals should be checking b < V
and c < V
not <=
. when either b
or c == V
you are definitely reading outside the matrix.
精彩评论