C/C++

Программирование алгоритмов обработки многомерных массивов.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
int N,M,P,**A,*B,*C,countM=0,countP=0,i;
do
{
cout << "N = ";
cin >> N;
}while(N<1);
A = new int*[N];
for(int x = 0; x < N; x++)
A[x] = new int[N];
srand(time(nullptr));
rand();
for(int x = 0; x < N; x++)
for(int y = 0; y < N; y++)
{
A[x][y] = rand() % 31 - 15; // [-15;15]
if(A[x][y] > 0)countP++;
else if(A[x][y] < 0)countM++;
}
B = new int[countM];
int x;
for(i = 0,x = 0; x < N && i < countM; x++)
for(int y = 0; y < N && i < countM; y++)
if(A[x][y] < 0) B[i++] = A[x][y];
C = new int[countP];
for(i = 0, x = 0; x < N && i < countP; x++)
for(int y = 0; y < N && i < countP; y++)
if(A[x][y] > 0) C[i++] = A[x][y];
for(x = 0; x < N; x++)
{
for(int y = 0; y < N; y++)
cout << A[x][y] << '\t';
cout << endl;
}
cout << endl;
for(x = 0; x < countM; x++)
cout << B[x] << ' ';
cout << endl << endl;
for(x = 0; x < countP; x++)
cout << C[x] << ' ';
cout << endl;
if(C == (int*) nullptr) exit(1);
else
{
delete [] C;
C = (int*) nullptr;
}
if(B == (int*) nullptr) exit(1);
else
{
delete [] B;
B = (int*) nullptr;
}
if(A == (int**) nullptr) exit(1);
else
{
for(x = 0; x < N; x++)
{
if(A[x] == (int*) nullptr) exit(1);
else
{
delete [] A[x];
A[x] = (int*) nullptr;
}
}
}
return 0;
}
АТ
Александр Туликпаев
65 511
Лучший ответ