sorting td arrays


PHOTO EMBED

Thu Jan 07 2021 06:42:04 GMT+0000 (UTC)

Saved by @mahmoud hussein #c++

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;


void sortRowWise(int m[][4],
    int r, int c)
{
    // loop for rows of matrix
    for (int i = 0; i < r; i++)
    {
        // loop for column of matrix
        for (int j = 0; j < c; j++)
        {
            // loop for comparison and swapping
            for (int k = 0; k < c - j - 1; k++)
            {
                if (m[i][k] > m[i][k + 1])
                {
                    // swapping of elements
                    swap(m[i][k], m[i][k + 1]);
                }
            }
        }
    }

    // printing the sorted matrix
    for (int i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
            cout << m[i][j] << " ";
        cout << endl;
    }
}

// Driver code
int main()
{
    int m[4][4];
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            cin >> m[i][j];
        }
    }
    int r = sizeof(m[0]) / sizeof(m[0][0]);
    int c = sizeof(m) / sizeof(m[0]);
    sortRowWise(m, r, c);
    return 0;
}
content_copyCOPY

http://cpp.sh/