0

I am having trouble swapping rows inside a Matrix (nested array) according to the value of a specific column, I am making a console application that displays the results of a group stage in the football world cup so first I insert the results of the games to update the statistics of each team (rows) and generate the input array, but finally, I need to sort the rows according to the number of points of each team (the last position in each row). I need to create a function to generate the output array.

Input array:

inputArray[4][7] {

                  {0, 2, 1, 1, 3, -2, 2},
                  {1, 1, 1, 3, 3,  0, 4},
                  {2, 0, 1, 3, 1,  2, 6},
                  {1, 1, 1, 2, 2,  0, 4},
                }

Output array of the function:

outputArray[4][7] {

                  {2, 0, 1, 3, 1,  2, 6},
                  {1, 1, 1, 3, 3,  0, 4},
                  {1, 1, 1, 2, 2,  0, 4},
                  {0, 2, 1, 1, 3, -2, 2},
                  
                }
EduMurbol
  • 1
  • 1
  • It's quite trivial to write this kind of sort algorithm from scratch. I recommend to use `vector` and `std::sort` with lambda function – nnzzll Oct 13 '22 at 07:19
  • FYI: [Sort 2 dimensional c array with std::sort](https://stackoverflow.com/q/52347204/7478597) – Scheff's Cat Oct 13 '22 at 08:01
  • You should expose a [mcve] with your failed attempt. There are multiple ways to built up a matrix in C++: nested C arrays (as your code samples look like), nested `std::array`s, nested `std::vector`s, a matrix class with an internal 1d array or vector (which I would prefer), and maybe even more. How to instrument the `std::sort` with a resp. predicate according to your requirement depends on that data type. – Scheff's Cat Oct 13 '22 at 08:05

1 Answers1

0

The solution is straightforward. But my guess is that this is not what you want.

And, I think that the last value in the row is not the sum . . .

But let us first look at one potential solution:

#include <iostream>
#include <algorithm>
#include <array>

constexpr size_t NumberOfRows = 4u;
constexpr size_t NumberOfColumns = 7u;

using Columns = std::array<int, NumberOfColumns>;
using Array = std::array<Columns,NumberOfRows>;

int main() {
    Array array{{ {0, 2, 1, 1, 3, -2, 2},
                  {1, 1, 1, 3, 3,  0, 4},
                  {2, 0, 1, 3, 1,  2, 6},
                  {1, 1, 1, 2, 2,  0, 4} }};

    std::sort(std::begin(array), std::end(array), [](const Columns& c1, const Columns& c2) {return c1[6] < c2[6]; });

    for (const Columns& c : array) {
        for (const int i : c) std::cout << i << '\t';
        std::cout << '\n';
    }
}

If you want the array to be dynamic, then you may use a std::vector instead. You can then resize the number of rows and then number of columns.

#include <iostream>
#include <algorithm>
#include <vector>

constexpr size_t NumberOfRows = 4u;
constexpr size_t NumberOfColumns = 7u;

using Columns = std::vector<int>;
using Array = std::vector<Columns>;

int main() {
    Array array{  {0, 2, 1, 1, 3, -2, 2},
                  {1, 1, 1, 3, 3,  0, 4},
                  {2, 0, 1, 3, 1,  2, 6},
                  {1, 1, 1, 2, 2,  0, 4} };

    std::sort(std::begin(array), std::end(array), [](const Columns& c1, const Columns& c2) {return c1[6] < c2[6]; });

    for (const Columns& c : array) {
        for (const int i : c) std::cout << i << '\t';
        std::cout << '\n';
    }
}

But I still think that this is the wrong design. Becuase the last value in a row is the sum of other values. It is dependent, can be calculated, and there is no need to store ist.

See the following better design:

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>

struct Result {
    std::vector<int> values{};
    int sum() const { return std::accumulate(values.begin(), values.end(), 0); }

    friend std::ostream& operator << (std::ostream& os, const Result& r) {
        for (const int i : r.values) os << i << '\t';
        return os << "--> " << r.sum();;
    }
};
struct Series {
    std::vector<Result> results{};
    friend std::ostream& operator << (std::ostream& os, const Series& s) {
        for (const Result r : s.results) os << r << '\n';
        return os;
    }
};
int main() {
    Series series{{     
        {{0, 2, 1, 1, 3,-2}},
        {{1, 1, 1, 3, 3, 0}},
        {{2, 0, 1, 3, 1, 2}},
        {{ 1, 1, 1, 2, 2, 0}}
    }};
    std::sort(series.results.begin(), series.results.end(), [](const Result& r1, const Result& r2) {return r1.sum() < r2.sum(); });

    std::cout << series;
}

But you did not give enough information to give a good answer.

A M
  • 14,694
  • 5
  • 19
  • 44