Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAX_SIZE_ROW 100
- #define MAX_SIZE_COLUMN MAX_SIZE_ROW * 4
- void input(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columntCount) {
- printf("Input matrix elements\n");
- for (int i = 0; i < rowCount; i++) {
- for (int j = 0; j < columntCount; j++) {
- printf("Element [%d][%d]:", i, j);
- scanf("%d", &matrix[i][j]);
- printf("\n");
- }
- printf("\n");
- }
- }
- void printMatrix(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columntCount) {
- for (int i = 0; i < rowCount; i++) {
- for (int j = 0; j < columntCount; j++) {
- printf("[%d][%d]=%d ", i, j, matrix[i][j]);
- }
- printf("\n");
- }
- }
- int getDigitsSum(int number) {
- int sum = 0;
- while (number != 0)
- {
- sum += number % 10;
- number /= 10;
- }
- return sum;
- }
- void sortColumn(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columnIndex) {
- for (int i = 0; i < rowCount - 1; i++) {
- for (int j = 0; j < rowCount - i - 1; j++) {
- int firstElementSumDigits = getDigitsSum(matrix[j][columnIndex]);
- int secondElementSumDigits = getDigitsSum(matrix[j + 1][columnIndex]);
- if (firstElementSumDigits > secondElementSumDigits) {
- int temp = matrix[j][columnIndex];
- matrix[j][columnIndex] = matrix[j + 1][columnIndex];
- matrix[j + 1][columnIndex] = temp;
- }
- }
- }
- }
- void sortMatrixColumns(int matrix[][MAX_SIZE_COLUMN], int rowCount, int columnCount) {
- for (int j = 0; j < columnCount; j++) {
- sortColumn(matrix, rowCount, j);
- }
- }
- int main()
- {
- int matrix[MAX_SIZE_ROW][MAX_SIZE_COLUMN];
- int rowCount;
- int columnCount;
- printf("Enter number of rows\n");
- scanf("%d", &rowCount);
- columnCount = rowCount * 4;
- printf("Number of rows: %d, number of columns: %d\n", rowCount, columnCount);
- //Task 3 - Input data
- input(matrix, rowCount, columnCount);
- //Task 4 - Print the input data
- printMatrix(matrix, rowCount, columnCount);
- //Task 5 - Sort the elements in a column by the sum of their digits in ascending order
- sortMatrixColumns(matrix, rowCount, columnCount);
- //Task 6 - Print processed data
- printMatrix(matrix, rowCount, columnCount);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement