Advertisement
cd62131

Sort int[][]

Jul 11th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int compare(const void *p1, const void *p2) {
  4.   return ((int *) p2)[4] - ((int *) p1)[4];
  5. }
  6. int main(void) {
  7.   int score[5][5];
  8.   int i, j;
  9.   FILE *fp;
  10.   fp = fopen("testdata.txt", "r");
  11.   if (fp == NULL) {
  12.     printf("ファイルがありません\n");
  13.     return -1;
  14.   }
  15.   for (i = 0; i < 5; i++)
  16.     for (j = 0; j < 4; j++)
  17.       fscanf(fp, "%d", &score[i][j]);
  18.   for (i = 0; i < 5; i++)
  19.     score[i][4] = score[i][1] + score[i][2] + score[i][3];
  20.   qsort(score, 5, sizeof(score[0]), compare);
  21.   for (i = 0; i < 5; i++) {
  22.     printf("%d ", score[i][0]);
  23.     printf("%3d ", score[i][1]);
  24.     printf("%3d ", score[i][2]);
  25.     printf("%3d ", score[i][3]);
  26.     printf("%4d", score[i][4]);
  27.     printf("\n");
  28.   }
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement