I have some 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; i < n; i++) {
for(j = 0; j < m; j++) {
fscanf(fp, "%d", &matrix[i][j]);
}
}
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
sum += multiply(matrix[j][i]);
}
columnSums[i] = sum;
sum = 0;
}
} else {
puts("Cannot read the file!");
}
puts("");
system("pause");
return 0;
}
int multiply(int thisNum) {
return thisNum * thisNum *thisNum;
}
My tasks wants me to read multidimensional array from a text file, and then sort each column by the addition of each column member multiplied by itself three times.
Wasn’t so hard to read an array and find each columns addition and store it into other array that stores the addition of each column (hoped that would help), but I’m stuck with sorting it out. Any tips, please? 🙂
You can use qsort(). Don’t forget to include stdlib.h