Please help me: you need to select all negative values ​​from a given two-dimensional dynamic array and replace them with positive values, and vice versa.

I created the array randomly:

#include <iostream> #include <string> using std::cout; using std::cin; using std::endl; int m, n, i, j, l, i_cont; int main() { setlocale(0, ""); cout << "Введите количество строк n= "; cin >> n; cout << endl; cout << "Введите количество столбцов m= "; cin >> m; cout << endl; int *str = new int[n]; // Инициализация двумерного динамического массива int **a = new int*[n]; // n строк в массиве for (i = 0; i < n; i++) a[i] = new int[m]; // и m столбцов for (l = 0; l <= i_cont; l++) { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { a[i][j] = 0.001 * rand(); if (a[i][j] < 10) cout << a[i][j] << " "; else cout << a[i][j] << " "; } cout << endl; } ??????????? } delete[] str; for (i = 0; i < n; i++) delete[] a[i]; system("pause"); } 
  • Code without a tab is difficult to read ... - HolyBlackCat
  • for (l = 0; l <= i_cont; l ++), and your i_cont is zero - AR Hovsepyan
  • rand() never returns negative values. What is int *str = new int[n]; and why is it needed? What is i_cont and why is it needed? Why is memory a not freed? - AnT
  • int * str = new int [n]; // initialization can be executed immediately when a dynamic object is declared - Hattori Hanzo
  • It is wonderful. I repeat the question again: what is str and why is it needed ??? Your program doesn’t use this str . What is it here for? - AnT

1 answer 1

 int n, m; cout << "Введите количество строк n= "; cin >> n; cout << endl; cout << "Введите количество столбцов m= "; cin >> m; cout << endl; int a[n][m]; bool b[n][m]; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { a[i][j] = rand() % 100 - 50; if (a[i][j] < 0) { b[i][j] = true; a[i][j] = abs(a[i][j]); } else b[i][j] = false; } // обратно for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (b[i][j]) a[i][j] = -a[i][j]; } 
  • 3
    In C ++, there are no embedded arrays whose size is not known at the compilation stage. int a[n][m] is no longer C ++. - AnT
  • And what does it mean? - AR Hovsepyan
  • That is exactly what it means. Since n and m are not constant expressions, the declaration int a[n][m] is an error in C ++. There are such arrays in C, but not in C ++. In C ++, std::vector plays this role. - AnT
  • @AnT, C ++ does not deny expressions in C. Nobody claimed that it was C ++ - AR Hovsepyan
  • one
    Yes, I agree, reservations are compilers :) and the studio is a separate subject for endless debate. - NewView pm