0

So I need to be able to read integers from a file into a two dimensional array. The numbers are sorted as follows:
3 3 4 5
6 3 5 6 7 8 9
and so on and so forth.
My issue is I'm not sure how to start because the first number in each row indicates the amount of numbers that are going to be read into array[i][j] where j would equal either 3 or 6. I also don't know how how many rows are going to be in the file to be tested later.

3
  • 3
    You are using C++. Please explain why the "magic problem avoider" std::vector is not an option. Commented Jan 13, 2021 at 7:55
  • 1
    You start by reading line by line. Then you split each line on space and parse each piece as an integer. The initial amount indicator is quite useless with this format so you can either ignore it or you can use it for consistency check. You don't know a priori how many rows there are and so you need to use some dynamic container, e.g. vector. Commented Jan 13, 2021 at 8:01
  • Imagine doing this task manually with a box full of cards. Then transform this approach to a computer program so do don't have to do it yourself... Commented Jan 13, 2021 at 8:13

1 Answer 1

2

As others pointed out you really should use std::vector for this. But if it have to be an array you could either store everything into a vector anyway and copy the content of that vector into an new array afterwards. Or you read the file line by line until the end such that you know the number of rows and then you can allocate and fill an array.

And for the size of the second dimension of this array: it should always be 7, except each and every row starts with 3 (where the size would be 4).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.