由小到大排序(含负数、小数)

来源:互联网 发布:摄像头监控软件 编辑:程序博客网 时间:2024/05/17 05:08
//输入5个数(含负数、小数)将它们按由小到大的顺序排列起来//需要排数的数字通过参数传递进来//例如:输入:./a.out -1  2.1  -3  5  7  输出:-3  -1  2.1  5  7#include <stdio.h>float sort(float b[5]);int main(){    int flag = -1;    int i;    float a[5];    printf("Please input 5 numbers:");    for(i = 0; i < 5; i++)    {        scanf("%f",&a[i]);    }    sort(a);    printf("The outcome is:");        for(i = 0;i < 5;i++)    {        printf("%g ",a[i]);    }    printf("\n");         return 0;}float sort(float b[]){    int i,j;    float temp = 0.0;    for(i = 0;i < 5;i++)    {        for(j = i + 1;j < 5;j++){    if(b[j] < b[i])    {        temp = b[i];b[i] = b[j];b[j] = temp;    }}    }}

0 0
原创粉丝点击