|
> その入れ替え方がわかんないです(>_<)
#include <stdio.h>
#include <string.h>
int main() {
char x[4][10]={ "りんご","みかん","ばなな","いちご"};
int data[4]={124,200,14,150};
int i, j, pos, max;
char t[10]; /* ついか */
for ( i = 0; i < 4; i++ ) {
printf("%10s,%5d\n",x[i],data[i]);
}
printf("\n");
for ( i = 0; i < (4-1); i++ ) {
pos = i ;
for ( j = i+1; j < 4; j++ ) {
if (data[j] > data[pos]) {
pos = j ;
}
}
max = data[pos] ;
data[pos] = data[i] ;
data[i] = max;
strcpy(t, x[pos]); /* ついでに */
strcpy(x[pos], x[i]); /* なまえも */
strcpy(x[i], t); /* いれかえ */
}
for ( i = 0; i < 4; i++ ) {
printf("%10s,%5d\n",x[i],data[i]);
}
}
|