二维动态数组 C++显示问题

发布于 2024-11-01 06:15:37 字数 1705 浏览 4 评论 0原文

我读过有关二维动态数组的内容,但显然我还没有完全理解它,因为这个程序不起作用。该程序似乎在于显示数组。 输入文件是一个文本文件,第一行有 V 和 E,它们之间有“制表符缩进”。输入顶点再次位于下一行,每行缩进一个新的集合。在 DevCpp 上它说存在分段错误。任何帮助将非常感激。谢谢。

#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)
   {
      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();
}

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)
   {
      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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯淡〆 2024-11-08 06:15:37
//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];
} 
//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];
} 
浅笑轻吟梦一曲 2024-11-08 06:15:37

我在初始化数据数组的代码中看到两个重要问题。首先,像这样的循环

    for (int i=0;i<=V;i++)

会循环遍历比数组中实际存在的元素多的元素。如果数组的长度是 V 个元素,则循环的正确形式是

for (int i=0;i<V;i++)

“小于”而不是“小于或等于”。

其次,将指针数组分配为 V 指针长,并将各个列分配为 V 元素长;但稍后您使用相同的数组并期望其大小为 V x E。总而言之,我认为分配代码应该是

int** adj = new int*[V];
for (int i=0;i<V;i++)
{
   adj[i]= new int[E];
} 

其他地方可能存在其他错误,但至少我已经让您开始了。

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.

风向决定发型 2024-11-08 06:15:37

我不知道哪一行导致分段错误,但这里有一些需要注意的事项:

for (j =0;j<=E;++j)
{
    f>>u>>v;
    adj[u][v] = 1;
    adj[v][u] = 1;
}

uv 是否保证小于 V?如果不是,你可能会写在矩阵的边界之外。

j == E时会发生什么?您正在尝试读取文件中最后一行之后的一行。您应该检查 j < E。更好的方法仍然是一起忽略 E 并执行以下操作:

while(f >> u >> v)
{
    adj[u][v] = 1;
    adj[v][u] = 1;
}

尽管分段错误更有可能在这里:

for (b=0;b<=V;++b)
{
    cout<<(b+1)<<"| ";
    for (c=0;c<=V;++c)
    {
        cout<<adj[b][c]<<"| ";
    }
    cout<<endl;
}

for 循环条件应该检查​​ b b b b b b b b Vc < V 不是 <=。当 bc == V 时,你肯定是在矩阵之外读取。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文