This blog is under construction

Saturday, 27 July 2013

C program to sort two dimensional array in descending order

Write a C program to sort a matrix in descending order.


  #include <stdio.h>
  #define ROW 10
  #define COL 10

  int main() {
        int mat[ROW][COL];
        int i, j, k, l, n, tmp;

        /* get the order of the matrix from the user */
        printf("Enter the order of the matrix:");
        scanf("%d", &n);

        /* get the matrix entries from the user */
        printf("Enter your entries for the input mat:\n");
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        scanf("%d", &mat[i][j]);
                }
        }

        /* sort the contents of the two dimensional array */
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        tmp = mat[i][j];
                        l = j + 1;
                        for (k = i; k < n; k++) {
                                while (l < n) {
                                        /* swapping the data */
                                        if (tmp < mat[k][l]) {
                                                tmp = mat[k][l];
                                                mat[k][l] = mat[i][j];
                                                mat[i][j] = tmp;
                                        }
                                        l++;
                                }
                                l = 0;
                        }
                }
        }

        /* print the result */
        printf("\n");
        printf("Resultant Matrix:\n");
        for (i = 0; i < n; i++) {
                for (j = 0; j < n; j++) {
                        printf("%d ", mat[i][j]);
                }
                printf("\n");
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the order of the matrix:3
  Enter your entries for the input mat:
  10 90 80
  70 20 60
  50 40 30

  Resultant Matrix:
  90 80 70 
  60 50 40 
  30 20 10 


5 comments:

  1. Correctly executed just made some changes in it to make it applicable to any array where number of rows and column can be different

    // Updated by Shrinivas
    #include
    #define ROW 10
    #define COL 10

    int main()
    {
    int mat[ROW][COL];
    int i, j, k, l, n, m, tmp;

    /* get the order of the matrix from the user */
    printf("Enter the rows of the matrix:");
    scanf("%d", &n);
    printf("Enter the column of the matrix:");
    scanf("%d", &m);

    /* get the matrix entries from the user */
    printf("Enter your entries for the input mat:\n");
    for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
    scanf("%d", &mat[i][j]);
    }
    }

    /* sort the contents of the two dimensional array */
    for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
    tmp = mat[i][j];
    l = j + 1;
    for (k = i; k < n; k++) {
    while (l < m) {
    /* swapping the data */
    if (tmp < mat[k][l]){
    tmp = mat[k][l];
    mat[k][l] = mat[i][j];
    mat[i][j] = tmp;
    }
    l++;
    }
    l = 0;
    }
    }
    }

    /* print the result */
    printf("\n");
    printf("Resultant Matrix:\n");
    for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
    printf("%d ", mat[i][j]);
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  2. how to be in ascending order?

    ReplyDelete
    Replies
    1. Change "if (tmp < mat[k][l])" to "if (tmp > mat[k][l])"

      Delete
  3. How to sort 2d array without buble sort

    ReplyDelete
  4. Very Nice Tutorial.Love the Content. Amazing Programiz Site

    ReplyDelete