The function adds the number 9 to the end of the dynamic two-dimensional array.

The problem is that it works correctly only if the matrix is ​​square.

Part of the code:

void addColEnd(int**& ar, int& refm, int& refn) { int **ar1 = 0; refn++; ar1 = new int*[refm]; for (int i = 0; i < refm; i++) { *(ar1 + i) = new int[refn]; } for (int i = 0; i < refm; i++) { for (int j = 0; j < refn-1; j++) { *(*(ar1 + i) + j) = *(*(ar + i) + j); } } for (int j = 0; j < refm; j++) { *(*(ar1 + j) + refn - 1) = 9; } delete[]ar; ar = ar1; for (int i = 0; i < refm; i++) { for (int j = 0; j < refn ; j++) { cout << *(*(ar + i) + j) << " "; } cout << endl; } } 

Closed due to the fact that the essence of the issue is incomprehensible by the participants αλεχολυτ , aleksandr barakin , rjhdby , cheops , post_zeew Oct 22 '16 at 16:04 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • the question is not clear. formulate correctly, please. - Senior Pomidor

1 answer 1

Data cycles

 for (int i = 0; i < refm; i++) { for (int j = 0; j < refn; j++) { ^^^^^^^^ *(*(ar1 + i) + j) = *(*(ar + i) + j); } } 

lead to indefinite program behavior, as in the sentence

  *(*(ar1 + i) + j) = *(*(ar + i) + j); 

there is an attempt to refer to the non-existent elements of the array ar , which has refn - 1 columns.

The same problem exists for the cycle.

 for (int j = 0; j < refn; j++) { *(*(ar1 + j) + refn-1) = 9; ^^^^^^^ } 

since the ar1 array has refm lines, not refn lines.

Also, you do not delete all the memory allocated for the original array, which leads to a memory leak.

Below is a demonstration program that shows how this function can be defined. I called it appendArray

 #include <iostream> #include <algorithm> void freeArray( int ** &a, size_t m ) { for ( size_t i = 0; i < m; i++ ) delete [] a[i]; delete [] a; a = nullptr; } bool appendColumn( int ** &a, size_t m, size_t n, int value ) { int ** tmp = new int *[ m ]; for ( size_t i = 0; i < m; i++ ) tmp[i] = new int[ n + 1 ]; for ( size_t i = 0; i < m; i++ ) { std::copy( a[i], a[i] + n, tmp[i] ); } for ( size_t i = 0; i < m; i++ ) tmp[i][n] = value; freeArray( a, m ); a = tmp; return true; } int main() { size_t m = 2; size_t n = 3; int **a; a = new int *[m]; a[0] = new int[n] { 1, 2, 3 }; a[1] = new int[n] { 4, 5, 6 }; for ( size_t i = 0; i < m; i++ ) { for ( size_t j = 0; j < n; j++ ) std::cout << a[i][j] << ' '; std::cout << std::endl; } n += appendColumn( a, m, n, 9 ); for ( size_t i = 0; i < m; i++ ) { for ( size_t j = 0; j < n; j++ ) std::cout << a[i][j] << ' '; std::cout << std::endl; } freeArray( a, m ); return 0; } 

Her console output is as follows.

 1 2 3 4 5 6 1 2 3 9 4 5 6 9 
  • one
    Thanks changed, but the array is displayed only square (entered 10 by 6, only 6 is displayed by 6) Now I will change the code. - Adam Hodovanets
  • @AdamHodovanets So they changed the program without fixing all the errors. See my updated answer. - Vlad from Moscow
  • @AdamHodovanets And in vain you change your answer, making changes in accordance with my comments, as it introduces the error of reading the question and answer. - Vlad from Moscow