| | |
Ranking Array Failure?
![]() |
•
•
Join Date: Sep 2007
Posts: 16
Reputation:
Solved Threads: 0
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!
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!
C Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { // Local Declarations int arr[5]={2,9,5,1,3}; int rank[5]={0,1,2,3,4}; int i; int j; int smallest; int temp; // Statements for(i=0;i<4;i++) { smallest = i; for(j=i+1;j<=4;j++) if(arr[rank[smallest]] > arr[rank[j]]) smallest = j; temp = rank[i]; rank[i] = smallest; rank[smallest] = temp; printf("\n"); for(i=0;i<5;i++) printf("%3d", rank[i]); printf("\n"); } for(i=0;i<5;i++) printf("%3d", arr[i]); printf("\n"); return 0; } // main /* Execution Results 3 1 2 0 4 2 9 5 1 3 */
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!
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?
![]() |
Other Threads in the C Forum
- Previous Thread: stack printing string problem
- Next Thread: Stable marriage
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






