| | |
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 |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory fflush file fork forloop frequency givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o inches infiniteloop input interest iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number open opendocumentformat owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi






