1

I have a int a[10][2] array. Can I assign values in other way like this:

int a = someVariableValue;
int b = anotherVariableValue;
for (int i = 0; i < 10; ++i){
   a[i][0] = a;
   a[i][1] = b;
}

like:

for (int i = 0; i < 10; ++i){
   a[i][] = [a,b]; //or something like this
}

Thank you! :)

Totati
  • 1,489
  • 12
  • 23
  • You may be interested in this question: https://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function – delgato Aug 12 '22 at 02:46

1 Answers1

5

Arrays do not have the assignment operator. However you could use an array of std::array.

For example

#include <iostream>
#include <array>

int main()
{
    const size_t N = 10;
    std::array<int, 2> a[N];
    int x = 1, y = 2;

    for ( size_t i = 0; i < N; ++i ) a[i] = { x, y };

    for ( const auto &row : a )
    {
        std::cout << row[0] << ' ' << row[1]  << std::endl;
    }
}

The output is

1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335