Home > Mobile >  Rearranging the matrix column accordance to characteristics C
Rearranging the matrix column accordance to characteristics C

Time:05-20

My task sounds like: The characteristic of a column of an integer matrix is the sum of its negative odd elements. Rearranging the columns of a given matrix, arrange them in accordance with the growth of characteristics.

I am make two subarrays. In first which is called "nesort" i see not sorted characteristics of array . In other which is called "tab" i see sorted according my task. Then i comparing the subarrays by the elements and generate new one. But the trouble is when in generated on start array be a replay of number. The final array gonna be bigger than we need. Can you please help me and watch my code. Especially my loops. What's the problem?

There is example of my code :
enter code here 




#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
    setlocale(LC_ALL, "ru");
    cout << "Практическая работа по практике Казаков 9903:" << endl;

    const int ROWS = 5;
    const int COLS = 5;

    int arr[ROWS][COLS];
    int nesort[COLS];
    int tab[COLS];


    //Array generating
    srand(time(NULL));
    for (int i = 0; i < ROWS; i  )
    {
        for (int j = 0; j < COLS; j  )
        {
            arr[i][j] = rand() % 41 - 20;
        }
    }

    for (int i = 0; i < ROWS; i  )
    {
        for (int j = 0; j < COLS; j  )
        {
            cout << arr[i][j] << "\t";
        }

        cout << endl;
    }

    cout << "Массив построен!" << endl << "\n";

    //Counting and output of characteristics
    cout << "Суммы нечётных, отрицательных элементов столбцов: " << endl;


    for (int j = 0; j < COLS; j  )
    {
        int sum = 0;
        for (int i = 0; i < ROWS; i  )
        {
            if (arr[i][j] < 0 && arr[i][j] & 1)
            {
                sum = sum   arr[i][j];
            }

        }
        nesort[j] = sum;
        tab[j] = sum;
    }



    for (int s = 0; s < COLS; s  )
    {
        cout << nesort[s] << "\t";
        cout << endl;
    }

    cout << endl;

    //Sorting of columns characteristics
    cout << "Отсортированные характеристики столбцов: " << endl;

    int n = sizeof(tab) / sizeof(tab[0]);
    sort(tab, tab   n);


    for (int r = 0; r < COLS; r  )
    {
        cout << tab[r] << "\t";
        cout << endl;
    }

    cout << endl;

    cout << "Отсортированный по хар-кам массив: " << endl;

    //Sorting the start array by characteristics

    for (int n = 0; n < ROWS; n  )
    {
        for (int i = 0; i < ROWS; i  )
        {
            if (tab[n] == nesort[i])
            {
                for (int j = 0; j < COLS; j  )
                {
                    cout << arr[j][i] << "\n";
                }
                cout << endl;
            }
        }
    }
}

CodePudding user response:

You have a bug in the last loops. This can be easily be fixed:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main()
{
    setlocale(LC_ALL, "ru");
    cout << "Практическая работа по практике Казаков 9903:" << endl;

    const int ROWS = 5;
    const int COLS = 5;

    int arr[ROWS][COLS];
    int nesort[COLS];
    int tab[COLS];


    //Array generating
    srand(time(NULL));
    for (int i = 0; i < ROWS; i  )
    {
        for (int j = 0; j < COLS; j  )
        {
            arr[i][j] = rand() % 41 - 20;
        }
    }

    for (int i = 0; i < ROWS; i  )
    {
        for (int j = 0; j < COLS; j  )
        {
            cout << arr[i][j] << "\t";
        }

        cout << endl;
    }

    cout << "Массив построен!" << endl << "\n";

    //Counting and output of characteristics
    cout << "Суммы нечётных, отрицательных элементов столбцов: " << endl;


    for (int j = 0; j < COLS; j  )
    {
        int sum = 0;
        for (int i = 0; i < ROWS; i  )
        {
            if (arr[i][j] < 0 && arr[i][j] & 1)
            {
                sum = sum   arr[i][j];
            }

        }
        nesort[j] = sum;
        tab[j] = sum;
    }



    for (int s = 0; s < COLS; s  )
    {
        cout << nesort[s] << "\t";
        cout << endl;
    }

    cout << endl;

    //Sorting of columns characteristics
    cout << "Отсортированные характеристики столбцов: " << endl;

    int n = sizeof(tab) / sizeof(tab[0]);
    sort(tab, tab   n);


    for (int r = 0; r < COLS; r  )
    {
        cout << tab[r] << "\t";
        cout << endl;
    }

    cout << endl;

    cout << "Отсортированный по хар-кам массив: " << endl;

    //Sorting the start array by characteristics

    for (int sortedColSumIndex = 0; sortedColSumIndex < COLS;   sortedColSumIndex) {
        for (int colSumIndex = 0; colSumIndex < COLS;   colSumIndex) {
            if (tab[sortedColSumIndex] == nesort[colSumIndex]) {
                for (int row = 0; row < ROWS;   row) {
                    std::cout << arr[row][colSumIndex] << '\n';
                }
                std::cout << '\n';
            }
        }
    }
}

But this iwll not help the major design problem that the columns will apper multiple times, if the sums of the comums are double or multiple.

That cannot be healed that easily.

A change in design would be necessary. You need to sort accoring to the column index.

So, something like this:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>

using namespace std;


int main()
{
    setlocale(LC_ALL, "ru");
    cout << "Практическая работа по практике Казаков 9903:" << endl;

    const int ROWS = 5;
    const int COLS = 5;

    int arr[ROWS][COLS];
    int nesort[COLS];
    int tab[COLS];


    //Array generating
    srand(time(NULL));
    for (int i = 0; i < ROWS; i  )
    {
        for (int j = 0; j < COLS; j  )
        {
            arr[i][j] = rand() % 41 - 20;
        }
    }

    for (int i = 0; i < ROWS; i  )
    {
        for (int j = 0; j < COLS; j  )
        {
            cout << arr[i][j] << "\t";
        }
        cout << endl;
    }

    cout << "Массив построен!" << endl << "\n";

    //Counting and output of characteristics
    cout << "Суммы нечётных, отрицательных элементов столбцов: " << endl;


    for (int j = 0; j < COLS; j  )
    {
        int sum = 0;
        for (int i = 0; i < ROWS; i  )
        {
            if (arr[i][j] < 0 && arr[i][j] & 1)
            {
                sum = sum   arr[i][j];
            }

        }
        nesort[j] = sum;
        tab[j] = j;
    }

    for (int s = 0; s < COLS; s  )
    {
        cout << nesort[s] << "\t";
        cout << endl;
    }

    cout << endl;

    //Sorting of columns characteristics
    cout << "Отсортированные характеристики столбцов: " << endl;

    int n = sizeof(tab) / sizeof(tab[0]);
    sort(tab, tab   n, [&](const int i1, const int i2) {return nesort[i1] < nesort[i2]; });


    for (int r = 0; r < COLS; r  )
    {
        cout << tab[r] << "\t";
        cout << endl;
    }

    cout << endl;

    cout << "Отсортированный по хар-кам массив: " << endl;

    //Sorting the start array by characteristics
    for (int row = 0; row < ROWS;   row) {
        for (int sortedColSumIndex = 0; sortedColSumIndex < COLS;   sortedColSumIndex) {
            std::cout << arr[row][tab[sortedColSumIndex]] << '\t';
        }
        std::cout << '\n';
    }
}
  • Related