-4

プリムのアルゴリズムをコーディングしようとしています。ただし、コンパイルすると、次の行でエラーが発生します: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)エラー:代入で'int'を'int()(int)'に変換できません(LINE 60)

4

1 に答える 1

3

スペルを間違えclosedました。close代わりに入力しました。

closeのすべてのインスタンスを。に置き換えますclosed

#include <cstdlib>また、ファイルの先頭に追加します。

于 2012-05-01T20:53:52.867 に答える