Skip to main content
Search
Search This Blog
Livecodo
Pages
Home
Courses
Blog
About us
More…
Share
Get link
Facebook
X
Pinterest
Email
Other Apps
May 02, 2024
Write a c program to sort elements in row wise and print the elements of matrix in Column major order.
#include
int swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int selectionSort(int arr[], int n) { for (int i = 0; i < n- 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (minIndex != i) { swap(&arr[i], &arr[minIndex]); } } } int printMatrixColumnMajor(int matrix[][100], int m, int n) { for (int j = 0; j < n; j++) { for (int i = 0; i < m; i++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int main() { int m, n; printf("Enter the number of rows (m): "); scanf("%d", &m); printf("Enter the number of columns (n): "); scanf("%d", &n); int matrix[m][100]; printf("Enter the matrix elements:\n"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } for (int i = 0; i < m; i++) { selectionSort(matrix[i], n); } printf("Matrix in column-major order after sorting rows:\n"); printMatrixColumnMajor(matrix, m, n); return 0; }
Popular Posts
March 26, 2024
Dynamic Memory allocation in C
May 18, 2024
Searching and sorting Algorithm in C