#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
double sredn(int i, int M, double **A)
{
double summ = 0;
for (int j = 0; j < M; j++)
{
summ = summ + A[i][j];
}
return summ / M;
}
void readMatrix(int &N, int &M, double** (&A), FILE *dat)
{
fscanf(dat, "%d", &N);
fscanf(dat, "%d", &M);
A = new double*[N];
for (int i = 0; i < N; i++)
{
A[i] = new double[M];
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
fscanf(dat, "%lf", &A[i][j]);
}
}
}
void writeMatrix(int N, int M, double **A, FILE *res)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
fprintf(res, "%6.2lf ", A[i][j]);
}
fprintf(res, "\n");
}
}
void writeMassiv(int N, double *B, FILE *res)
{
for (int i = 0; i < N; i++)
{
fprintf(res, "%6.2lf ", B[i]);
}
}
double massiv(int N, int M, double **A, double *B)
{
for (int i = 0; i < N; i++)
{
B[i] = sredn(i, M, A);
}
return 0;
}
bool summa(int N, int M, double **A)
{
bool sum = false; // Допустим сумма НЕ отрицательна
double summ = 0;
for (int i = 0; i < N; i++)
{
summ = summ + A[i][M - 1];
}
if (summ < 0)
{
sum = true;
}
return sum;
}
int main(int argn, char *argc[])
{
FILE *dat, *res;
dat = fopen("D:\\in.txt", "r");
res = fopen("D:\\out.txt", "w");
int N = 0;
int M = 0;
double **A = nullptr;
readMatrix(N, M, A, dat);
double *B = new double[N];
fprintf(res, "Задана матрица A из %d строк и %d столбцов:\n", N, M);
bool sum = summa(N, M, A);
if (true)
{
fprintf(res, "\nСумма элементов последнего столбца не отрицательна\n");
fprintf(res, "\n");
massiv(N, M, A, B);
writeMassiv(N, B, res);
fprintf(res, "\n");
fprintf(res, "\n");
}
else
{
fprintf(res, "\nСумма элементов последнего столбца отрицательна\n");
fprintf(res, "\n");
writeMatrix(N, M, A, res);
}
fclose(dat);
fclose(res);
for (int i = 0; i < N; i++)
{
delete[] A[i];
}
delete[] A;
delete[] B;
}