{"id":3266,"date":"2018-09-08T00:01:57","date_gmt":"2018-09-08T07:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3266"},"modified":"2018-09-01T14:15:30","modified_gmt":"2018-09-01T21:15:30","slug":"shuffle-that-playlist-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3266","title":{"rendered":"Shuffle That Playlist &#8211; Solution"},"content":{"rendered":"<p>The solution to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3254\">this month&#8217;s Exercise<\/a> is similar to code I&#8217;ve presented in my books and in this blog with regards to randomly drawing from a fixed set of elements. Yet it has an extra level of complexity.<br \/>\n<!--more--><br \/>\nIn those examples, the goal is to not draw the same lotto ball or card from a deck twice. You can&#8217;t do that in real life, so you must replicate this restriction in your code. To do so, you create an array or other method that monitories which items can no longer be drawn.<\/p>\n<p>For example, with a deck of cards you create a 52-element <em>int<\/em> array where each element number is the card. The array element values are either 1 or 0 depending on whether a card (element number) is drawn.<\/p>\n<p>When implementing a random song playlist, you must ensure that the same tune doesn&#8217;t play twice within a given range. If the entire list of tunes are shuffled alone, the solution to the Exercise would be identical to the Lotto program given in my books: Play a song once, flag it as played, then don&#8217;t play it again. But in this Exercise the playlist repeats, playing up to 100 instances of the variety of 20 tunes. Still, the solution is similar.<\/p>\n<p>To the <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/09\/09exercise-a.c\">sample code<\/a> presented in the Exercise, I add a <code>recent[]<\/code> array. This array, like the array for a deck of cards or lotto balls, keeps track of the tunes played:<\/p>\n<p><code>int recent[COUNT];<\/code><\/p>\n<p>The size of the array is the same as the amount of tunes, <code>COUNT<\/code> (or 20). It could be 15, which is the limit during which no tune can play twice, but I chose 20 so that the array could be initialized at the same time as the <code>frequency[]<\/code> array:<\/p>\n<pre class=\"screen\">\r\nfor(t=0;t&lt;COUNT;t++)\r\n{\r\n    frequency[t] = 0;\r\n    recent[t] = COUNT+1;\r\n}<\/pre>\n<p>The first statement in the <em>for<\/em> loop initializes the <code>frequency[]<\/code> array, setting all elements to zero. The second line initializes the <code>recent[]<\/code> array to the value <code>COUNT+1<\/code>. This value is higher than any element number in the <code>tune[]<\/code> array.<\/p>\n<p>Variable <code>play<\/code> holds a random value, 0 to 19, indicating which tune to play. Before the tune is played, the contents of the <code>recent[]<\/code> array are scanned to see if that tune\/number has played recently:<\/p>\n<pre class=\"screen\">do\r\n{\r\n    play = rand() % COUNT;\r\n    found = 0;\r\n    for(t=0;t&lt;RANGE;t++)\r\n    {\r\n        if(recent[t]==play)\r\n        found=1;\r\n    }\r\n} while(found);<\/pre>\n<p>When the value of <code>play<\/code> is stored within <code>RANGE<\/code> (15) elements of array <code>recent[]<\/code>, the <code>found<\/code> flag is set. If so, the <em>while<\/em> loop repeats, fetching another random number for <code>play<\/code>. Only when <code>play<\/code> doesn&#8217;t appear within the first <code>RANGE<\/code> elements of the <code>recent[]<\/code> array does the loop stop.<\/p>\n<p>After the code is certain that the <code>play<\/code> value is fresh, the tune is &#8220;played&#8221; and its title output.<\/p>\n<p>Next, a <em>for<\/em> loop shifts the values in the <code>recent[]<\/code> array:<\/p>\n<pre class=\"screen\">for(t=COUNT-1;t&gt;0;t--)\r\n{\r\n    recent[t] = recent[t-1];\r\n}\r\nrecent[0] = play;<\/pre>\n<p>Each element is replaced by the preceding element. So element 18 becomes 19, and so on. After the elements are shifted, the most recent tune played is added at the start of the list. This technique is how the code keeps track of recent songs played.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/09\/09exercise-b.c\">Click here<\/a> to view the full code. Here&#8217;s the tail part of the output:<\/p>\n<pre><code>...\r\nPlaying 99: Call Me\r\nPlaying 100: My Sharona\r\nPlay Frequency:\r\nBrandy, 5\r\nRespect, 5\r\nHey Jude, 4\r\nThe Twist, 5\r\nUptown Funk, 5\r\nMacarena, 5\r\nKiller Queen, 5\r\nLady, 5\r\nFoolish Games, 5\r\nCall Me, 6\r\nRoyals, 6\r\nThe Sign, 6\r\nHappy, 6\r\nBillie Jean, 5\r\nMy Sharona, 6\r\nFamily Affair, 5\r\nHurts So Good, 4\r\nSmooth, 4\r\nMack The Knife, 4\r\nNo One, 4<\/code><\/pre>\n<p>Unlike the example shown in the Exercise post, the play frequency results show values of 4, 5, and 6. This means the tunes are distributed relatively evenly throughout the 100 plays. Reviewing the detailed output shows that no two songs are played back-to-back &mdash; or within 15 tunes of itself.<\/p>\n<p>I hope that you were also able to devise a clever and workable solution to the puzzle. This challenge was more nerdy that most, which is good when you must exercise your C language kung fu.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The solution to this month&#8217;s Exercise is similar to code I&#8217;ve presented in my books and in this blog with regards to randomly drawing from a fixed set of elements. Yet it has an extra level of complexity.<\/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-3266","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\/3266","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=3266"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3266\/revisions"}],"predecessor-version":[{"id":3284,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3266\/revisions\/3284"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}