In the square matrix, sort out those lines where an even number is on the side diagonal



  • #include <stdio.h>
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    int main()
    {
        setlocale(LC_ALL, "ru");
        srand(time(NULL));
        const int n = 6;
        int matr[n][n];
    
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; j++)
            {
                matr[i][j] = rand() % 100;//Заполняем массив случайными числами  до 100
                cout << matr[i][j] << "\t|\t"; //Вывод на экран
            }
            cout << endl;
        }
    
        for (int i = 0; i < n; ++i)
        {//Вибираем четные элементы побочной диагонали матрицы
            if (matr[i][n - 1 - i] % 2 == 0)
            {
                for (int k = 0; k < n - 1; ++k) //Сортировка
                    for (int i = 0; i < n - 1 - k; ++i)
                    {
                        if (matr[i][i + 1] < matr[i][i])
                        {
                            int c = matr[i][i + 1];
                            matr[i][i + 1] = matr[i][i];
                            matr[i][i] = c;
                        }
                    }
            }
        }
    
            cout << endl <<"Отсортированная матрица: " << endl;
    
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    cout << matr[i][j] << "\t|\t"; //вывод на экран новой марицы
                }
                cout << endl;
            }
        
        system("pause");
        return 0;
    }
    

    It doesn't. I don't even know what the problem is.



  • Here on C++, standard grading:

    #include <iostream>
    #include <time.h>
    #include <algorithm>
    using namespace std;
    

    int main() {
    srand(time(NULL));
    const int n = 6;
    int matr[n][n];

    for (int i = 0; i &lt; n; ++i) {
        for (int j = 0; j &lt; n; j++) {
            matr[i][j] = rand() %
                         100;//Заполняем массив случайными числами  до 100
            cout &lt;&lt; matr[i][j] &lt;&lt; "\t|\t"; //Вывод на экран
            }
    
        cout &lt;&lt; endl;
        }
    
    for (int i = 0; i &lt; n; ++i) {
        //Вибираем четные элементы побочной диагонали матрицы
        if (matr[i][n - 1 - i] % 2 == 0) {
            sort(&amp;matr[i][0], &amp;matr[i][0] + n);
            }
        }
    
    cout &lt;&lt; endl &lt;&lt; "Отсортированная матрица: " &lt;&lt; endl;
    
    for (int i = 0; i &lt; n; i++) {
        for (int j = 0; j &lt; n; j++) {
            cout &lt;&lt; matr[i][j] &lt;&lt; "\t|\t"; //вывод на экран новой марицы
            }
    
        cout &lt;&lt; endl;
        }
    }
    

    Here's her job: https://ideone.com/TrTBkq

    If it's categorical to use sort, be it:

    for (int i = 0; i < n; ++i) {
    //Вибираем четные элементы побочной диагонали матрицы
    if (matr[i][n - 1 - i] % 2 == 0) {
    for (int k = 0; k < n - 1; ++k) //Сортировка
    for (int j = 0; j < n - 1 - k; ++j) {
    if (matr[i][j + 1] < matr[i][j]) {
    int c = matr[i][j + 1];
    matr[i][j + 1] = matr[i][j];
    matr[i][j] = c;
    }
    }
    }
    }



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2