Question: I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning the values in the rows go from low to

I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning the values in the rows go from low to high). I tried to make the sort function but it just puts all the numbers from lowest to highest, up to down in column format. So please please help me fix it so it sorts correctly, I have bolded the code that needs to be fixed, everything else is fine. The code is below:

#include #define MAX 100

void sor(int a[], int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (a[i] < a[j]) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } } }

void sort1(int mat[MAX][MAX], int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp; } }

}

int main() {

int mat[MAX][MAX];

int i, j, m, n; int rowsum, columnsum, diagonalsum;

int k;

int magic = 0; int transpose[MAX][MAX];

printf("Enter the # of rows and columns of the square matrix (must be n x n): "); scanf("%d %d", &m, &n); if(m==n) { printf("Enter the elements of the matrix: "); for(i=0; i for(j=0; j scanf("%d", &mat[i][j]); } } printf("The matrix is: "); for(i=0; i for(j=0; j printf("%d\t", mat[i][j]); } printf(" "); }

for (i = 0; i < m; i++) for( j = 0 ; j < n ; j++ ) transpose[j][i] = mat[i][j];

printf("Transpose of the matrix: "); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) printf("%d\t", transpose[i][j]); printf(" "); }

// calculate diagonal sum diagonalsum = 0; for(i=0; i for(j=0; j if(i==j) { diagonalsum = diagonalsum + mat[i][j]; } } } // calculate row sum for(i=0; i rowsum = 0; for(j=0; j rowsum = rowsum + mat[i][j]; } if(rowsum != diagonalsum) { printf("The matrix is not magic square "); return; } } // calculate column sum for(i=0; i columnsum = 0; for(j=0; j columnsum = columnsum + mat[j][i]; } if(columnsum != diagonalsum) { printf("The matrix is not magic square "); return; } }

printf("The matrix is a magic square "); } else { printf("Please enter a square matrix, where m = n "); } int all_distinct = 1;

int count, l;

for (i = 0; i < m; i++) {

for (j = 0; j < n; j++) {

count = 0;

for (k = 0; k < m; k++) {

for (l = 0; l < n; l++) {

if (mat[k][l] == mat[i][j]) {

count++;

}

}

}

if (count != 1) {

all_distinct = 0;

}

}

}

if (all_distinct) {

printf("The square matrix is distinct ");

} else {

printf("The square matrix is not distinct ");

}

for (i = 0; i < n; i++) { sor(mat[i], n); } sort1(mat, n); for (i = 0; i < n; i++) { sor(mat[i], n); } sort1(mat, n); printf("The sorted matrix is: "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("%d\t", mat[i][j]); } printf(" "); }

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!