[問題] sort int and char*
[code]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 7
int main()
{
int i=0,j=0,k=0;
int b[7]={7,6,5,4,3,2,1};
char a[N][7]={"g","f","e","d","c","b","a"};
int temp_b=0;
char *temp_a=NULL;
printf("before sorted\n");
printf("a[] b[]\n");
printf("-----\n");
for(j=0;j<7;j++)
{
printf("%s %d\n",a[j],b[j]);
}
//sort
for(i=0;i<7;i++)
{
if(a[i]>a[i+1])
{
strcpy(temp_a,a[i+1]);
strcpy(a[i+1],a[i]);
strcpy(a[i],temp_a);
}
}
for(k=0;k<7;k++)
{
if(b[k]>b[k+1])
{
/*
b[i+1]=temp_b;
temp_b=b[i+1];
b[i+1]=b[i];
*/
temp_b=b[k+1];
b[k+1]=b[k];
b[k]=temp_b;
}
}
printf("----------------\n");
printf("after sorted\n");
printf("a[] b[]\n");
printf("-----\n");
for(i=0;i<7;i++)
printf("%s %d\n",a[i],b[i]);
system("PAUSE");
return 0;
}
[Output]
before sorted
a[] b[]
-----
g 7
f 6
e 5
d 4
c 3
b 2
a 1
----------------
after sorted
a[] b[]
-----
g 6
f 5
e 4
d 3
c 2
b 1
a 7
[Question]
why no sorted
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.72.142.198
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1430287226.A.2F5.html
→
04/29 14:07, , 1F
04/29 14:07, 1F
→
04/29 14:20, , 2F
04/29 14:20, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章