Last active
December 10, 2015 17:58
-
-
Save ArnonEilat/4471201 to your computer and use it in GitHub Desktop.
Sort of a column in two dimensional array.
Usage example included.
I will add documentation someday.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define COLUMNS_NO 3 | |
void sort_column(char matrix[][COLUMNS_NO], int lines_no, int col) { | |
int i, j; | |
char temp; | |
for (i = 0; i < lines_no - 1; i++) { | |
for (j = 0; j < lines_no - 1 - i; j++) { | |
if (matrix[j][col] > matrix[j + 1][col]) { | |
temp = matrix[j][col]; | |
matrix[j][col] = matrix[j + 1][col]; | |
matrix[j + 1][col] = temp; | |
} | |
} | |
} | |
} | |
int main() { | |
char matrix[4][COLUMNS_NO] = { | |
{'f', 'j', 'h'}, | |
{'w', 'g', 'k'}, | |
{'o', 'c', 'l'}, | |
{'d', 'a', 'i'} | |
}; | |
int i, j; | |
printf("Before Sort:\n"); | |
for (i = 0; i < 4; i++) { | |
for (j = 0; j < 3; j++) { | |
printf("%3c", matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
sort_column(matrix, 4, 1); | |
printf("After Sort:\n"); | |
for (i = 0; i < 4; i++) { | |
for (j = 0; j < 3; j++) { | |
printf("%3c", matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment