Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c - 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("
");
        }
    }

    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;
}
question from:https://stackoverflow.com/questions/65934731/arranging-columns-in-a-matrix-lexicographically

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:
");
int i, j;
for (i = 0; i < m; i++)
{
    for (j = 0; j < n; j++)
    {
        printf("%d ", mat[i][j]);
    }
    printf("
");
 }
}

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;

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...