Sunday, December 25, 2022

C Programming. Arranging columns of a matrix.

 Arranging columns of a matrix.


#include<stdio.h>

int main() {
    int r, c, t;
    printf("Enter the no. of rows and columns : ");
    scanf("%d%d",&r,&c);
    int mat[r][c];
    // Taking input from the user.
    for (int i=0; i<r; i++){
        for (int j=0; j<c; j++ ){
            printf("matrix[%d][%d] = ", i+1,j+1);
            scanf("%d",&mat[i][j]);
        }
    }

    for (int i=0; i<r; i++){
        for (int j=0; j<c; j++){
            for (int k=j+1; k<r; k++){
                if (mat[j][i]>mat[k][i]){
                    t=mat[j][i];
                    mat[j][i]=mat[k][i];
                    mat[k][i]=t;
                }
            }
        }
    }
    printf("After arranging the columns... \n");
    for (int i=0; i<r; i++){
        for (int j=0; j<c; j++){
            printf("%d ",mat[i][j]);
        }printf("\n");
    }
    return 0;
}

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...