Please find the error. If possible, complete the task

A square matrix is given. If in the i-th row of the matrix the element belonging to the main diagonal is negative, then replace this element with the sum of the elements of the i-th row; otherwise, with the product of the elements of the i-th row. Output the elements of the main diagonal (modified) to a one-dimensional array in ascending order. Output the original and transformed matrices, the resulting array.Here is the actual problem, I wrote this code, but it outputs some cosmic numbers. Even when I try output the transformed matrix, for some reason it is displayed in a column. Please help me.

#include "stdafx.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    setlocale(0, "");
    srand(unsigned(time(NULL)));
    double sum = 0, proizv = 1;
    int N, M, fv, lv;

    cout << "_________________________________________________________________________"<< endl;
    cout << "Введите размер матрицы: ";
    cin >> N >> M;
    cout << "Введите нижний предел рандомных чисел" << endl;
    cin >> fv;
    cout << "Введите верхний предел рандомных чисел" << endl;
    cin >> lv;
    int** A = new int*[N];
    for (int i = 0; i < N; i++)
        A [i] = new int[M];

    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            A[i][j] = fv + rand() % (lv - fv + 1);;
                    
    
    cout << "Вот матрица:" << endl;
    for (int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
            cout << A[i][j] << " ";
        cout << endl;
     }
    cout << endl;
    for (int i = 0; i < N; i++)   
    {
        for (int j = 0; j < M; j++)
        {
            if (A[i][i] < 0)
            {
                A[i][i] = sum + A[i][j];
            }
        }
    }
    for (int i = 0; i < N; i++)   
    {
        for (int j = 0; j < M; j++)
        {
            if (A[i][i] >= 0);
            {
                proizv *= A[i][j];
                A[i][i] = proizv * A[i][j];
            }   
        }
    }
    cout << "Преобразованная матрица:" << endl;
    for (int i = 0; i < N; i++)
    {
       for (int j = 0; j < M; j++)
       {
           cout << A[i][j] << "\t";
           cout << "\n";
       }
    }
    system("pause");
    return 0;
}
 0
c++
Author: EOF, 2020-11-07

1 answers

Here is this part of the code:

for (int i = 0; i < N; i++)   
    {
        for (int j = 0; j < M; j++)
        {
            if (A[i][i] < 0)
            {
                A[i][i] = sum + A[i][j];
            }
        }
    }
    for (int i = 0; i < N; i++)   
    {
        for (int j = 0; j < M; j++)
        {
            if (A[i][i] >= 0);
            {
                proizv *= A[i][j];
                A[i][i] = proizv * A[i][j];
            }   
        }
    }

Can be replaced with:

for (int i = 0; i < N; i++)
{
    if (A[i][i] < 0)
    {
        int sum = 0;
        for (int j = 0; j < N; j++)
        {
            sum += A[i][j];
        }
        A[i][i] = sum;
    }
    else if (A[i][i] > 0)
    {
        int product = 1;
        for (int j = 0; j < N; j++)
        {
            product *= A[i][j];
        }
        A[i][i] = product;
    }
    else
    {
        A[i][i] = 0;
    }
}
 0
Author: EOF, 2020-11-07 17:07:03