Question

I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning...

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):\n");
scanf("%d %d", &m, &n);
if(m==n) {
printf("Enter the elements of the matrix: \n");
for(i=0; i for(j=0; j scanf("%d", &mat[i][j]);
}
}

printf("The matrix is:\n");
for(i=0; i for(j=0; j printf("%d\t", mat[i][j]);
}
printf("\n");
}


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

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

// 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\n");
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\n");
return;
}
}

printf("The matrix is a magic square\n");
} else {
printf("Please enter a square matrix, where m = n\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\n");

} else {

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

}


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:\n");
    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        printf("%d\t", mat[i][j]);
      }
      printf("\n");
    }

return 0;


}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>

#define MAX 100

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 < m; i++)

{

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

{

scanf("%d", &mat[i][j]);

}

}

printf("The matrix is: ");

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

{

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

{

printf("%d ", 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 ", transpose[i][j]);

printf(" ");

}

// calculate diagonal sum

diagonalsum = 0;

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

{

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

{

if (i == j)

{

diagonalsum = diagonalsum + mat[i][j];

}

}

}

// calculate row sum

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

{

rowsum = 0;

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

{

rowsum = rowsum + mat[i][j];

}

if (rowsum != diagonalsum)

{

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

}

}

// calculate column sum

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

{

columnsum = 0;

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

{

columnsum = columnsum + mat[j][i];

}

if (columnsum != diagonalsum)

{

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

}

}

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 (int i = 0; i < m; i++)

{

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

{

for (int k = j + 1; k < n; k++)

{

if (mat[i][j] > mat[i][k])

{

int t = mat[i][j];

mat[i][j] = mat[i][k];

mat[i][k] = t;

}

}

}

}

printf("The matrix after sorting row wise is: ");

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

{

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

{

printf("%d ", mat[i][j]);

}

printf(" ");

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
I'm having trouble correctly sorting my 2d array/matrix. I need it so its row-wise sorted (meaning...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 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...

  • #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i);...

    #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i); for(j=0;j<3;j++){ scanf("%f",&array[i][j]); } } printf("Given table data:\n"); for(i=0;i<5;i++){ for(j=0;j<3;j++){ printf(" %0.1f",array[i][j]); } printf("\n"); } for(i=0;i<5;i++){ for(j=0;j<3;j++){ rowsum=rowsum+array[i][j]; } printf("sum of the %d row is %0.1f\n",i,rowsum); rowsum=0; } for(j=0;j<3;j++){ colsum+=array[j][i]; printf("Sum of %d coloumn is %0.1f\n",j,colsum); colsum=0; } return(0); } can someone help me to get the correct row sum and column sum in c program

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle):...

    I'm having trouble getting my program to print shapes For example: Which shape (L-line, T-triangle, R-rectangle): L Enter an integer length between 1 and 25: 13 ************* Which shape (L-line, T-triangle, R-rectangle): t Enter an integer base length between 3 and 25: 5 * ** *** **** ***** Which shape (L-line, T-triangle, R-rectangle): r Enter an integer width and height between 2 and 25: 4 5 **** **** **** **** **** Here's my code: #include <stdio.h> #include <ctype.h> const int...

  • I'm having trouble getting my program to output this statement Enter a sentence: Test! There are...

    I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...

  • i'm having trouble making an else statement that will allow the program to add and output...

    i'm having trouble making an else statement that will allow the program to add and output the average the valid numbers inputed into the program. Here's my output: Enter Score 1:36 Enter Score 2:-1 Invalid Input Enter Score 2:89.5 Enter Score 3:42 Enter Score 4:66.3 Enter Score 5:93 You entered: 36.000000, 89.500000, 42.000000, 66.300000, 93.000000 Total Score: 362.800000 Average Score: 72.560000 Max Score: 93.000000 Min Score: 36.000000 Here's my code: #include <stdio.h> int main(void) { double score[5]; double min; double...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT