-1

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)?

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • Is your question just "how to read 7 lines from `std::cin`"? ... – user202729 Mar 09 '18 at 07:25
  • It is somewhat that. I need to read 7 additional lines, but I have to figure out how many additional lines to read. – Jonathan Larsen Mar 09 '18 at 07:26
  • Just ask a user to give you a number of rows and columns, then read required amount of data iterating over them. These hints should be enough to finish your task. – CaptainTrunky Mar 09 '18 at 07:26
  • [How to read to EOF from cin using C++](https://stackoverflow.com/questions/201992/how-to-read-until-eof-from-cin-in-c) – user202729 Mar 09 '18 at 07:27
  • So, that code accepts lines until EOF is reached. I am not reading from a file but having the lines entered at the command line. Is there a way to make it stop accepting lines after an amount that is discovered after the first line is entered? – Jonathan Larsen Mar 09 '18 at 07:31
  • Sure. Can you describe in plain language what the criteria for stopping are? Sorry, but your question seems to indicate that you are missing some real basics of the language and should rather work through a tutorial. – Ulrich Eckhardt Mar 09 '18 at 07:43
  • So, I am trying to get the user to enter the Weight Adjacency Matrix(WAM) (the block of 8x8 numbers). I need to accept numbers until the first enter is pressed. I need to figure out how many numbers are in the first line, then to accept the rest of the lines. The lines can be figured out by matrix.size()-1. But, the problem arises when I try to figure out the number of lines I need. I don't know how to work around not knowing the amount of lines in the matrix until the first line is entered. The problem doesn't allow me to ask for the number of lines, just ask for the WAM. – Jonathan Larsen Mar 09 '18 at 07:47
  • 1
    Your issue is unrelated to matrices. Please take the time to properly abstract the problem you are trying to solve. Ask about *that* problem, once you are there (or do a search and find the solution). – IInspectable Mar 09 '18 at 08:15
  • You can (almost) always send an EOF condition in an interactive terminal. Use Ctrl-D on Unix likes and F6 or Ctrl-Z on DOS/Windows (*almost always* means that a terminal can be set in special mode where it would not process specially the control characters, but it is not the default state) – Serge Ballesta Mar 09 '18 at 08:27

1 Answers1

0

Let's extract what you already have into a function:

std::vector<std::string> read_row(std::istream & is)
{
    string line;
    string number;
    vector<string> row;

    getline(is, line);

    stringstream ss(line);

    while(ss >> number)
    {
         row.push_back(number);
    }
    return row;
}

Now we can do that multiple times

int main()
{
     std::vector<std::vector<std::string>> matrix;
     std::vector<std::string> first_row = read_row(std::cin);
     matrix.push_back(first_row);
   // We already have the first line, so loop from 1
     for (int i = 1; i < first_row.size(); ++i)
     {
          std::vector<std::string> next_row = read_line(std::cin);
          if (next_row.size() != first_row.size())
          {
               std::cout << "Row " << i << " does not match length";
               return 1;
          }
          matrix.push_back(next_row);
     }
     return 0;
 }

Now you probably want to use the numbers to do calculation with, so instead of std::string you can use int or double. The ss >> number is a formatted input, so it will convert text to numbers for you.

Caleth
  • 52,200
  • 2
  • 44
  • 75