Home > front end >  Sorting a matrix in descending order in C
Sorting a matrix in descending order in C

Time:12-21

I need to input a matrix, where the first element of a row is the student ID number, and the other 12 elements are student points and GPA. Each row represents one student. What the program needs to do is calculate the total point amount, and then sort the matrix so it goes in descending order by the amount of points.

This is my code:

#include <stdio.h>

int main() {
    int number_of_students,i,j;
    double matrix[50][50],GPA,grades,total,GPA_before;

    printf("Number of students: ");
    scanf("%d", &number_of_students);

    printf("Input students: ");
    for(i=0; i<number_of_students; i  ){
        for(j=0; j<12; j  ){
            scanf("%lf", &matrix[i][j]);
        }
    }

    printf("Final list: \n");
    for(i=0; i<number_of_students; i  ){
        GPA_before=(matrix[i][2] matrix[i][3] matrix[i][4] matrix[i][5])*2.5;
        if(GPA_before==50){
            GPA=GPA_before 3;
        }
        else{
            GPA=GPA_before;
        }
        grades=(matrix[i][6] matrix[i][7] matrix[i][8] matrix[i][9] matrix[i][10] matrix[i][11])*0.67;
        total=GPA grades matrix[i][12];

        printf("%d. %g | %g\n",i,matrix[i][1],total);
    }
    
    return 0;
}

And this is what I get:

Number of students: 4
Input students: 123456 4.00 4.50 5.00 5.00 5 5 5 4 4 5 15
456789 4.50 5.00 4.00 3.50 3 5 4 5 5 2 10
789321 5.00 5.00 4.50 3.00 5 4 5 3 5 3 15
456987 4.50 4.50 4.50 5.00 4 4 4 5 5 5 25
Final list: 
1. 123456 | 80.01
2. 456789 | 68.58
3. 789321 | 75.5
4. 456987 | 89.34

The matrix should be sorted from highest number of points to the lowest, not by ID. It is specified not to sort the list while printing it out, the matrix itself must be sorted. I would really appreciate the help.

CodePudding user response:

Firstly, I saw a few things in your code that I would like to point out and change, if possible.

  1. You are allocating an 2-D Array of 50 rows and 50 columns. However, you're only utilizing 12 of those columns. So, first thing that I would like you to fix is the use of those extra unnecessary array indexes. So, I wrote the following code:
const int MAX = 50;
const int NUM_INPUTS = 12;
int number_of_students, i, j;
double matrix[MAX][NUM_INPUTS   1], GPA, grades, total;

Note: I added NUM_INPUTS 1, that we will use to store the total of that you were calculating in the loop and also printing it too.

Now, another thing I would like to point out, is your use of loops. You're iteration is beginning from the index 1. You're not using the 0th index. So, you'll have to fix each loop from this:

for(i=1; i<=number_of_students; i  ){
    for(j=1; j<=12; j  ){
        scanf("%lf", &matrix[i][j]);
    }
}

to this:

for (i = 0; i < number_of_students; i  ) {
    for (j = 0; j < NUM_INPUTS; j  ) {
        scanf("%lf", &matrix[i][j]);
    }
}

Now, this indexing from 0 will cause the code that you wrote for the calculations, to not work properly, so, you will have to fix that too:

for(i=1; i<=number_of_students; i  ){
    GPA_before=(matrix[i][2] matrix[i][3] matrix[i][4] matrix[i][5])*2.5;
    if(GPA_before==50){
        GPA=GPA_before 3;
    }
    else{
        GPA=GPA_before;
    }
    grades=(matrix[i][6] matrix[i][7] matrix[i][8] matrix[i][9] matrix[i][10] matrix[i][11])*0.67;
    total=GPA grades matrix[i][12];

You were using this code in-junction with printf. I used this code to calculate the total and then store it in the index matrix[i][NUM_INPUTS]. Now, I slightly changed your code and loop for storing the total in the matrix itself becomes:

for (i = 0; i < number_of_students; i  ) {
    GPA = (matrix[i][1]   matrix[i][2]   matrix[i][3]   matrix[i][4]) * 2.5;
    if (GPA == 50) {
        GPA  = 3;
    }
    grades = (matrix[i][5]   matrix[i][6]   matrix[i][7]   matrix[i][8]   matrix[i][9]   matrix[i][10]) * 0.67;
    total = GPA   grades   matrix[i][11];

    matrix[i][NUM_INPUTS] = total;
}

Now, this made it easy for me to implement a basic Bubble Sort algorithm to sort your matrix:

int k;
for (i = 0; i < number_of_students; i  ) {
    for (j = 0; j < number_of_students - 1; j  ) {
        if (matrix[j][NUM_INPUTS] > matrix[j   1][NUM_INPUTS]) {
            double temp;
            for (k = 0; k < NUM_INPUTS   1; k  ) {
                temp = matrix[j][k];
                matrix[j][k] = matrix[j   1][k];
                matrix[j   1][k] = temp;
            }
        }
    }
}

Now, since the matrix itself has been sorted, we will simply print the final matrix:

for (i = 0; i < number_of_students; i  ) {
    printf("%d. %g | %g\n", i   1, matrix[i][0], matrix[i][NUM_INPUTS]);
}

Note: One more thing you can do (totally for input validation) is write an if statement, that would check if the user input for number of students is in the range of 1 <= number_of_students <= MAX (50), if it is greater than that, it would cause an out-of-bounds exception or a Buffer Overflow.

if(number_of_students > MAX || number_of_students <= 0) {
    printf("Invalid number of students");
    return 1;
}

I hope you understood what I wrote and would love if you'd upvote as this is my first answer here.

Here's the full source code:

#include <stdio.h>

int main() {

    const int MAX = 50;
    const int NUM_INPUTS = 12;
    int number_of_students, i, j;
    double matrix[MAX][NUM_INPUTS   1], GPA, grades, total;

    printf("Number of students: ");
    scanf("%d", &number_of_students);

    if(number_of_students > MAX || number_of_students <= 0) {
        printf("Invalid number of students");
        return 1;
    }

    printf("Input students: ");
    for (i = 0; i < number_of_students; i  ) {
        for (j = 0; j < NUM_INPUTS; j  ) {
            scanf("%lf", &matrix[i][j]);
        }
    }

    // Firstly calculating the total and storing it in the final index of the matrix:
    for (i = 0; i < number_of_students; i  ) {
        GPA = (matrix[i][1]   matrix[i][2]   matrix[i][3]   matrix[i][4]) * 2.5;
        if (GPA == 50) {
            GPA  = 3;
        }
        grades = (matrix[i][5]   matrix[i][6]   matrix[i][7]   matrix[i][8]   matrix[i][9]   matrix[i][10]) * 0.67;
        total = GPA   grades   matrix[i][11];

        matrix[i][NUM_INPUTS] = total;
    }

    // Sorting this based on total:
    for (i = 0; i < number_of_students; i  ) {
        for (j = 0; j < number_of_students - 1; j  ) {
            if (matrix[j][NUM_INPUTS] > matrix[j   1][NUM_INPUTS]) {
                double temp;
                for (int k = 0; k < NUM_INPUTS   1; k  ) {
                    temp = matrix[j][k];
                    matrix[j][k] = matrix[j   1][k];
                    matrix[j   1][k] = temp;
                }
            }
        }
    }


    printf("Final list:\n");
    for (i = 0; i < number_of_students; i  ) {
        printf("%d. %g | %g\n", i   1, matrix[i][0], matrix[i][NUM_INPUTS]);
    }

    return 0;
}
  • Related