Sunday, July 12, 2009

C Programming?

I have a structure:


struct ranking{


char name[1000] [30];


char pos [1000] [2];


int PR[1000];


};





How do I sort the PR array in descending that has numbers stored into it, but also have the the correct name next to it?

C Programming?
Shane,





I think this question was answered for you already. Specifically, the answerer provided you with the structure that you needed. Is there something that you do not understand?
Reply:As the sorting function parses the structure array, it checks to see the _order_ of the numbers only, but it moves _both_ items (number and the name) during the move portion of the sort. That way, the name and numbers, etc, stay with each other.





while(not_sorted)


{


not_sorted=0;


if(ranking[cur].PR %26gt; ranking[next].PR)


{


swap(ranking[cur] , ranking[next]);


not_sorted=1;


}


cur++;


next++;


...


...


}





...where the swap function would move every asset of that index in the structure that you want swapped/moved. But note that only the integer value (PR) was evaluated...





For an easy to understand-and-write sorting algorythm, do a search on "bubble sort" on the net. Note that a bubble sort is slow, but good to start with when you're learning, cuz it's easy to code. Later, you can look for "exchange sort" -- generally the fastest of them all...





PS: I think you have the structure set up wrong, try more like this





struct tab_ranking{


char name[30];


char pos [2];


int PR;


}ranking[1000] ;





...see?


No comments:

Post a Comment