Ordering arrays?

Thread Solved

Join Date: Dec 2007
Posts: 7
Reputation: chaosatom333 is an unknown quantity at this point 
Solved Threads: 0
chaosatom333 chaosatom333 is offline Offline
Newbie Poster

Ordering arrays?

 
0
  #1
Dec 6th, 2007
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
  1. for(fill = 0; fill < (m-1); ++fill)
  2. {
  3. if(atomic_num[index_of_min] <= atomic_num[fill])
  4. {
  5. temp = atomic_num[index_of_min];
  6. atomic_num[index_of_min] = atomic_num[fill];
  7. atomic_num[fill] = temp;
  8. }
  9. ++index_of_min;
  10. }
  11.  
  12.  
  13. for(j = 0; j < (m-1); j++)
  14. fprintf(outp,"%d\n", atomic_num[j]);
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,
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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Ordering arrays?

 
0
  #2
Dec 6th, 2007
You can using qsort() function which is come with <stdlib.h> header. Here the example of how to use this function:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int CompareFunction(const void *p1, const void *p2)
  5. {
  6. if (*(const int*)p1 > *(const int*)p2)
  7. return 1;
  8. else return -1;
  9. return 0;
  10. }
  11.  
  12. int main()
  13. {
  14. int i, a[5] = {5, 3, 2, 8, 1};
  15. qsort(&a, 5, sizeof(int), CompareFunction);
  16. for(i = 0; i < 5; i++)
  17. printf("%d ", a[i]);
  18. }
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Ordering arrays?

 
0
  #3
Dec 6th, 2007
Originally Posted by chaosatom333 View Post
  1. for(fill = 0; fill < (m-1); ++fill)
  2. {
  3. if(atomic_num[index_of_min] <= atomic_num[fill])
  4. {
  5. temp = atomic_num[index_of_min];
  6. atomic_num[index_of_min] = atomic_num[fill];
  7. atomic_num[fill] = temp;
  8. }
  9. ++index_of_min;
  10. }
  11.  
  12.  
  13. for(j = 0; j < (m-1); j++)
  14. fprintf(outp,"%d\n", atomic_num[j]);
It looks to me that you were trying to implement a bubble sort.
For that you need two index variables and two loops.
  1.  
  2. /* indexes for loops */
  3. int a, b;
  4.  
  5. /* How many elements atomic_num has? */
  6. int size = sizeof atomic_num / sizeof ( int );
  7.  
  8. /* temporal storage */
  9. int temp;
  10.  
  11.  
  12. for ( a = 0; a < size - 1; a++ )
  13. {
  14. for ( b = a + 1; b < size; b++ )
  15. {
  16. if ( atomic_num[a] > atomic_num[b] )
  17. {
  18. temp = atomic_num[a];
  19. atomic_num[a] = atomic_num[b];
  20. atomic_num[b] = temp;
  21. }
  22. }
  23. /* printf( "%d\n", atomic_num[a] ); uncomment for displaying */
  24. }
Last edited by Aia; Dec 6th, 2007 at 8:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: chaosatom333 is an unknown quantity at this point 
Solved Threads: 0
chaosatom333 chaosatom333 is offline Offline
Newbie Poster

Re: Ordering arrays?

 
0
  #4
Dec 7th, 2007
yeah it works, but I also need to printf the name of the element and the symbol along with atomic numbers. How would I do that? Would i use sturctures or parallel array and switch indexes or somethings? I don't know how I would go about it?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Ordering arrays?

 
0
  #5
Dec 7th, 2007
You could use a struct, or if the idea is too abstract for you, toy with the idea of parallel arrays, like you said.
Last edited by iamthwee; Dec 7th, 2007 at 3:19 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC