{"id":2238,"date":"2016-12-08T00:01:13","date_gmt":"2016-12-08T08:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2238"},"modified":"2016-12-03T09:32:41","modified_gmt":"2016-12-03T17:32:41","slug":"ranking-scores-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2238","title":{"rendered":"Ranking Scores &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2223\">Exercise<\/a> is to create an array of six players, assign each a random score (1 to 100), then display the player&#8217;s scores by rank. The first problem you most likely encountered was how to keep the player&#8217;s number and score together.<br \/>\n<!--more--><br \/>\nFor my solution, I chose to create an array of structures as opposed to an array of integers. That way, the player number and their score are bound as members within a structure. This solution became obvious because the alternative is to create an integer array. The problem with that solution is associating the array element offset with the score. Rather than mess with all that logic, a structure like this works better:<\/p>\n<pre class=\"screen\">\r\nstruct p {\r\n    int number;\r\n    int score;\r\n};<\/pre>\n<p>After creating array <code>players[]<\/code> of structure <code>p<\/code>, I use a <em>for<\/em> loop to fill each structure with the player number and a random score, 1 to 100.<\/p>\n<p>The final part of the code sorts the structure based on the <code>player[].score<\/code> member. I used a simple bubble sort. And the sort swap works with structure variables directly, so I didn&#8217;t need to mess with pointers.<\/p>\n<p>Here is the code for my solution:<\/p>\n<pre class=\"screen\"\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define PLAYERS 6\r\n\r\nint main()\r\n{\r\n    struct p {\r\n        int number;\r\n        int score;\r\n    };\r\n    struct p player[PLAYERS];\r\n    struct p temp;\r\n    int x,y;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL));\r\n\r\n    <span class=\"comments\">\/* assign each player a number and score 1 to 100 *\/<\/span>\r\n    for(x=0;x&lt;PLAYERS;x++)\r\n    {\r\n        player[x].number = x + 1;\r\n        player[x].score = rand() % 100 + 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* sort the scores *\/<\/span>\r\n    for(x=0;x&lt;PLAYERS;x++)\r\n        for(y=x+1;y&lt;PLAYERS;y++)\r\n            if(player[x].score < player[y].score)\r\n            {\r\n                temp = player[x];\r\n                player[x] = player[y];\r\n                player[y] = temp;\r\n            }\r\n\r\n    <span class=\"comments\">\/* display the results *\/<\/span>\r\n    puts(\"Round Results:\");\r\n    for(x=0;x&lt;PLAYERS;x++)\r\n        printf(\"#%d Player %d, score %d\\n\",\r\n                x+1,\r\n                player[x].number,\r\n                player[x].score);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The output looks like this:<\/p>\n<pre><code>Round Results:\r\n#1 Player 2, score 95\r\n#2 Player 6, score 63\r\n#3 Player 5, score 60\r\n#4 Player 4, score 55\r\n#5 Player 3, score 36\r\n#6 Player 1, score 16<\/code><\/pre>\n<p>If you want to get fancy, you could change structure <code>p<\/code> member <code>number<\/code> to a string and assign names to the players. I indulge you to attempt this modification on your own. As a hint: Use pointers. You can <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/12exercise-strings.c\">click here<\/a> to view my solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is to create an array of six players, assign each a random score (1 to 100), then display the player&#8217;s scores by rank. The first problem you most likely encountered was how to keep the player&#8217;s number &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2238\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2238","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2238"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2238\/revisions"}],"predecessor-version":[{"id":2247,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2238\/revisions\/2247"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}