Question
C++ help please I need help with the output. The weights for the edges aren't correct, please modify this code. Background: User either enters WAM
C++ help please
I need help with the output. The weights for the edges aren't correct, please modify this code. Background: User either enters WAM manually or from sperate file and called by ./progname < WAM ...... *Size isn't fixed and can't ask the user*
My Code:
#include
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 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 For reference this is what the output for the weights should be:
Edge : Weight 0 - 2 : 355 2 - 1 : 74 2 - 3 : 262 3 - 5 : 242 5 - 6 : 83 5 - 4 : 151 6 - 7 : 230
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started