0

I want to create a two-dimensional array and one of the parameters should be 2, but the other one has to be a variable. This is how I tried it:

int a[2][n];
int i, test_cases;

int main(){
    cin>>test_cases;
    for(i=0; i<test_cases; i++){
        cin>>n;
    }

}

compiler says: error: 'n' was not declared in this scope

Ken White
  • 123,280
  • 14
  • 225
  • 444
Myrodis
  • 75
  • 6
  • 3
    Where have you declared `n`? Also what value do you think `n` will be before `int main()` is executed? Remember that globals are initialized before main() starts and that even if your compiler supports the nonstandard VLA its size will not grow after it is initialized. – drescherjm Oct 22 '22 at 18:54
  • 1
    Do you know how to construct an array with length determined at run-time? Making the array two-dimensional just confuses the issue. – Beta Oct 22 '22 at 19:01
  • 2
    *"compiler says: [something]"* -- if you disagree with your compiler's assessment, add a rebuttal to your question. If you agree with the assessment, add an explanation of your understanding and what you tried to rectify the error. If you don't understand the error enough to agree or disagree, please mention that and try to refine your question by differentiating what you understand and what you don't (For example: Which `n` is involved? Which scope / what is a scope? What is a declaration?) – JaMiT Oct 22 '22 at 19:49
  • Use a [std::vector](https://en.cppreference.com/w/cpp/container/vector). – Jesper Juhl Oct 23 '22 at 09:24

1 Answers1

1

I apologize(thanks to @JaMiT) because misread your question.

In C++ number of rows and columns in a two dimensional array must be compile time constants. You are not allowed to use variable to set array dimension. So if second dimension must be variable you could use std::vector or another mechanism to allocate/deallocate dynamic memory. Here is my example with std::vector:

#include <vector>

using resizable_second_dim_array = std::vector<int> (&)[2];

void set_second_dimension(resizable_second_dim_array aArray, 
std::size_t aNewDim)
{
    for (auto & vec : aArray)
    {
        vec.resize(aNewDim);
    }
}

int main()
{
    std::vector<int> my_arr[2]{};
    
    std::size_t secondDim = 10;
    set_second_dimension(my_arr, secondDim);
    
    //do something with my_arr
    //...
    
    //change second dimension
    secondDim = 20;
    set_second_dimension(my_arr, secondDim);
    
    //do something with my_arr
    //...
    return 0;
}

As @Sebastian pointed out another option would be to use std::array:

#include <array>
#include <vector>

using resizable_second_dim_array = std::array<std::vector<int>, 2>;

void set_second_dimension(resizable_second_dim_array & aArray,
std::size_t aNewDim)
{
    for (auto & vec : aArray)
    {
        vec.resize(aNewDim);
    }
}

int main()
{
    resizable_second_dim_array my_arr{};
    
    std::size_t secondDim = 10;
    set_second_dimension(my_arr, secondDim);
    
    //do something with my_arr
    //...
    
    //change second dimension
    secondDim = 20;
    set_second_dimension(my_arr, secondDim);
    
    //do something with my_arr
    //...
    return 0;
}
dummy
  • 103
  • 6
  • 2
    Under C++ `std::array` should be preferred to C arrays, so it would be std::array, 2>. Perhaps it can be combined with the multidimensional subscript [] operator supported by gcc and clang. – Sebastian Oct 22 '22 at 23:09