我正在尝试编写Prim的算法。但是当我编译时,我在行上得到错误:57,59,60,62,63,65
显然这与我的阵列有关......我只是不知道到底是什么。我感觉它与我试图找出并失败的指针有关。
int map [] [7]是一个全局数组,用于跟踪我的初始图形。
编辑 - 这里要求的是所有代码
#include <iostream>
using namespace std;
void performPrims(int);
void print(int *);
// Global Variables
int nodes = 7;
int cur = 0;
int cost = 0;
int map[][7] =
{{0,3,6,0,0,0,0},
{3,0,7,0,0,12,0},
{6,7,0,10,13,8,0},
{0,0,10,0,0,0,5},
{0,0,13,0,0,9,4},
{0,12,8,0,9,0,11},
{0,0,0,5,4,11,0}};
int main()
{
// Initializations
srand(time(NULL));
int S = rand() % 7; // Create an arbitrary starting point
// Perform Prim's Algorithm starting with S
performPrims(S);
return 0;
}
void performPrims(int S)
{
int low = 100;
// open[] is used to keep track of what vertices are available
int open [] = {1,2,3,4,5,6,7};
// closed [] is used to keep track of what vertices have been reached and in what order
int closed [] = {0,0,0,0,0,0,0};
open [S] = 0; // Make our starting node unavailable
closed [cur] = S+1; // Record our starting node in the path
cur++;
while (cur < 7)
{
int i=0;
while (i<nodes)
{
int k=0;
while (k<nodes)
{
if (*map[*close[i]-1][k] > 0 && *map[*close[i]-1][k] < low)
{
low = map[close[i]-1][k];
close [cur] = k+1;
}
map[close[cur-1]-1][close[cur]-1] = 0;
map[close[cur]-1][close[cur-1]-1] = 0;
cost += low; low = 100;
open[close[cur]-1] = 0;
k++;
}
i++;
}
cur++;
}
print(closed);
}
void print(int *closed)
{
cout<<"The path taken was ";
for (int i=0; i<nodes; i++)
cout<<closed[i]<<" "<<endl;
cout<<"\nThe cost is "<<cost<<endl;
}
错误
有几个不同的:
错误:指向算术中使用的函数的指针(多行) 错误:无效类型'int [7] [7] [int(*)(int)]'对于数组下标(多行) 错误:指定只读位置(LINE 60) 错误:无法在赋值(LINE 60)中将'int'转换为'int()(int)'
答案 0 :(得分:3)
你拼错了closed
。您改为键入close
。
将close
的所有实例替换为closed
。
另外,将#include <cstdlib>
添加到文件的顶部。