Ranking Array Failure?

Reply

Join Date: Sep 2007
Posts: 16
Reputation: RaDeuX is an unknown quantity at this point 
Solved Threads: 0
RaDeuX RaDeuX is offline Offline
Newbie Poster

Ranking Array Failure?

 
0
  #1
May 10th, 2008
Well I tried to look for any posts on ranking arrays, but all I ever really got were stuff about Google's ranking system. Anyway, I have two arrays. One is arr[] which is the original array, and rank[] is the indexing array. So if I were to print arr[rank[0]], it would give me the smallest value of arr[]. Without further ado, here is the code!
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. // Local Declarations
  6. int arr[5]={2,9,5,1,3};
  7. int rank[5]={0,1,2,3,4};
  8. int i; int j;
  9. int smallest;
  10. int temp;
  11.  
  12. // Statements
  13.  
  14. for(i=0;i<4;i++)
  15. {
  16. smallest = i;
  17. for(j=i+1;j<=4;j++)
  18. if(arr[rank[smallest]] > arr[rank[j]])
  19. smallest = j;
  20.  
  21. temp = rank[i];
  22. rank[i] = smallest;
  23. rank[smallest] = temp;
  24. printf("\n");
  25. for(i=0;i<5;i++)
  26. printf("%3d", rank[i]);
  27. printf("\n");
  28. }
  29. for(i=0;i<5;i++)
  30. printf("%3d", arr[i]);
  31. printf("\n");
  32.  
  33. return 0;
  34. } // main
  35.  
  36. /* Execution Results
  37.  
  38.   3 1 2 0 4
  39.   2 9 5 1 3
  40. */

For some reason, the for loop for the ranking sort just goes through only once. Otherwise it would print the ranking array 3 more times I believe. Any help would be greatly appreciated. Thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Ranking Array Failure?

 
0
  #2
May 10th, 2008
Using more { } to denote exactly which bits of code belong to which block would certainly help clarify the problem.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Ranking Array Failure?

 
0
  #3
May 10th, 2008
very simple: you are using ' i ' in two nested loops and every time you enter a loop i becomes 0 (lines 14 and 25)
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 16
Reputation: RaDeuX is an unknown quantity at this point 
Solved Threads: 0
RaDeuX RaDeuX is offline Offline
Newbie Poster

Re: Ranking Array Failure?

 
0
  #4
May 10th, 2008
Okay, I fixed line 25 to loop with k instead of i. Now I'm getting 3 more arrays printed out, although not in the correct order.

3 1 2 0 4 //this one is correct

3 3 2 1 4 //but not this one... the second 3 should be a 0 instead...

3 3 4 1 2

3 3 4 4 1
2 9 5 1 3 //original array
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Ranking Array Failure?

 
0
  #5
May 10th, 2008
I have no idea what your program is supposed to do, so maybe you could clear this up?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 16
Reputation: RaDeuX is an unknown quantity at this point 
Solved Threads: 0
RaDeuX RaDeuX is offline Offline
Newbie Poster

Re: Ranking Array Failure?

 
0
  #6
May 10th, 2008
The array rank[] is supposed to be an indexing array that sorts the arr[] from low to high. For example, arr[rank[0]] would give me the lowest value, which in this case would be 1. arr[rank[1]] would give me the second lowest number which is 2.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Ranking Array Failure?

 
0
  #7
May 10th, 2008
Hmm, sounds a bit strange to me. How about using a simple bubblesort on the original array? That way arr[0] would be smallest and arr[4] will be biggest. No need for an extra array right?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 16
Reputation: RaDeuX is an unknown quantity at this point 
Solved Threads: 0
RaDeuX RaDeuX is offline Offline
Newbie Poster

Re: Ranking Array Failure?

 
0
  #8
May 10th, 2008
Okay, I got it. The reason why was because "smallest" in line 22 wasn't an array address. When I changed it to rank[smallest] it worked just fine. Thank you for your help guys, I really appreciate it! Now I can go to sleep...
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 16
Reputation: RaDeuX is an unknown quantity at this point 
Solved Threads: 0
RaDeuX RaDeuX is offline Offline
Newbie Poster

Re: Ranking Array Failure?

 
0
  #9
May 10th, 2008
Well that sort of defeats the purpose of a ranking sort. It's used so that the original array's order is not tampered with so it could still be used as a reference for something. Thanks for your input though.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC