Hello everyone,

Can anyone please help me figure out what I miss or did wrong to my code for not to compile?

below are the code I did and I just couldn't figure it out what I missed. Thanks.

``````#include <iostream>

typedef struct NodeType {
char name;		// the node's name
int weight;		// the node's current weight
int pi;			// the node's parent
} Node;

int graph = {
{0,1,2,0,0,0},
{1,0,5,9,0,10},
{2,5,0,3,0,0},
{0,9,3,0,3,0},
{0,0,0,3,0,2},
{0,10,0,0,2,0}
};

Node node;
bool visited;

int getMinNode()
{
int index = -1;
for (int i=0; i<6; i++)
{
if (!visited[i])
{
index = i;
break;
}
}
for (int j=i+1; j<6; j++)
{
if (node[j].weight < node[index].weight) index = j;
}
return index;
}

void printResult(char *message) {
printf("%s\n", message);
for (int i=0; i<6; i++) {
if (node[i].pi >= 0) {
int parent = node[i].pi;
printf("%c -- %c \n", node[i].name, node[parent].name);
}
}
}

void main() {	// Prim's algorithm

// Initialize the 6 nodes
for (int i=0; i<6; i++) {
node[i].name = 'A'+i;
node[i].pi = -1;		// no parent
node[i].weight = 10000;	// 10000 as infinity
visited[i] = false;
}
// Start from node 'A'
node.weight = 0;
int index = getMinNode();

// Prim's algorithm
while (index >= 0) {
visited[index] = true;
for (int j=0; j<6; j++) {
if (j==index) continue;
if (graph[j][index] == 0) continue;
int weight = node[index].weight + graph[j][index];
if (weight < node[j].weight) {
node[j].weight = weight;
node[j].pi = index;
}
}
index = getMinNode();
}

// Print out the result
printResult("Final Minimum Spanning Tree:");
}``````

## Recommended Answers

Think about what you're doing.

You want to start j at i + 1, is there any orther variable that you could use for this, that IS defined at for( int j=... ) ?

If you declare a variable inside the for statement like this, it is ONLY …

## All 5 Replies

hi, use code tags

``for (int j=i+1; j<6; j++)``

i is undefined in this line

So how can I define it?? Thanks. Please give me examples

Think about what you're doing.

You want to start j at i + 1, is there any orther variable that you could use for this, that IS defined at for( int j=... ) ?

If you declare a variable inside the for statement like this, it is ONLY visible inside the for block, after that you can not use it any more (unless you re-define it).

commented: This thread is not marked as answered. My post is a suggestion. And how is int i = 0; messy? And the negative counter for being an arrogant self rightous person. Just pm me dude. Or reply in the thread. +0
``````for (int i=0; i<6; i++) // i is defined for this block
{
if (!visited[i])
{
index = i;
break;
}
} // i definition ends here
for (int j=i+1; j<6; j++) // i is not defined
{
if (node[j].weight < node[index].weight) index = j;
}``````

If you want to use i then do the following:

``````int i = 0; // i is now defined for the main program
for (; i<6; i++) // i is used in this block
{
if (!visited[i])
{
index = i;
break;
}
}
for (int j=i+1; j<6; j++) // i is still defined since it is still in the main block
{
if (node[j].weight < node[index].weight) index = j;
}``````
commented: Don't give people exact code solutions for their question, let them think so that they will actually learn. +0
commented: Just because I don't like lamb. +0

To fix your problem

Just put this before line 53. (I think)

int i = 0;

commented: Thread has already been answered 3 times, beside that - your suggestion is messy (the second int i will mask the one you wish to add) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.