Hi.
The objective is to sort an array of doubles. My effort is listed below:
#include <stdio.h>
#define MAX 10
void sort( double* n[], int q );
void print_nums( double* n[], int q );
int main( void ) {
double* nums[MAX];
int i;
for( i = 0; i < MAX; i++ ) {
printf( "Please enter a double variable: " );
scanf( "%f", &nums[i] );
}
sort( nums, 10 );
print_nums( nums, 10 );
return 0;
}
void sort( double* n[], int q ) {
int a, b;
double* x;
for( a = 1; a < q; a++ ) {
for( b = 0; b < q - 1; b++ ) {
if( n[b] > n[b + 1] ) {
x = n[ b ];
n[b] = n[b + 1];
n[b + 1] = x;
}
}
}
}
void print_nums( double* n[], int q ) {
int count;
for( count = 0; count < q; count++ )
printf( "\n%f", n[count] );
}
The output I'm seeing is as follows:
Please enter a double variable: 2.2
Please enter a double variable: 2.2
Please enter a double variable: 2.1
Please enter a double variable: 2.12
Please enter a double variable: 333.2
Please enter a double variable: 332.22
Please enter a double variable: 1.1
Please enter a double variable: 2.21
Please enter a double variable: 2.122
Please enter a double variable: 2.122
2.800000
2.800000
2.800000
2.800000
2.800000
2.800000
2.800000
2.800000
2.800000
2.800000
Thanks,
Molly