I am trying to recreate both Kruskal's and Prim's Algorithm. To do this, I need a weighted adjacency matrix to be entered.
An example input of a Weighted Adjacency Matrix 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
These numbers have been edited a small amount to give more clarity. There is supposed to be one space separating each of the numbers.
As for accepting these values and recording them I am able to accept one line of them with this code:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string temp;
string buf;
vector<string> matrix;
cout << "Matrix: ";
getline(cin, temp);
stringstream ss(temp);
while(ss >> buf)
{
matrix.push_back(buf);
}
for(int i = 0; i < matrix.size(); i++)
{
cout << matrix[i] << " ";
}
cout << endl;
return 0;
}
Example output:
Matrix: 0 0 355 0 695 0 0 0
0 0 355 0 695 0 0 0
Onto my question, how can I ask for the correct amount of the matrix lines and record all of them?
In the case of the example, How would I ask for 7 more lines of matrix values (Without needing a cout/cin statement before accepting values)?