Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to sort each column of a matrix with strings in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h> // qsort
#include <string.h>

#define ROWS 3
#define COLS 3

// Function to compare strings for sorting
int compareStrings(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

// Function to sort each column of the matrix
void sortColumns(char *matrix[ROWS][COLS]) {
    for (int col = 0; col < COLS; ++col) {
        char *column[ROWS];

        // Extract column values
        for (int row = 0; row < ROWS; ++row) {
            column[row] = matrix[row][col];
        }

        // Sort column using qsort
        qsort(column, ROWS, sizeof(char *), compareStrings);

        // Place sorted values back into matrix
        for (int row = 0; row < ROWS; ++row) {
            matrix[row][col] = column[row];
        }
    }
}

// Function to print the matrix
void printMatrix(char *matrix[ROWS][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%s ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    char *matrix[ROWS][COLS] = {
        {"ccc", "zzzz", "x"},
        {"eee", "aaa", "ffff"},
        {"bbb", "gg", "yyyyyy"}
    };

    printf("Original Matrix:\n");
    printMatrix(matrix);

    sortColumns(matrix);

    printf("\nSorted Matrix:\n");
    printMatrix(matrix);

    return 0;
}



/*
run:

Original Matrix:
ccc zzzz x 
eee aaa ffff 
bbb gg yyyyyy 

Sorted Matrix:
bbb aaa ffff 
ccc gg x 
eee zzzz yyyyyy 

*/

 



answered Jun 1, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROWS 4
#define COLS 3

// Comparison function for qsort
int compareStrings(const void *a, const void *b) {
    return strcmp(*(const char **)a, *(const char **)b);
}

// Function to sort each column of the matrix
void sortColumns(char *matrix[ROWS][COLS]) {
    for (int col = 0; col < COLS; ++col) {
        char *column[ROWS];

        // Extract column values
        for (int row = 0; row < ROWS; ++row) {
            column[row] = matrix[row][col];
        }

        // Sort column using qsort
        qsort(column, ROWS, sizeof(char *), compareStrings);

        // Place sorted values back into matrix
        for (int row = 0; row < ROWS; ++row) {
            matrix[row][col] = column[row];
        }
    }
}

// Function to print the matrix
void printMatrix(char *matrix[ROWS][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%s ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    char *matrix[ROWS][COLS] = {
        {"ccc", "zzzz", "x"},
        {"eeee", "aaa", "ffff"},
        {"uu", "hhh", "uuu"},
        {"bbb", "gg", "yyyyyy"}
    };

    printf("Original Matrix:\n");
    printMatrix(matrix);

    sortColumns(matrix);

    printf("\nSorted Matrix:\n");
    printMatrix(matrix);

    return 0;
}



/*
run:

Original Matrix:
ccc zzzz x 
eeee aaa ffff 
uu hhh uuu 
bbb gg yyyyyy 

Sorted Matrix:
bbb aaa ffff 
ccc gg uuu 
eeee hhh x 
uu zzzz yyyyyy 

*/

 



answered Jun 1, 2025 by avibootz
...