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 calculate the median of a matrix in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

#define ROWSCOLS 4

// Step 1: Flatten the matrix into a single array
double findMedianUnsortedMatrix(int matrix[][ROWSCOLS], int rows, int cols) {
    int total = rows * cols;
    int* elements = (int*)malloc(total * sizeof(int));
    int k = 0;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            elements[k++] = matrix[i][j];
        }
    }

    // Step 2: Sort the elements
    for (int i = 0; i < total - 1; i++) {
        for (int j = i + 1; j < total; j++) {
            if (elements[i] > elements[j]) {
                int temp = elements[i];
                elements[i] = elements[j];
                elements[j] = temp;
            }
        }
    }

    // Step 3: Calculate median
    double median;
    if (total % 2 == 1) {
        // Odd number of elements: return middle element
        median = elements[total / 2];
    } else {
        // Even number of elements: return average of two middle elements
        median = (elements[total / 2 - 1] + elements[total / 2]) / 2.0;
    }

    free(elements);
    
    return median;
}

int main() {
    int matrix[ROWSCOLS][ROWSCOLS] = {
        {5, 8, 9, 10},
        {1, 4, 6, 13},
        {7, 3, 0, 18},
        {6, 8, 9, 20},
    };

    double median = findMedianUnsortedMatrix(matrix, ROWSCOLS, ROWSCOLS);
    printf("Median of the matrix is: %.1f\n", median);

    return 0;
}


/*
run:

Median of the matrix is: 7.5

*/

 



answered Oct 5, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

#define ROWSCOLS 4

void sortMatrix(int matrix[][ROWSCOLS], int total, int* elements) {
    for (int i = 0; i < total - 1; i++) {
        for (int j = i + 1; j < total; j++) {
            if (elements[i] > elements[j]) {
                int temp = elements[i];
                elements[i] = elements[j];
                elements[j] = temp;
            }
        }
    }
}

// Step 1: Flatten the matrix into a single array
double findMedianUnsortedMatrix(int matrix[][ROWSCOLS], int rows, int cols) {
    int total = rows * cols;
    int* elements = (int*)malloc(total * sizeof(int));
    int k = 0;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            elements[k++] = matrix[i][j];
        }
    }

    // Step 2: Sort the elements
    sortMatrix(matrix, total, elements);

    // Step 3: Calculate median
    double median;
    if (total % 2 == 1) {
        // Odd number of elements: return middle element
        median = elements[total / 2];
    } else {
        // Even number of elements: return average of two middle elements
        median = (elements[total / 2 - 1] + elements[total / 2]) / 2.0;
    }

    free(elements);
    
    return median;
}

int main() {
    int matrix[ROWSCOLS][ROWSCOLS] = {
        {5, 8, 9, 10},
        {1, 4, 6, 13},
        {7, 3, 0, 18},
        {6, 8, 9, 20},
    };

    double median = findMedianUnsortedMatrix(matrix, ROWSCOLS, ROWSCOLS);
    printf("Median of the matrix is: %.1f\n", median);

    return 0;
}


/*
run:

Median of the matrix is: 7.5

*/


 



answered Oct 5, 2025 by avibootz
...