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 in ascending order and reverse the individual row elements of an mxn matrix
#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 reverseArray(int arr[], int n) { for (int i = 0; i < n / 2; i++) { swap(&arr[i], &arr[n- i- 1]); } } int sortAndReverseRows(int matrix[][100], int m, int n) { for (int i = 0; i < m; i++) { selectionSort(matrix[i], n); // Sort the row in ascending order reverseArray(matrix[i], n); // Reverse the sorted row elements } } int printMatrix(int matrix[][100], int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { 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]); } } sortAndReverseRows(matrix, m, n); printf("Modified matrix:\n"); printMatrix(matrix, m, n); return 0; }
Popular Posts
March 26, 2024
Dynamic Memory allocation in C
May 18, 2024
Searching and sorting Algorithm in C