C/C++

Написать программу на C++

Дан текстовый файл. В первой строке которого содержится количество строк

матрицы, со второй строки построчно даны значения элементов матрицы. Создать

файл содержащий в первой строке общее количество отрицательных элементов, во

второй - количество отрицательных элементов в каждой строке, разделенных

пробелом.
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>

using namespace std;

int main()
{
int **A;
int s,i,j,k,num;
vector <int> N;
ifstream ifs;
ifs.open("input.txt");
if(ifs.is_open())
{
ifs >> s;
while(!ifs.eof())
{
ifs >> num;
N.push_back(num);
}
ifs.close();
}
else
{
cerr << "Файл с данными не открыт!" << endl;
return 1;
}
A = new int*[s];
for(int x = 0; x < s; x++)
A[x] = new int[N.size()/s];

i = 0, k = 0;
while(i < s)
{
j = 0;
while(j < N.size() / s)
{
A[i][j] = N[k++];
j++;
}
i++;
}

int count_minus = 0, *count_str;
count_str = new int[s];
for(int x = 0; x < s; x++)
{
count_str[x] = 0;
for(int y = 0; y < j; y++)
{
if(A[x][y] < 0)
count_str[x]++;
}
count_minus += count_str[x];
}

ofstream ofs;
ofs.open("output.txt");
if(ofs.is_open())
{
ofs << count_minus << endl;
for(int x = 0; x < s; x++)
{
ofs << count_str[x] << ' ';
}
ofs.close();
}

if(count_str != (int*) nullptr)
{
delete [] count_str;
count_str = (int*) nullptr;
}
else
exit(1);

if(A != (int**) nullptr)
{
for(int x = 0; x < 4; x++)
{
if(A[x] != (int*) nullptr)
{
delete [] A[x];
A[x] = (int*) nullptr;
}
else
exit(1);
}
delete [] A;
A = (int**) nullptr;
}
else
exit(1);
return 0;
}
ДЧ
Динис Черногурский
88 239
Лучший ответ