Home > front end >  C 2d array using vectors returns a consistent incorrect result on the second query
C 2d array using vectors returns a consistent incorrect result on the second query

Time:09-22

I'm doing a coding challenge where the aim is to take x arrays and query it y times. Each array is given a size (N) and a list of values and each query wants a specific value (b) from a specific array (a).

The input is given as follows:

X Y
N n n n ...
N n n n ...
a b
a b

Here is all of my code:

#include <vector>
#include <iostream>
using namespace std;

int main() {
    int numArrays;
    int numQueries;
    scanf("%d %d", &numArrays, &numQueries);
    
    vector<int*> arrays;
    for (int i = 0; i < numArrays; i  )
    {
        int size;
        scanf("%d", &size);
        
        int arr[size];
        for (int j = 0; j < size; j  )
        {
            scanf("%d", &arr[j]);
        }
        
        arrays.push_back(arr);
    }
    
    for (int i = 0; i < numQueries; i  )
    {
        int arr, ind;
        scanf("%d %d", &arr, &ind);
        printf("%d\n", arrays.at(arr)[ind]);
    }
    return 0;
}

The test input is 2 arrays and 2 queries given as follows:

2 2
3 1 2 3
5 9 8 7 6 5
0 1
1 3

The expected output should give 2 for the first query (which seems to be working fine) and then 6 for the second query however my actual output is this:

2
32767

Does anybody know why the second output comes out as 32767. This seems to be a consistent value no matter what the arrays contain or what the second query is looking for. I think it's likely something to do with my vector declaration but I'm still relatively new to C so I'm not sure.

CodePudding user response:

The problem has been already described in comments by others: You are storing a pointer to an array on the stack that will be undefined outside the for loop. Just use vectors for both array dimensions and let them handle pointers and memory allocations for you:

#include <iostream>
#include <utility>
#include <vector>

int main() {
  size_t numArrays, numQueries;
  std::cin >> numArrays >> numQueries;
  std::vector<std::vector<int>> arrays;

  for (size_t i = 0; i < numArrays;   i) {
    size_t size;
    std::cin >> size;
    std::vector<int> array;
    for (size_t j = 0; j < size;   j) {
      int number;
      std::cin >> number;
      array.push_back(number);
    }
    arrays.push_back(std::move(array));
  }

  for (size_t i = 0; i < numQueries;   i) {
    size_t arr, ind;
    std::cin >> arr >> ind;
    std::cout << arrays[arr][ind] << std::endl;
  }
}

Admittedly, this lacks even the most basic error checking and error handling; it is simply a rewrite of the code from the question.

Of course you can handle the memory allocations yourself, but in that case you also need to make sure things get properly deallocated. (Again, the following example lacks error checking.)

#include <iostream>
#include <memory>
#include <utility>

int main() {
  size_t numArrays, numQueries;
  std::cin >> numArrays >> numQueries;
  const auto arrays{
      std::make_unique_for_overwrite<std::unique_ptr<int[]>[]>(numArrays)};

  for (size_t i = 0; i < numArrays;   i) {
    size_t size;
    std::cin >> size;
    arrays[i] = std::make_unique_for_overwrite<int[]>(size);
    for (size_t j = 0; j < size;   j) std::cin >> arrays[i][j];
  }

  for (size_t i = 0; i < numQueries;   i) {
    size_t arr, ind;
    std::cin >> arr >> ind;
    std::cout << arrays[arr][ind] << std::endl;
  }
}
  • Related