我试图在C中实现Dijkstra算法,而我试图将2D数组传递给函数。我尝试用C99和C11编译,所以我编写函数的方式应该(并且确实)编译。但是,当我进行调试时,我在所有数据中扫描并跳转到我的dijkstra()
功能后,在控制台中出现以下错误,此功能暂时无效。如果有人能告诉我发生了什么,我会很感激。
如果它完全相关,我使用的是GCC编译器,Eclipse Kepler和Fedora 20
warning: Range for type <error type> has invalid bounds 0..-11105
#include <stdio.h>
#define INFINITY 4294967296
void dijkstra(int root, int end, int size, int adjMatrix[size][size]);
int main(void)
{
/*------------------------------------------------------------------------*/
/* Variable Declaration
/*-----------------------------------------------------------------------*/
int i, j; /* Temporary counting variables */
int n; /* Number of verticies */
int distance; /* Temporary distance variable */
int root, end; /* Root and terminating verticies */
/*------------------------------------------------------------------------*/
/* Data Input
/*-----------------------------------------------------------------------*/
/*Get number of verticies and check for erroneous input */
printf("Please enter the number of verticies in your graph: ");
scanf("%d", &n);
while (n <= 0)
{
printf("\nERROR: value must not be less than or equal to zero! \n\n"
"Please enter the number of verticies in your graph: ");
scanf("%d", &n);
}
/*Get root node and check for erroneous input */
printf("Please enter the root vertex: ");
scanf("%d", &root);
while (root > n || root <= 0)
{
printf("\nERROR: value must be less than or equal to the total number"
" of\n verticies and may not be less than or equal to "
"zero! \n\nPlease enter the root vertex: ");
scanf("%d", &root);
}
/*Get terminating node and check for erroneous input */
printf("Please enter the terminating vertex: ");
scanf("%d", &end);
while (end > n || end <= 0)
{
printf("\nERROR: value must be less than or equal to the total number"
" of\n verticies and may not be less than or equal to "
"zero! \n\nPlease enter the terminating vertex: ");
scanf("%d", &end);
}
/* Fill adjacency matrix */
printf("\n\nPlease enter the distance between verticies i and j below.\nIf "
"the two verticies are disjoint, enter a negative number "
"instead.\n");
int adjMatrix[n][n];
/* Scan across entire array */
for (i = 0; i < n; ++i)
{
/* Scan across only half of array; no need to ask user for the same
* distance twice.
*/
for (j = 0; j < i; ++j)
{
printf("\ndistance(%d,%d) = ", i + 1, j + 1);
scanf("%f", &distance);
/* Distance from i to j is the same as the distance from j to i.
* Hence we place it in two parts of the adjacency matrix.
*/
adjMatrix[i][j] = distance;
adjMatrix[j][i] = distance;
}
/*For the case of i = j, set distance to -1 as self-loops are not
* taken into consideration here.
*/
adjMatrix[j][i] = -1;
}
dijkstra(root,end,adjMatrix,n);
return 0;
}
void dijkstra(int root, int end, int size, int adjMatrix[size][size])
{
int i = adjMatrix[3][1];
}