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

Use %lf with scanf for type double .

[edit]But there are a number of indirection issues as well...

You have too much indirection.

#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( "%lf", &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] );
}

Thanks for the help Dave.

Looking at the initial exercise, it reads:

Write a program that uses pointers to type double variables to accept 10 numbers from the user, sort them, and print them to the screen.

double* n[]

uses double indirection? If so, can you explain? I assumed it translated to:

'n' is an array (unspecified length) of pointers to type double.

Thanks,

Molly

double* n[]

uses double indirection? If so, can you explain? I assumed it translated to:

'n' is an array (unspecified length) of pointers to type double.

Not in a function prototype -- there it is a pointer to a pointer to double. You may want to peruse the earlier c-faq.com link again.

Write a program that uses pointers to type double variables to accept 10 numbers from the user, sort them, and print them to the screen.

This is pretty tricky. The question says "pointers to type double variable", but it don't anywhere say "pointers to type double array. Which is what is this

double *n[];

That is totally wrong. That a pointer to a pointer. Well let me give you an example. With the above notation what you could do is this

double arr1[] = { 20.5, 3.6, 452.3, 6.2, 4.25 };
double arr2[] = { 40.5, 3.5, 42.3, 6.2006, 45.25 };
       
double *array[10];

array[0] = arr1;
array[1] = arr2;
.
.

As you can the each elements of array hold an starting address of an array of doubles. If you are trying to sort that sort of an array its completely a different approach. You got to do more than what sort methods you have followed. You should consult your professor to make sure what he is expecting.

Well, at least i understand that from the question. Please correct if am wrong.

ssharish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.