| | |
Sorting an array of doubles?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
Hi.
The objective is to sort an array of doubles. My effort is listed below:
The output I'm seeing is as follows:
Thanks,
Molly
The objective is to sort an array of doubles. My effort is listed below:
C Syntax (Toggle Plain Text)
#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:
C Syntax (Toggle Plain Text)
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
[edit]But there are a number of indirection issues as well...
%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
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
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
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
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.
C Syntax (Toggle Plain Text)
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
•
•
•
•
uses double indirection? If so, can you explain? I assumed it translated to:C Syntax (Toggle Plain Text)
double* n[]
'n' is an array (unspecified length) of pointers to type double.
"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
•
•
•
•
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.
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
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; . .
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.
![]() |
Similar Threads
- First post, homework help.... (me != sorting array) (C++)
- help with sorting arrays from hightest to lowest (C++)
Other Threads in the C Forum
- Previous Thread: help me with this code
- Next Thread: How to understand this input/output code?
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






