943,808 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 967
  • C RSS
Dec 6th, 2007
0

Ordering arrays?

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chaosatom333 is offline Offline
7 posts
since Dec 2007
Dec 6th, 2007
0

Re: Ordering arrays?

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. }
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Dec 6th, 2007
0

Re: Ordering arrays?

  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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 7th, 2007
0

Re: Ordering arrays?

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chaosatom333 is offline Offline
7 posts
since Dec 2007
Dec 7th, 2007
0

Re: Ordering arrays?

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how do i label a linked list?
Next Thread in C Forum Timeline: basic tutorial in c programming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC