Comma Operator with indexing 2D arrays

痞子三分冷 提交于 2019-11-28 11:58:20

问题


I have this algorithm that is pseudocode for the dijkstra algorithm for graph theory. The first thing that goes on is a basic for loop.

visitedSet[0] = true //visitedSet is a array of bools
for (int i = 1; i <= numberNodes; ++i)
{
    distanceArray[i] = adjacencyMatrix[0,i];
    //distanceArray is 1D with size of fifty
    //adjacencyMatrix is 2D with size of fifty
    //Both arrays hold values of unsigned ints
}

Here are the array definitions

enum GraphLimit300 {MAX_NODES = 50};
unsigned int adjacencyMatrix[MAX_NODES][MAX_NODES];
unsigned int distanceArray[MAX_NODES];

Visual studio is giving me an array saying that I can't assign an array of unsigned integers to a pointer. I have looked up online that with the comma operator basically in this case throws out the first case, 0, and treats it as distanceArray[i] = adjacencyMatrix[i]; Which doesn't make sense to me since adjacenyMatrix is a 2D array. I am just wondering what is giving me this compile error and get more information on why, because I basically just copying in variable names where the pseudo code basically says to.

Pseudo code:

    S = { 1 }
for ( index = 2; index <= N; ++ index )
    D[ index ] = C[ 1, index ]
for ( index = 1; index <= N – 1; ++ index )
    Choose a vertex v in V – S such that D[ v ] is a minimum
    Add v to S
    for each vertex w in V – S do
        D[ w ] = min( D[ w ], D[ v ] + C[ v, w ] )

The above pseudo code uses lists to represent their arrays they for some reason start at 1 so I modified it to start at 0 in my code.


回答1:


You have to review how to access elements of 2D array. Also, take look at what comma operator does. You have to use [] twice:

adjacencyMatrix[0][i]

The following:

adjacencyMatrix[0, i]

is equivalent to:

adjacencyMatrix[i]

Which will still leave you with 1D array. And, as the error message says:

   distanceArray[i] = adjacencyMatrix[i];
// ^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^^
//   unsigned int   array of unsigned ints

You can not possibly expect this assignment to happen.



来源:https://stackoverflow.com/questions/34098166/comma-operator-with-indexing-2d-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!