Q:

C Program to Sort Rows in Ascending and Cols in Descending Order

0

Write a C Program to Sort Rows in Ascending and Cols in Descending Order. Here’s simple Program to Sort Rows in Ascending and Cols in Descending Order in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


Below is the source code for C Program to Sort Rows in Ascending and Cols in Descending Order which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* C Program to Sort Rows in Ascending and Cols in Descending Order  */

#include <stdio.h>

int main()
{
    static int array1[10][10], array2[10][10];
    int i, j, k, a, m, n;

    printf("Enter no. of rows :: ");
        scanf("%d", &m);
        printf("\nEnter no. of cols :: ");
        scanf("%d",&n);
        printf("\nEnter values to the matrix :: \n");
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                 printf("\nEnter a[%d][%d] value :: ",i,j);
                 scanf("%d", &array1[i][j]);
                 array2[i][j] = array1[i][j];
        }
    }
    printf("\nThe given matrix is \n\n");
        for (i = 0; i < m; ++i)
        {
        for (j = 0; j < n; ++j)
        {
            printf("\t%d", array1[i][j]);
        }
        printf("\n\n");
    }
    printf("\nAfter arranging rows in ascending order : \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            for (k =(j + 1); k < n; ++k)
            {
                if (array1[i][j] > array1[i][k])
                {
                    a = array1[i][j];
                    array1[i][j] = array1[i][k];
                    array1[i][k] = a;
                }
            }
        }
    }
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf("\t%d", array1[i][j]);
        }
        printf("\n\n");
    }
    printf("\nAfter arranging the columns in descending order : \n");
    for (j = 0; j < n; ++j)
    {
        for (i = 0; i < m; ++i)
        {
            for (k = i + 1; k < m; ++k)
            {
                if (array2[i][j] < array2[k][j])
                {
                    a = array2[i][j];
                    array2[i][j] = array2[k][j];
                    array2[k][j] = a;
                }
            }
        }
    }
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf("\t%d", array2[i][j]);
        }
        printf("\n\n");
    }

    return 0;
}

OUTPUT : :


/*  C Program to Sort Rows in Ascending and Cols in Descending Order in Matrix */

Enter no. of rows :: 3

Enter no. of cols :: 3

Enter values to the matrix ::

Enter a[0][0] value :: 6

Enter a[0][1] value :: 1

Enter a[0][2] value :: 3

Enter a[1][0] value :: 6

Enter a[1][1] value :: 2

Enter a[1][2] value :: 3

Enter a[2][0] value :: 0

Enter a[2][1] value :: 9

Enter a[2][2] value :: 4

The given matrix is

        6       1       3

        6       2       3

        0       9       4


After arranging rows in ascending order :
        1       3       6

        2       3       6

        0       4       9


After arranging the columns in descending order :
        6       9       4

        6       2       3

        0       1       3


Process returned 0

Above is the source code for C Program to Sort Rows in Ascending and Cols in Descending Order in Matrix which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Matrix Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Find Frequency of Odd and Even Number... >>
<< C Program to Swap rows and columns of matrix...