1
升序排行做得很好。但是當我輸入一個不同於一個的數字時,它應該調用降序,我認爲這是很明確的。然而我有一個隨機排序..降序無法正常工作c編程
#include<stdio.h>
void sort(int b[], int size, int (*upordown)(int a, int b));
void swap(int *elt1, int *elt2);
int ascending(int a, int b);
int descending(int a, int b);
main()
{
int size =8;
int order,a[size],i;
fprintf(stdout, "please enter a sequence of numbers you wish to sort:\n");
for(i =0; i<size; i++)
{
scanf("%d", &a[i]);
}
printf("\n please enter 1 for ascending order or 2 for descending order");
scanf("%d", &order);
if(order == 1)
{
sort(a, size, ascending);
}
else
{
sort(a, size, descending);
}
printf("Here is the new re arranged array: \n");
for(i =0; i<size; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
return 0;
}
void sort(int b[], int size, int (*upordown)(int a, int b))
{
int pass, j;
for(pass =0; pass< size;pass++)
{
for(j=0; j<size; j++)
{
if((*upordown)(b[j], b[j+1]))
{
swap(&b[j], &b[j+1]);
}
}
}
}
void swap(int *elt1, int *elt2)
{
int hold;
if(*elt1 > *elt2)
{
hold = *elt1;
*elt1 = *elt2;
*elt2 = hold;
}
}
int ascending(int a, int b)
{
return b<a;
}
int descending(int a, int b)
{
return b>a;
}
你有額外的> check in'swap',這可能是不必要的 – congusbongus 2013-02-12 06:06:45