| | |
Ordering arrays?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi, i am given an array of atomic numbers, and I need to arrange them in order from 1-100 or something.
the program i wrote is this
gives these the output below. It only orders 88 in the right order. How should I order all the elements in the right order.
the array of elements
1
1
1
50
84
88
1
1
1
1
1
1
11
20
84
ordering only 88.
1
1
1
50
84
1
1
1
1
1
1
11
20
84
88
how should i make them order 1,1, 1, 1, 11, 20, 84, 84, 88 something like this?
thanks,
the program i wrote is this
C Syntax (Toggle Plain Text)
for(fill = 0; fill < (m-1); ++fill) { if(atomic_num[index_of_min] <= atomic_num[fill]) { temp = atomic_num[index_of_min]; atomic_num[index_of_min] = atomic_num[fill]; atomic_num[fill] = temp; } ++index_of_min; } for(j = 0; j < (m-1); j++) fprintf(outp,"%d\n", atomic_num[j]);
the array of elements
1
1
1
50
84
88
1
1
1
1
1
1
11
20
84
ordering only 88.
1
1
1
50
84
1
1
1
1
1
1
11
20
84
88
how should i make them order 1,1, 1, 1, 11, 20, 84, 84, 88 something like this?
thanks,
Last edited by WaltP; Dec 6th, 2007 at 9:46 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
You can using qsort() function which is come with <stdlib.h> header. Here the example of how to use this function:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int CompareFunction(const void *p1, const void *p2) { if (*(const int*)p1 > *(const int*)p2) return 1; else return -1; return 0; } int main() { int i, a[5] = {5, 3, 2, 8, 1}; qsort(&a, 5, sizeof(int), CompareFunction); for(i = 0; i < 5; i++) printf("%d ", a[i]); }
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
•
•
C Syntax (Toggle Plain Text)
for(fill = 0; fill < (m-1); ++fill) { if(atomic_num[index_of_min] <= atomic_num[fill]) { temp = atomic_num[index_of_min]; atomic_num[index_of_min] = atomic_num[fill]; atomic_num[fill] = temp; } ++index_of_min; } for(j = 0; j < (m-1); j++) fprintf(outp,"%d\n", atomic_num[j]);
For that you need two index variables and two loops.
C Syntax (Toggle Plain Text)
/* indexes for loops */ int a, b; /* How many elements atomic_num has? */ int size = sizeof atomic_num / sizeof ( int ); /* temporal storage */ int temp; for ( a = 0; a < size - 1; a++ ) { for ( b = a + 1; b < size; b++ ) { if ( atomic_num[a] > atomic_num[b] ) { temp = atomic_num[a]; atomic_num[a] = atomic_num[b]; atomic_num[b] = temp; } } /* printf( "%d\n", atomic_num[a] ); uncomment for displaying */ }
Last edited by Aia; Dec 6th, 2007 at 8:10 pm.
![]() |
Similar Threads
- c++ cashier (C++)
- Ordering an array (Java)
- Sorting arrays of pointers with function? (C)
Other Threads in the C Forum
- Previous Thread: how do i label a linked list?
- Next Thread: basic tutorial in c programming
| Thread Tools | Search this Thread |
#include * append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string structures systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






