| | |
Ordering arrays?
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 adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






