The check function checks if the line number is odd

bool check(int i) { if (i % 2 != 0) return true; } 

The fill function changes an element of a two-dimensional array to the corresponding element of a one-dimensional

 int fill(int**mas, int*mas1, int i, int j) { mas[i][j] = mas1[i]; return mas[i][j]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (check(i) ==true) { mas[i][j] = fill(mas, mas1, i, j); } } } 

odd lines

so it works. why doesn’t it work when I similarly want to replace even lines?

 bool check(int i) { if (i % 2 == 0) return true; } 

even lines

    2 answers 2

    Tell me, what does your function return when i%2 != 0 ?

     bool check(int i) { if (i % 2 == 0) return true; } 

    How did you compile it at all? After all, there was clearly a warning - why did you not listen to him?

    • there was no warning - choko
    • one
      @choko change the compiler) - pavel
    • Or add a key ... And what compiled something? - Harry
    • @Harry local windows debugger in microsoft visual studio - choko
    • Strange. See here - i90.fastpic.ru/big/2017/0117/84/84 - created with all the default settings. See the underlined red? This is a warning to your function. Did you have no such warning? - Harry

    This feature

     bool check(int i) { if (i % 2 != 0) return true; } 

    has an undefined behavior, since it returns nothing when the number i is even.

    In addition, it is written in your assignment that you need to replace all the lines with even numbers, whereas in this cycle

     for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (check(i) ==true) { mas[i][j] = fill(mas, mas1, i, j); } } } 

    there is an attempt to replace all the odd rows of a two-dimensional array.

    In addition, there is no point in trying to pass a two-dimensional array to this function.

     int fill(int**mas, int*mas1, int i, int j) { mas[i][j] = mas1[i]; return mas[i][j]; } 

    First, the types of argument and parameter are not compatible if the argument is really a two-dimensional array, and not a one-dimensional array of pointers to the first elements of one-dimensional arrays. And, secondly, it is enough to pass one term of a two-dimensional array to a function in order to fill it, and not the entire two-dimensional array. And, thirdly, in the body of the function you are only trying to assign one element, and not the entire line. It doesn't make much sense.

    Based on the above, the relevant functions can be defined as follows:

     bool is_even( int i ) { return i % 2 == 0; } 

    The fill function may look like this.

     void fill( int *mas, const int *mas1, int n ) { for ( int i = 0; i < n; i++ ) mas[i] = mas1[i]; } 

    Then the program cycle will be

     for (int i = 0; i < n; i++) { if ( is_even( i ) ) fill( mas[i], mas1, n ); } 

    It would be possible not to write the is_even function, since it is known that even indices are 0, 2, etc.

    Therefore one could simply write

     for (int i = 0; i < n; i +=2) { fill( mas[i], mas1, n ); } 

    Instead of the own function fill you can use the standard algorithm std::copy , declared in the header <algorithm>

    Then everything would look very simple. Turn on the header

     #include <algorithm> //... 

    and write a loop

     for (int i = 0; i < n; i +=2) { std::copy( mas1, mas1 + n, mas[i] ); }