Sort multidimensional array columns in C -
i have piece of code in c:
#include <stdio.h> #include <stdlib.h> int multiply(int); int main() { file *fp; int i, j, n, m, matrix[99][99], sum = 0, columnsums[99] = {0}; fp = fopen("data.txt", "r"); if(fp != null) { fscanf(fp, "%d", &n); fscanf(fp, "%d", &m); for(i = 0; < n; i++) { for(j = 0; j < m; j++) { fscanf(fp, "%d", &matrix[i][j]); } } for(i = 0; < m; i++) { for(j = 0; j < n; j++) { sum += multiply(matrix[j][i]); } columnsums[i] = sum; sum = 0; } } else { puts("cannot read file!"); } puts(""); system("pause"); homecoming 0; } int multiply(int thisnum) { homecoming thisnum * thisnum *thisnum; }
my tasks wants me read multidimensional array text file, , sort each column add-on of each column fellow member multiplied 3 times. wasn't hard read array , find each columns add-on , store other array stores add-on of each column (hoped help), i'm stuck sorting out. tips, please? :)
you can utilize qsort(). don't forget include stdlib.h
#define arr_size(a) (sizeof(a) / sizeof(*(a))) int comp(const void* a, const void* b) { homecoming *(const int*)a - *(const int*)b; } ... (i = 0; < arr_size(matrix); i++) { qsort(matrix[i], arr_size(matrix[i]), sizeof matrix[i][0], comp); }
c arrays sorting multiple-columns
No comments:
Post a Comment