Question: c++ help please I need some help with this code, it isn't outputting correctly. Program is called by ./progname < WAM My code: #include #include

c++ help please

I need some help with this code, it isn't outputting correctly. Program is called by ./progname < WAM

My code:

#include #include #include using namespace std; #define INF 9999999 #define MAX 1000 #define M 10 int main() { int i = 0; int j = 0; int v = 0; int edge = 0; int selected[v]; int matrix [MAX][MAX]; char x[MAX]; cout << "Enter the WAM: " << endl; cin.getline(x, MAX, ' '); stringstream ssin(x); while (ssin.good() && j < M) { ssin >> matrix[0][j]; ++j; } i = j; v = j;

for (int c = 1; c < i; c++) { cin.getline(x, MAX, ' '); stringstream ssin(x); j = 0; while (ssin.good() && j < M) { ssin >> matrix[c][j]; ++j; } }

cout << "The WAM entered is: " << endl;

for (int c = 0; c < i; c++) { for (int d = 0; d < j; d++) { cout << matrix[c][d] << " "; } cout << endl; }

for (int s = 0; s < v; s++) { selected[s] = false; } selected[0] = true;

int z = 0; //row int y = 0; ///column cout << "Edge : Weight" << endl; while (edge < v - 1) { int min = INF; z = 0; y = 0; for (int c = 0; c < v; c++) { if (selected[c]) { for(int d = 0; d < v; d++) { if (!selected[d] && matrix[c][d]) { if (min > matrix[c][d]) { min = matrix[c][d]; z = c; y = d; } } } } }

cout << z << " - " << y << " : " << matrix[z][y]; cout << endl; selected[y] = true; edge++; } return 0; } WAM file contents:

0 0 355 0 695 0 0 0

0 0 74 0 0 348 0 0

355 74 0 262 0 269 0 0

0 0 262 0 0 242 0 0

695 0 0 0 0 151 0 0

0 348 269 242 151 0 83 306

0 0 0 0 0 83 0 230

0 0 0 0 0 306 230 0

Expected output:

Enter the WAM:

The WAM entered is:

0 0 355 0 695 0 0 0

0 0 74 0 0 348 0 0

355 74 0 262 0 269 0 0

0 0 262 0 0 242 0 0

695 0 0 0 0 151 0 0

0 348 269 242 151 0 83 306

0 0 0 0 0 83 0 230

0 0 0 0 0 306 230 0

Edge : Weight 0 - 2 : 355 2 - 1 : 74 2 - 3 : 262 3 - 5 : 242 5 - 6 : 83 5 - 4 : 151 6 - 7 : 230

Additionally if anyone can modify the code so that it prints the min spanning tree that would be really helpful and greatly appreciated. It looks like this:

0 0 355 0 0 0 0 0

0 0 74 0 0 0 0 0

355 74 0 262 0 0 0 0

0 0 262 0 0 242 0 0

0 0 0 0 0 151 0 0

0 0 0 242 151 0 83 0

0 0 0 0 0 83 0 230

0 0 0 0 0 0 230 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!