{"id":6831,"date":"2025-02-22T00:01:54","date_gmt":"2025-02-22T08:01:54","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6831"},"modified":"2025-03-01T08:27:50","modified_gmt":"2025-03-01T16:27:50","slug":"rotating-the-data","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6831","title":{"rendered":"Rotating the Data"},"content":{"rendered":"<p>You wait all day in line &mdash; you know, one of those bureaucratic government things &mdash; only to get to the front of the line and discover that you&#8217;re missing something. So back to the end of the line you go, ready to toil all over again. Sound familiar? But a computer wouldn&#8217;t care.<br \/>\n<!--more--><br \/>\nComputers don&#8217;t mind waiting, nor do they find problems with doing things repetitively. Just write the proper code and the computer does the rest. If only the DMV were as simple . . .<\/p>\n<p>In this instance, I&#8217;m thinking of rotating or shuffling an array: Moving each element in the array up a notch and sending the first element (zero) to the end. For example:<\/p>\n<p><code>{ 1, 2, 3, 4, 5, 6, 7, 8 }<\/code><\/p>\n<p>Becomes:<\/p>\n<p><code>{ 2, 3, 4, 5, 6, 7, 8, 1 }<\/code><\/p>\n<p>You don&#8217;t need two arrays for this manipulation, just a routine that shuffles the elements. Here is my code that painlessly carries out the task:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_02_22-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_02_22-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };\r\n    int size,x,save;\r\n\r\n    <span class=\"comments\">\/* obtain array size *\/<\/span>\r\n    size = sizeof(a)\/sizeof(a[0]);\r\n\r\n    <span class=\"comments\">\/* output the array *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n        printf(\" %d\",a[x]);\r\n    putchar('\\n');\r\n\r\n    <span class=\"comments\">\/* shuffle to the left *\/<\/span>\r\n        <span class=\"comments\">\/* retain the first element *\/<\/span>\r\n    save = a[0];\r\n        <span class=\"comments\">\/* move over all remaining elements *\/<\/span>\r\n    for( x=0; x&lt;size-1; x++ )\r\n        a[x] = a[x+1];\r\n        <span class=\"comments\">\/* restore the first element to the last pos. *\/<\/span>\r\n    a[size-1] = save;\r\n\r\n    <span class=\"comments\">\/* output the array *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n        printf(\" %d\",a[x]);\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The program calculates the number of elements in <em>int<\/em> array <code>a[]<\/code> by using this trick:<\/p>\n<p><code>size = sizeof(a)\/sizeof(a[0]);<\/code><\/p>\n<p>The size of all storage occupied by <code>a[]<\/code> is divided by the size of a single element, <code>a[0]<\/code>. The result is the number of elements in the array. While I can easily see from the code that the number is 8, and I could just assign this value to variable <code>size<\/code>, this expression allows me to change the array&#8217;s size without having to work hard finding and replacing each reference to the array&#8217;s size.<\/p>\n<p>The first <em>for<\/em> loop outputs the original array. The next <em>for<\/em> loop performs the shuffle, but before then the first element is saved, <code>save = a[0]<\/code>. The <em>for<\/em> loop counts from element zero to the next-to-last element:<\/p>\n<p><code>for( x=0; x&lt;size-1; x++ )<\/code><\/p>\n<p>The <code>x&lt;size-1<\/code> limit is important as otherwise the statement that follows would access an array element beyond the end of the array. Its job is to &#8220;shuffle over&#8221; the element values:<\/p>\n<p><code>a[x] = a[x+1];<\/code><\/p>\n<p>Element <code>x<\/code> becomes the next element, <code>x+1<\/code>.<\/p>\n<p>The final duty is to copy the saved first element to the last element:<\/p>\n<p><code>a[size-1] = save<\/code><\/p>\n<p>Before the program exits, a <em>for<\/em> loop outputs the newly-updated array, as shown in the output:<\/p>\n<p><code>&nbsp;1 2 3 4 5 6 7 8<br \/>\n&nbsp;2 3 4 5 6 7 8 1<\/code><\/p>\n<p>While this program is fun, and it demonstrates what I consider to be a proper shuffle, I want it to do more!<\/p>\n<p>The following update performs the operation repeatedly until the array is re-shuffled back to its original order:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_02_22-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_02_22-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8 };\r\n    int size,x,y,save;\r\n\r\n    <span class=\"comments\">\/* obtain array size *\/<\/span>\r\n    size = sizeof(a)\/sizeof(a[0]);\r\n\r\n    <span class=\"comments\">\/* output the array *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n        printf(\" %d\",a[x]);\r\n    putchar('\\n');\r\n\r\n    <span class=\"comments\">\/* shuffle to the left `size ` times *\/<\/span>\r\n    for( y=0; y&lt;size; y++ )\r\n    {\r\n            <span class=\"comments\">\/* retain the first element *\/<\/span>\r\n        save = a[0];\r\n            <span class=\"comments\">\/* move over all remaining elements *\/<\/span>\r\n        for( x=0; x&lt;size-1; x++ )\r\n            a[x] = a[x+1];\r\n            <span class=\"comments\">\/* restore the first element to the last pos. *\/<\/span>\r\n        a[size-1] = save;\r\n\r\n        <span class=\"comments\">\/* output the array *\/<\/span>\r\n        for( x=0; x&lt;size; x++ )\r\n            printf(\" %d\",a[x]);\r\n        putchar('\\n');\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The above modification requires another <em>int<\/em> variable, <code>y<\/code>, to handle the outer loop. This loop spins <code>size<\/code> number of times after the original array is output. Otherwise, the code contained in the <code>y<\/code> loop is the same as presented earlier.<\/p>\n<p>Here&#8217;s the updated output:<\/p>\n<p><code>&nbsp;1 2 3 4 5 6 7 8<br \/>\n&nbsp;2 3 4 5 6 7 8 1<br \/>\n&nbsp;3 4 5 6 7 8 1 2<br \/>\n&nbsp;4 5 6 7 8 1 2 3<br \/>\n&nbsp;5 6 7 8 1 2 3 4<br \/>\n&nbsp;6 7 8 1 2 3 4 5<br \/>\n&nbsp;7 8 1 2 3 4 5 6<br \/>\n&nbsp;8 1 2 3 4 5 6 7<br \/>\n&nbsp;1 2 3 4 5 6 7 8<\/code><\/p>\n<p>There is a point to all this, which I&#8217;ll get to starting with <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6833\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is yet another way to manipulate data in array, the old &#8220;shuffle&#8221; to the right. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6831\">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":[2],"tags":[],"class_list":["post-6831","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6831","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=6831"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6831\/revisions"}],"predecessor-version":[{"id":6863,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6831\/revisions\/6863"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}