Tuesday, December 23, 2014

Matrix is a Sparse Matrix in c lang



Determine if a given Matrix is a Sparse Matrix

 
#include 
#include 
void main ()
{
    static int array[10][10];
    int i, j, m, n;
    int counter = 0;
    clrscr();
    printf("Enter the order of the matix \n");
    scanf("%d %d", &m, &n);
    printf("Enter the co-efficients of the matix \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array[i][j]);
            if (array[i][j] == 0)
            {
                ++counter;
            }
        }
    }
    if (counter > ((m * n) / 2))
    {
        printf("The given matrix is sparse matrix \n");
    }
    else
        printf("The given matrix is not a sparse matrix \n");
    printf("There are %d number of zeros", counter);
getch();
}

Output
 
Enter the order of the matix
3 3
Enter the co-efficients of the matix
10 20 30
5 10 15
3 6 9
The given matrix is not a sparse matrix
There are 0 number of zeros
 
Enter the order of the matix
3 3
Enter the co-efficients of the matix
5 0 0
0 0 5
0 5 0
The given matrix is sparse matrix
There are 6 number of zeros
 

Sort Rows of the Matrix in Ascending & Columns in Descending Order

#include 
#include 
void main()
{
    static int array1[10][10], array2[10][10];
    int i, j, k, a, m, n;
    clrscr();
    printf("Enter the order of the matrix \n");
    scanf("%d %d", &m, &n);
    printf("Enter co-efficients of the matrix \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array1[i][j]);
            array2[i][j] = array1[i][j];
        }
    }
    printf("The given matrix is \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
                printf(" %d", array1[i][j]);
        }
        printf("\n");
    }
    printf("After 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(" %d", array1[i][j]);
        }
        printf("\n");
    }
    printf("After 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(" %d", array2[i][j]);
        }
        printf("\n");
    }
getch();
}
 
Output
 
Enter the order of the matrix
3 3
Enter co-efficients of the matrix
3 7 9
2 4 8
5 2 6
The given matrix is
 3 7 9
 2 4 8
 5 2 6
After arranging rows in ascending order
 3 7 9
 2 4 8
 2 5 6
After arranging the columns in descending order
 5 7 9
 3 4 8
 2 2 6

 

No comments:

Post a Comment