943,892 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2858
  • C RSS
Jul 9th, 2008
0

Sorting an array of doubles?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
java_girl is offline Offline
6 posts
since Jul 2008
Jul 9th, 2008
0

Re: Sorting an array of doubles?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 9th, 2008
0

Re: Sorting an array of doubles?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 10th, 2008
0

Re: Sorting an array of doubles?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
java_girl is offline Offline
6 posts
since Jul 2008
Jul 10th, 2008
0

Re: Sorting an array of doubles?

Click to Expand / Collapse  Quote originally posted by java_girl ...
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 10th, 2008
0

Re: Sorting an array of doubles?

Quote ...
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.
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help me with this code
Next Thread in C Forum Timeline: How to understand this input/output code?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC