Untitled

 avatar
ketronix
c_cpp
2 years ago
2.4 kB
5
Indexable
Never
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
  const int n = 4;
  const int m = 4;

  int Matr[n][m] = { 0 };
  int Sum_r[n] = { 0 };
  int Sum_st[m] = { 0 };
  srand(time(NULL));
  cout << "Input Matrix" << n << 'x' << m << "\n";
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      Matr[i][j] = 1 + rand() % 5;

  for (int i = 0; i < n; i++)
  {cout << "|";
    for (int j = 0; j < m; j++)
    {
    cout << setw(4) << Matr[i][j] << " ";
    Sum_r[i] += Matr[i][j];

    }
    cout <<"|"<< endl;
  }

  cout << "\nSumm - rjadki \n";
  for (int i = 0; i < n; i++)
    cout << i << "rjadok:" << Sum_r[i] << endl;
       for (int i = 0; i < n; i++)
       {
         cout << "|";
         for (int j = 0; j < m; j++)
         {
         cout << setw(4) << Matr[i][j] << " ";
         Sum_st[j] += Matr[i][j];

          }
       cout << "|" << endl;
       }

  cout << "\nSumm - stovbec \n";
  for (int j = 0; j < n; j++)
    cout << j << "stovbec:" << Sum_st[j] << endl;

    int sum = 0;
// top row
    for (int i = 0; i < m; i++) {
        sum += Matr[0][i];
    }
// bottom row
    for (int i = 0; i < m; i++) {
        sum += Matr[n-1][i];
    }
// left column
    for (int i = 1; i < n-1; i++) {
        sum += Matr[i][0];
    }
// right column
    for (int i = 1; i < n-1; i++) {
        sum += Matr[i][m-1];
    }

    cout << "The sum of the elements along the perimeter of the matrix is: " << sum << endl;

    if (sum < 10) {
        int min_element = Matr[0][0];
        int min_index;
        // finding the minimum element in the first row
        for (int i = 1; i < m; i++) {
            if (Matr[0][i] < min_element) {
                min_element = Matr[0][i];
                min_index = i;
            }
        }

        int max_element = Matr[n-1][0];
        // finding the maximum element in the last column
        for (int i = 0; i < n; i++) {
            if (Matr[i][m-1] > max_element) {
                max_element = Matr[i][m-1];
            }
        }
        // replacing the minimum element of the first row with the maximum element of the last column
        Matr[0][min_index] = max_element;
    }

    for (int i = 0; i < n; i++)
    {cout << "|";
        for (int j = 0; j < m; j++)
        {
            cout << setw(4) << Matr[i][j] << " ";
            Sum_r[i] += Matr[i][j];

        }
        cout <<"|"<< endl;
    }

  return 0;
}