Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int compare(const void *p1, const void *p2) {
- return ((int *) p2)[4] - ((int *) p1)[4];
- }
- int main(void) {
- int score[5][5];
- int i, j;
- FILE *fp;
- fp = fopen("testdata.txt", "r");
- if (fp == NULL) {
- printf("ファイルがありません\n");
- return -1;
- }
- for (i = 0; i < 5; i++)
- for (j = 0; j < 4; j++)
- fscanf(fp, "%d", &score[i][j]);
- for (i = 0; i < 5; i++)
- score[i][4] = score[i][1] + score[i][2] + score[i][3];
- qsort(score, 5, sizeof(score[0]), compare);
- for (i = 0; i < 5; i++) {
- printf("%d ", score[i][0]);
- printf("%3d ", score[i][1]);
- printf("%3d ", score[i][2]);
- printf("%3d ", score[i][3]);
- printf("%4d", score[i][4]);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement