0

I started learning C++ a week ago, coming from C.

The input I have is of the following format:

7
13 -4 -10 4 9 7 -3
4
0 -2 -1 2
2
3 5
0

The first number gives the number of elements in the first array. We stop scanning arrays once this number is a zero.

I would like to scan these arrays into an array of arrays as follows:

[[13,-4,-10,4,9,7,-3] , [0,-2,-1,2] , [3,5]]

I know how to scan in the first array:

int n;
int array1[MAXLENGTH];
cin >> n;
for (int i = 0; i < n; i++) {
    cin >> array1[i];
    // scanf("%d", &array1[i]);
}

I get stuck on 0 -2 -1 2 since it starts with zero.

How can I scan in these arrays and stop once I encounter the last zero?

10
  • 2
    Why do you stop scanning at 0? If you stop scanning at 0, why do you ask for the number of elements? In any case, what is the desired behavior for the input 0 -2 -1 2? Commented Mar 6, 2017 at 16:30
  • 2
    Use std::vector instead of array. The std::vector can grow dynamically, using push_back(). Commented Mar 6, 2017 at 16:30
  • 1
    your code needs two distinct phases. Read the array length and if 0 stop. Then read the array (with no special 0 proccesing). Repeat Commented Mar 6, 2017 at 16:32
  • 2
    Are you reading all the data into one array or a 2d array? Commented Mar 6, 2017 at 16:33
  • 1
    @user6005857: No, you don't want to scan the data into an array of arrays. You want to scan the data into a vector of vector. The primary reason is the capacity of each array is not known at compile time. To reduce the defects injected by dynamically allocating memory for the array, use std::vector. Commented Mar 6, 2017 at 16:40

3 Answers 3

1

The input pattern is the same, regardless of data:

unsigned int array_index = 0U;
unsigned int quantity;
std::vector<std::vector<int> > database;
while (cin >> quantity)
{
  if (quantity == 0U)
  {
     break;
  }
  int value = 0;
  for (unsigned int i = 0; i < quantity; ++i)
  {
    cin >> value;
    database[array_index].push_back(value);
  }
  ++array_index;
}

A vector of vectors should be capable of containing the data.

The input data line:

4
0 -2 -1 2

The 4 represents the quantity of numbers for the second set. The 0 is the first datum of the second set of data.

There are 3 sets of data in your input file.

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

3 Comments

The second cin should be checked for errors. Don't trust the User or the data file.
You can probably fix this by a hack: add an empty array to the database: database.push_back(std::vector<int>()) before the inner loop. The code currently uses out-of-range subscripts; this will make sure the subscript valid.
@anatolyg: Good catch! I forgot about the outer array and the push_back.
1

As stated in other answers, you should have two nested loops.

This is the outer loop:

std::vector<std::vector<int>> data;

while (true)
{
    int size = 0;

    std::cin >> size;
    if (size == 0)
        break;

    std::vector<int> array;
    ... // fill the array
    data.push_back(array);
}

C++ vectors have dynamic size. That is, you don't need to know what the size is when you define the dynamic array - it will adjust its size as elements are added into it (using push_back). This is convenient for the outer loop.


However, for inner loop, it's more convenient to use pre-allocated vectors, because your code "knows" the size of the array early:

std::vector<int> array(size); // allocate the array and set all elements to zero
for (int i = 0; i < size; ++i)
{
    std::cin >> array[i];
}

You can also use a range-based loop:

std::vector<int> array(size); // allocate the array and set all elements to zero
for (int& value: array)
{
    std::cin >> value;
}

Comments

0
int n;
int array1[MAXLENGTH];
while(true)
{
cin >> n;
if(n == 0) break;
for (int i = 0; i < n; i++) {
    cin >> array1[i];
    // scanf("%d", &array1[i]);
}
}

plus of course you should be using std::vector, and you are lossing your input since it is all stored in the same array getting overwritten each time.

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.