【问题标题】:2D Dynamic Array C++ Display Problem2D 动态数组 C++ 显示问题
【发布时间】:2011-04-13 01:04:55
【问题描述】:

我读过关于 2d 动态数组的文章,但显然我还没有完全理解它,因为这个程序不起作用。该程序似乎在于显示数组。 输入文件是一个文本文件,第一行有 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();
}

【问题讨论】:

  • 你能用调试器找出段错误的确切行吗?

标签: c++ dynamic graph multidimensional-array vertices


【解决方案1】:
//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];
} 

【讨论】:

  • 好收获!我错过了那个!
  • 修复了很大一部分。非常感谢你。搞砸了课堂上的实现!健康长寿·繁荣昌盛! :)
【解决方案2】:

我在初始化数据数组的代码中发现了两个重大问题。首先,像这样的循环

    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];
} 

其他地方可能还有其他错误,但至少我已经让你开始了。

【讨论】:

  • 实际上这是一个VxV 邻接矩阵,他做得对。他唯一使用E 的地方是从文件中读取边缘。但是,该部分确实有一个错误。
【解决方案3】:

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

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

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

j == E 时会发生什么?您正在尝试读取文件中最后一行之后的一行。您应该改为检查j &lt; 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 &lt; Vc &lt; V 而不是&lt;=。当bc == V 你肯定是在矩阵之外阅读。

【讨论】:

  • 非常感谢您解决了一些问题。还要感谢您指出 j-for-loop 部分中的缺陷。长寿和繁荣! :)
猜你喜欢
  • 2011-01-18
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多