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

4
  • 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. Commented Oct 22, 2022 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. Commented Oct 22, 2022 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?) Commented Oct 22, 2022 at 19:49
  • Use a std::vector. Commented Oct 23, 2022 at 9:24

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Under C++ std::array should be preferred to C arrays, so it would be std::array<std::vector<int>, 2>. Perhaps it can be combined with the multidimensional subscript [] operator supported by gcc and clang.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.