Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions