Sorting an array of doubles?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 6
Reputation: java_girl is an unknown quantity at this point 
Solved Threads: 0
java_girl java_girl is offline Offline
Newbie Poster

Sorting an array of doubles?

 
0
  #1
Jul 9th, 2008
Hi.

The objective is to sort an array of doubles. My effort is listed below:

  1. #include <stdio.h>
  2. #define MAX 10
  3.  
  4. void sort( double* n[], int q );
  5. void print_nums( double* n[], int q );
  6.  
  7. int main( void ) {
  8.  
  9. double* nums[MAX];
  10. int i;
  11.  
  12. for( i = 0; i < MAX; i++ ) {
  13. printf( "Please enter a double variable: " );
  14. scanf( "%f", &nums[i] );
  15. }
  16.  
  17. sort( nums, 10 );
  18. print_nums( nums, 10 );
  19.  
  20. return 0;
  21. }
  22.  
  23. void sort( double* n[], int q ) {
  24.  
  25. int a, b;
  26.  
  27. double* x;
  28.  
  29. for( a = 1; a < q; a++ ) {
  30. for( b = 0; b < q - 1; b++ ) {
  31. if( n[b] > n[b + 1] ) {
  32. x = n[ b ];
  33. n[b] = n[b + 1];
  34. n[b + 1] = x;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. void print_nums( double* n[], int q ) {
  41.  
  42. int count;
  43.  
  44. for( count = 0; count < q; count++ )
  45. printf( "\n%f", n[count] );
  46. }

The output I'm seeing is as follows:

  1. Please enter a double variable: 2.2
  2. Please enter a double variable: 2.2
  3. Please enter a double variable: 2.1
  4. Please enter a double variable: 2.12
  5. Please enter a double variable: 333.2
  6. Please enter a double variable: 332.22
  7. Please enter a double variable: 1.1
  8. Please enter a double variable: 2.21
  9. Please enter a double variable: 2.122
  10. Please enter a double variable: 2.122
  11.  
  12. 2.800000
  13. 2.800000
  14. 2.800000
  15. 2.800000
  16. 2.800000
  17. 2.800000
  18. 2.800000
  19. 2.800000
  20. 2.800000
  21. 2.800000

Thanks,

Molly
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,413
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting an array of doubles?

 
0
  #2
Jul 9th, 2008
Use %lf with scanf for type double .

[edit]But there are a number of indirection issues as well...
Last edited by Dave Sinkula; Jul 9th, 2008 at 10:14 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,413
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting an array of doubles?

 
0
  #3
Jul 9th, 2008
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] );
}
Last edited by Dave Sinkula; Jul 9th, 2008 at 10:22 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: java_girl is an unknown quantity at this point 
Solved Threads: 0
java_girl java_girl is offline Offline
Newbie Poster

Re: Sorting an array of doubles?

 
0
  #4
Jul 10th, 2008
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.

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,413
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 248
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting an array of doubles?

 
0
  #5
Jul 10th, 2008
Originally Posted by java_girl View Post
  1. 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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Sorting an array of doubles?

 
0
  #6
Jul 10th, 2008
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

  1. 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

  1. double arr1[] = { 20.5, 3.6, 452.3, 6.2, 4.25 };
  2. double arr2[] = { 40.5, 3.5, 42.3, 6.2006, 45.25 };
  3.  
  4. double *array[10];
  5.  
  6. array[0] = arr1;
  7. array[1] = arr2;
  8. .
  9. .
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.
Last edited by ssharish2005; Jul 10th, 2008 at 12:51 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC