Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #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 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

10-17 Could Nasty Gal have avoided bankruptcy? Explain your answer.

Answered: 1 week ago

Question

5. Arranging for the training facility and room.

Answered: 1 week ago

Question

1. Discuss the five types of learner outcomes.

Answered: 1 week ago