0

I sometimes get confused in these numbers with an array and can’t count because the beginning is from scratch and not from one. And so that I do not get confused, I have to remove the numbers with the size of the arr[4][4].

I want to find a way for this to work automatically, more precisely, it automatically determines the numbers.

I'm really confused for me arr[4][4]. it should be arr[3][3]. because the counter starts at zero 0,1,2,3

int arr[][] // <--
    {
        { 0,0,0,1 },
        { 1,0,0,0 },
        { 0,2,0,4 },
        { 1,1,0,1 }
    };
loli
  • 17
  • 6
  • Possible duplicate of [How to create a dynamic array of integers](https://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers) – ivanjermakov Oct 12 '19 at 17:48
  • @ivanjermakov: no dynamic in the question. It is about static ones :-) – Klaus Oct 12 '19 at 17:53
  • Can you please edit your question post and try to explain what your question is? I'm not sure it is very clear from what you're describing. – Kevin Oct 12 '19 at 17:56
  • My understanding is: OP did not want to provide the size parameters for array creation. Op searches a func/helper/something to get an two or multi dimensional array from the given parameters. – Klaus Oct 12 '19 at 18:04
  • What should be the type if the inner dimension is inconsistent? And would you be fine with making `arr` an `std::array>`? Also, can you use C++17? – chtz Oct 12 '19 at 18:05
  • Arrays in C and C++ (as well as most other languages) start with index 0. Best to get used to it now. – dbush Oct 12 '19 at 18:16

1 Answers1

0

You maybe like it with a helper function like that:

#include < array >
#include < iostream >

template < typename ... T  >
constexpr auto f( T ... args) 
{   
    return std::array< std::tuple_element_t<0, std::tuple<T...>>, sizeof...(args)> { args...};
}   

int main()
{   
    constexpr auto arr = 
        f(
            f( 0,0,0,1),
            f( 1,0,0,0),
            f( 0,2,0,4),
            f( 1,1,0,1)
         );  

    for ( auto& x: arr ) { 
        for ( auto y: x ) { 
            std::cout << y << " " ;
        }   
        std::cout << std::endl;
    }   

    // or access via []
    std::cout << arr[0][3] << std::endl;
    std::cout << arr[2][3] << std::endl;

}
Klaus
  • 24,205
  • 7
  • 58
  • 113