Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Someone please help me to correct my error on the following code. Thank you. Her

ID: 3622691 • Letter: S

Question

Someone please help me to correct my error on the following code. Thank you.

Here is the code.....
#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[6][6] = {
{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[6];
bool visited[6];

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 ", message);
for (int i=0; i<6; i++) {
if (node[i].pi >= 0) {
int parent = node[i].pi;
printf("%c -- %c ", node[i].name, node[parent].name);
}
}
}

int 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[0].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:");
}


Here is the error message I get on line 34. error C2065: 'i' : undeclared identifier Thanks in advance.

Explanation / Answer

well, i dont know what your code is doing, but to fix the error you have specified, change:

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;
}

to

basically, you were trying to access the variable i even though its scope had ended. just move the bracket down to include the for-loop. i dont know if this changes how the program is ran, but at least it does.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote