Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAX_SIZE_ROW 100
  4. #define MAX_SIZE_COLUMN MAX_SIZE_ROW * 4
  5.  
  6. void input(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columntCount) {
  7. printf("Input matrix elements\n");
  8. for (int i = 0; i < rowCount; i++) {
  9. for (int j = 0; j < columntCount; j++) {
  10. printf("Element [%d][%d]:", i, j);
  11. scanf("%d", &matrix[i][j]);
  12. printf("\n");
  13. }
  14. printf("\n");
  15. }
  16. }
  17.  
  18. void printMatrix(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columntCount) {
  19. for (int i = 0; i < rowCount; i++) {
  20. for (int j = 0; j < columntCount; j++) {
  21. printf("[%d][%d]=%d ", i, j, matrix[i][j]);
  22. }
  23.  
  24. printf("\n");
  25. }
  26. }
  27.  
  28. int getDigitsSum(int number) {
  29. int sum = 0;
  30. while (number != 0)
  31. {
  32. sum += number % 10;
  33. number /= 10;
  34. }
  35.  
  36. return sum;
  37. }
  38.  
  39. void sortColumn(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columnIndex) {
  40. for (int i = 0; i < rowCount - 1; i++) {
  41. for (int j = 0; j < rowCount - i - 1; j++) {
  42. int firstElementSumDigits = getDigitsSum(matrix[j][columnIndex]);
  43. int secondElementSumDigits = getDigitsSum(matrix[j + 1][columnIndex]);
  44. if (firstElementSumDigits > secondElementSumDigits) {
  45. int temp = matrix[j][columnIndex];
  46. matrix[j][columnIndex] = matrix[j + 1][columnIndex];
  47. matrix[j + 1][columnIndex] = temp;
  48. }
  49. }
  50. }
  51. }
  52.  
  53. void sortMatrixColumns(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columnCount) {
  54. for (int j = 0; j < columnCount; j++) {
  55. sortColumn(matrix, rowCount, j);
  56. }
  57. }
  58.  
  59. int main()
  60. {
  61. int matrix[MAX_SIZE_ROW][MAX_SIZE_COLUMN];
  62. int rowCount;
  63. int columnCount;
  64.  
  65. printf("Enter number of rows\n");
  66. scanf("%d", &rowCount);
  67. columnCount = rowCount * 4;
  68. printf("Number of rows: %d, number of columns: %d\n", rowCount, columnCount);
  69.  
  70. //Task 3 - Input data
  71. input(matrix, rowCount, columnCount);
  72.  
  73. //Task 4 - Print the input data
  74. printMatrix(matrix, rowCount, columnCount);
  75.  
  76. //Task 5 - Sort the elements in a column by the sum of their digits in ascending order
  77. sortMatrixColumns(matrix, rowCount, columnCount);
  78.  
  79. //Task 6 - Print processed data
  80. printMatrix(matrix, rowCount, columnCount);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement