cfunctionsortingmatrixlexicographic

Arranging columns in a matrix lexicographically


I've been trying to sort columns in a matrix (the dimensions are m,n <= 10) via the lexicographical order (if the columns share the same element, then we compare the elements in the row beneath etc.) with some additional conditions. I need to use functions to print the matrix, input random integers up to 5 as its elements and finally arrange the matrix. I think I got the printing and random inputs correctly but I can't figure out the sorting. Plus I can't use global variables which I have no idea how to do, since I haven't been shown. An example matrix would be : enter image description here

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int m, n;
    int mat[10][10];

    void print_matrix()
    {
        int i, j;
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                printf("%d ", mat[i][j]);
            }
            printf("\n");
        }
    }

    void random_int()
    {
        int i, j;
        srand((unsigned)time(NULL));
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                mat[i][j] = rand() % 5;
            }
        }
    }

    void arrange()
    {
        int i, j, k, a;
        for (j = 0; j < n; ++j)
        {
            for (i = 0; i < m; ++i)
            {
                for (k = i + 1; k < m; ++k)
                {
                    if (mat[i][j] < mat[k][j])
                    {
                        a = mat[i][j];
                        mat[i][j] = mat[k][j];
                        mat[k][j] = a;
                    }
                }
            }
        }
    }


    printf("Input the number of rows : ");
    scanf("%d", &m);
    printf("Input the number of columns: ");
    scanf("%d", &n);

    random_int(mat[m][n]);
    print_matrix(mat[m][n]);
    arrange(mat[m][n]);
    print_matrix(mat[m][n]);

    return 0;
}

Solution

  • Try this solution(will work for input 0-8 only), also used global variables:

    There have multiple solutions. but is the easiest one. I have converted each of the columns as an integer value. then bubble sorted the integers. After sorting. I have then converted the integer value to digits. (You have to know how to convert individual digits to multiple digit integer and multiple digit integers to single-digit.

    Note I have added one(1) with each digit. Because the input can be zero(0). if you convert 0 0 2 1 to an integer will be only 21. the first two digits lost. So I have added 1. so 0 0 2 1 will be converted to 1132. I have done (added 1) for each input(deducted 1 after sorting). so it will not affect other inputs. Be careful input have to be from(0 to 8)

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int m, n;
    int mat[10][10];
    int generatedNumber[10];
    
    void print_matrix()
    {
    printf("The matrix is:\n");
    int i, j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
     }
    }
    
    void random_int()
    {
    int i, j;
    srand((unsigned)time(NULL));
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            mat[i][j] = rand() % 5;
        }
     }
    }
    
    void arrange()
    {
    int i, j, k, a;
    for (j = 0; j < n; ++j)
    {
        int number = 0;
        for (i = 0; i < m; ++i)
        {
            number = number * 10 + mat[i][j] + 1;///Adding one for avoiding zero(0)
    
        }
        generatedNumber[j] = number;
    }
    
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n-i-1; j++)
        {
            if( generatedNumber[j] > generatedNumber[j+1])
            {
                // swap the elements
                int temp = generatedNumber[j];
                generatedNumber[j] = generatedNumber[j+1];
                generatedNumber[j+1] = temp;
            }
        }
    }
    
    for(i = 0; i < n; i++)///columwise
    {
        int generatedColumnvalue = generatedNumber[i];
        for(j = m -1; j>= 0; j--)///row wise and fro last vaue to first
        {
    
            mat[j][i] = (generatedColumnvalue%10)-1;///removing extra added 1
            generatedColumnvalue/=10;
        }
      }
     }
    
     int main()
     {
    printf("Input the number of rows : ");
    scanf("%d", &m);
    printf("Input the number of columns: ");
    scanf("%d", &n);
    
    random_int();
    print_matrix();
    arrange();
    print_matrix();
    
    return 0;
    

    }