fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int matrix[4][4] = {
  5. {16, 14, 10, 4},
  6. {13, 9, 3, 5},
  7. {8, 2, 6, 11},
  8. {1, 7, 12, 15}
  9. };
  10.  
  11. // Create an array to store the matrix elements
  12. int elements[16];
  13. int k = 0;
  14.  
  15. // Iterate through the matrix and store elements in the array
  16. for (int i = 0; i < 4; i++) {
  17. for (int j = 0; j < 4; j++) {
  18. elements[k] = matrix[i][j];
  19. k++;
  20. }
  21. }
  22.  
  23. // Sort the elements in ascending order
  24. for (int i = 0; i < 16; i++) {
  25. for (int j = i + 1; j < 16; j++) {
  26. if (elements[i] > elements[j]) {
  27. int temp = elements[i];
  28. elements[i] = elements[j];
  29. elements[j] = temp;
  30. }
  31. }
  32. }
  33.  
  34. // Print the sorted elements
  35. printf("Matrix elements in ascending order:\n");
  36. for (int i = 0; i < 16; i++) {
  37. printf("%d ", elements[i]);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Matrix elements in ascending order:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16