如何用C语言编译使a,b,c三个数按大小顺序输出?
1个回答
展开全部
冒泡排序多少个都能排,这个是由小到大排
# include <stdio.h>
# include <malloc.h>
//输入数组
void input(float * pArr, int len)
{
int i = 0;
for (i=0; i<len; ++i)
{
printf("请输入第%d个数: ", i+1);
scanf("%f", &pArr[i]);
}
return;
}
//排序 冒泡法
void sort (float * a, int len)
{
int i, j, t;
for (i=0; i<len-1; ++i)
{
for (j=0; j<len-1-i; ++j)
{
if (a[j]>a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
return;
}
int main (void)
{
float *p;
int m,len, i;
char ch;
do{
printf("请输入您要排序数字的个数: ");
scanf("%d", &len);
p = (float *)malloc(len*sizeof(float));//动态内存放p数组
input(p, len);
sort(p, len);
for (m=0; m<len; ++m)
printf("%f ", p[m]);
printf("\n");
free(p);
printf("请问是否继续?Y/N ");
scanf(" %c", &ch);
}while ('Y' == ch || 'y' == ch);
return 0;
}
# include <stdio.h>
# include <malloc.h>
//输入数组
void input(float * pArr, int len)
{
int i = 0;
for (i=0; i<len; ++i)
{
printf("请输入第%d个数: ", i+1);
scanf("%f", &pArr[i]);
}
return;
}
//排序 冒泡法
void sort (float * a, int len)
{
int i, j, t;
for (i=0; i<len-1; ++i)
{
for (j=0; j<len-1-i; ++j)
{
if (a[j]>a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
return;
}
int main (void)
{
float *p;
int m,len, i;
char ch;
do{
printf("请输入您要排序数字的个数: ");
scanf("%d", &len);
p = (float *)malloc(len*sizeof(float));//动态内存放p数组
input(p, len);
sort(p, len);
for (m=0; m<len; ++m)
printf("%f ", p[m]);
printf("\n");
free(p);
printf("请问是否继续?Y/N ");
scanf(" %c", &ch);
}while ('Y' == ch || 'y' == ch);
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询