Here's an example of the first - a simple bubble sort after the fact.
Note that for testing, I made the number of inputs to read as a constant (so you can just do 5 for testing, then up it to 12).
Also note I haven't done anything with the rounding - it will print the full double.
Code:
#include <stdio.h>
#define NUM 5
void printArray(double a[])
{
int x=0;
printf("\nYou entered:\n");
for (x=0; x<NUM; x++)
{
printf("%lf ", a[x]);
}
printf("\n");
}
void sortArray(double a[])
{
int i,j;
double tmp;
for (i=0; i<NUM; i++)
{
for (j=NUM-1; j>i; j--)
{
if (a[j] < a[j-1])
{
tmp = a[j];
a[j] = a[j-1];
a[j-1] = tmp;
}
}
}
}
int main(int argc, char *argv[])
{
double numbers[NUM];
double i;
int x;
printf("Please enter %d numbers.\n", NUM);
for (x=0; x<NUM; x++)
{
scanf("%lf", &i);
numbers[x] = i;
}
sortArray(numbers);
printArray(numbers);
return 0;
}
Sample output from Unix terminal:
Unix >> cc testNumbers.c
Unix >> a.out
Please enter 5 numbers.
3
5
6
0
-12
You entered:
-12.000000 0.000000 3.000000 5.000000 6.000000
Unix >> cc testNumbers.c
Unix >> a.out
Please enter 5 numbers.
34
36.5
-23.6
12
5
You entered:
-23.600000 5.000000 12.000000 34.000000 36.500000