Загрузка данных


#include <iostream>
#include <fstream>
#include <clocale>

using namespace std;

const int rows = 7;
const int cols = 6;

// функция подсчета сумм отрицательных элементов по столбцам
void colSums(int arr[rows][cols], int res[cols])
{
    for (int j = 0; j < cols; j++) // идем по столбцам
    {
        int sum = 0;

        for (int i = 0; i < rows; i++) // идем по строкам
        {
            if (arr[i][j] < 0)
            {
                sum += arr[i][j];
            }
        }

        res[j] = sum; // сохраняем сумму для текущего столбца
    }
}

int main()
{
    setlocale(LC_ALL, "Russian");

    fstream fin("in4.txt", ios::in);
    fstream fout("out4.txt", ios::out);

    if (!fin.is_open() || !fout.is_open())
    {
        cout << "Ошибка открытия файлов in4.txt или out4.txt!" << endl;
        system("pause");
        return 1;
    }

    int Y[rows][cols];

    // читаем матрицу из файла
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            fin >> Y[i][j];
        }
    }

    // выводим матрицу в консоль
    cout << "Исходная матрица Y (7x6):" << endl;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << Y[i][j] << " ";
        }
        cout << endl;
    }

    // записываем матрицу в файл
    fout << "Исходная матрица Y (7x6):" << endl;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            fout << Y[i][j] << " ";
        }
        fout << endl;
    }

    int sums[cols];

    // передаем матрицу в функцию как двумерный массив
    colSums(Y, sums);

    // выводим полученные суммы
    cout << "\nСуммы отрицательных по столбцам:" << endl;
    fout << "\nСуммы отрицательных по столбцам:" << endl;

    for (int j = 0; j < cols; j++)
    {
        cout << "Столбец " << j + 1 << ": " << sums[j] << endl;
        fout << "Столбец " << j + 1 << ": " << sums[j] << endl;
    }

    fin.close();
    fout.close();

    system("pause");
    return 0;
}