The file contains a two-dimensional array of integers of the form (spaces between numbers> = 1)

x_11 x_12 ... x_1m x_21 x_22 ... x_2m .................. x_n1 x_n2 ... x_nm 

It is necessary to fill a one-dimensional array of integers arr and two integers i, j so that arr = { x_11, x_12, ..., x_1m, x_21, x_22, ..., x_2m, ..., x_n1, x_n2, ..., x_nm } and i = n, j = m , in one cycle.

Closed due to the fact that off-topic participants Streletz , D-side , sercxjo , user194374, Kromster 9 Jun '16 at 5:35 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Streletz, D-side, sercxjo, Community Spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What did you do and what did not work out? - free_ze
  • @free_ze failed to complete and calculate in 1 cycle, this is the question - mrr
  • one
    Give the code and you will indicate errors. In the meantime, it’s like: “Do it for me.” - free_ze
  • one
    They are sometimes closed, yes. - free_ze
  • five
    Where did the restriction “for one cycle” come from? If it is a production code, the restriction is meaningless. If this is a training task, then decide it yourself. - VladD

1 answer 1

You can do without cycles (explicit) and unnecessary variables:

 #include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> v; std::copy(std::istream_iterator<int>(std::cin), {}, std::back_inserter(v)); std::copy(std::begin(v), std::end(v), std::ostream_iterator<int>(std::cout, " ")); } 

Check result


To get the count of rows and columns, you can read line by line:

 #include <algorithm> #include <iostream> #include <iterator> #include <sstream> #include <vector> int main() { std::vector<int> v; std::string s; int lines = 0; while(std::getline(std::cin, s)) { std::istringstream ss{s}; std::copy(std::istream_iterator<int>{ss}, {}, std::back_inserter(v)); ++lines; } std::copy(std::begin(v), std::end(v), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\nlines=" << lines << " rows=" << v.size() / lines << "\n"; } 

Check result

And it would be possible to go down character-by-character reading. Parse a numeric string for spaces and newlines. But I'm not ready for it :)

  • and the sizes M and N (well, that is, the number of lines and the number of characters in a line separately) how to find out from here?) - pavel