Range-based for loops or for_each with multi-dimensional intra-dependent arrays

Hi, I've been using (bad) codes a lot for years, and I want to update them using latest/most optimized c++ tools now. I feel like I just need a little better understanding to get going.

I want to start using range-based for loops or for_each, and I'm kinda of blocked with this type of arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double interpolation_term_tab[8][8];
for (int n = 0 ; n < 8 ; n++)
  for (int i = 0 ; i < 8 ; i++) {
    interpolation_term_tab[n][i] = 0.0;
  }

for (int n = 0 ; n < 8 ; n++)
  for (int i = 0 ; i < n ; i++) {
    const int i_plus_one = i + 1;
		
    double interpolation_term = 1.0;
			
    for (int ii = 0 ; ii < n ; ii++) {
      if (i != ii) interpolation_term *= i_plus_one/static_cast<double> (i - ii);
    }
		
    interpolation_term_tab[n][i] = interpolation_term;		
  }


Is there a way of constructing such a (nested) array in a simpler way, as in with no raw loops. I don't even mind to use standard C++ algorithms. I just don't know where to start.

Thanks in advance!
Last edited on
The lines 1-5 you can achieve with:
double interpolation_term_tab[8][8] {};
http://en.cppreference.com/w/cpp/language/zero_initialization


You are writing values only to the lower triangle of the matrix.
That is definitely not a for each element operation.

You do process each row. However, you do need the index of the current row within the loop body, which neither range for nor for_each do provide.
Topic archived. No new replies allowed.