前提・実現したいこと
ファイルから読み込んだデータを身長順にソートして表示したいです。
発生している問題・エラーメッセージ
washio 0.000000 0.0
0.000000 0.0
0.000000 0.0
0.000000 0.0
0.000000 0.0
0.000000 0.0
実行した結果、上記のようになります。
該当のソースコード
c
1#include<stdio.h> 2#include<string.h> 3typedef struct { 4 char name[100]; 5 int height; 6 float weight; 7}Student; 8void swap_Student(Student *x,Student *y){ 9 Student temp=*x; 10 *x=*y; 11 *y=temp; 12 } 13void sort_by_height(Student s[],int n){ 14 int i,j; 15 for(i=0;i<n-1;i++){ 16 for(j=n-1;j>i;j--){ 17 if(s[j-1].height>s[j].height){ 18 swap_Student(&s[j-1],&s[j]); 19 } 20 } 21 } 22} 23int main(void){ 24 FILE *fp; 25 Student s[256]; 26 int ninzu=0; 27 int i=0; 28 int j; 29 if((fp=fopen("hw.dat","r"))==NULL) 30 printf("ファイルをオープンできません。\n"); 31 else{ 32 while(fscanf(fp,"%s%lf%lf",s[i].name,&s[i].height,&s[i].weight)==3) 33 ninzu++; 34 } 35 sort_by_height(s,ninzu); 36 for(j=0;j<ninzu;j++){ 37 printf("%-8s %6f%6.1f\n",s[j].name,s[j].height,s[j].weight); 38 } 39 fclose(fp); 40 return 0; 41} 42```hw.dat
aiba 160 59.3
kurata 162 51.6
masaki 182 76.5
tanaka 170 60.7
tsuji 175 83.9
washio 175 72.5
### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。