{"id":6833,"date":"2025-03-01T00:01:39","date_gmt":"2025-03-01T08:01:39","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6833"},"modified":"2025-03-08T09:09:23","modified_gmt":"2025-03-08T17:09:23","slug":"flipping-the-data","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6833","title":{"rendered":"Flipping the Data"},"content":{"rendered":"<p>Just as it&#8217;s possible to rotate elements in an array, shuffling them all up a notch and moving the first element to the end, it&#8217;s also possible to flip an array, reversing the order of its elements.<br \/>\n<!--more--><br \/>\nFor example, you start with this array:<\/p>\n<p><code>{ 1, 2, 3, 4, 5, 6, 7, 8 }<\/code><\/p>\n<p>After the operation, the array&#8217;s values are:<\/p>\n<p><code>{ 8, 7, 6, 5, 4, 3, 2, 1 }<\/code><\/p>\n<p>As shown in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6831\">last week&#8217;s Lesson<\/a>, it&#8217;s possible to perform this operation without requiring a second array. Here&#8217;s how I do it:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_03_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_03_01-Lesson.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\">\/* reverse the elements *\/<\/span>\r\n    for( x=0; x&lt;(size\/2); x++ )\r\n    {\r\n        save = a[x];\r\n        a[x] = a[size-x-1];\r\n        a[size-x-1] = save;\r\n    }\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>After the array is output, a <em>for<\/em> loop reverses the elements. Variable <code>save<\/code> holds the value of the first element, which is then replaced with the value of the last element. Then the last element is set to the value of <em>int<\/em> variable <code>save<\/code>:<\/p>\n<p><code>for( x=0; x&lt;(size\/2); x++ )<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;save = a[x];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;a[x] = a[size-x-1];<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;a[size-x-1] = save;<br \/>\n}<\/code><\/p>\n<p>This operation repeats as variable <code>x<\/code> increments to half the size of the array. A final <em>for<\/em> loop outputs the results:<\/p>\n<p><code>&nbsp;1 2 3 4 5 6 7 8<br \/>\n&nbsp;8 7 6 5 4 3 2 1<\/code><\/p>\n<p>This code is designed to handle an array of any size, which is why the statement <code>size = sizeof(a)\/sizeof(a[0]);<\/code> is used to obtain the array&#8217;s size. But what about an array with an odd number of elements?<\/p>\n<p>I modified the value of <code>a[]<\/code> to add three more elements: 9, 10, and 11. Here&#8217;s the program&#8217;s updated output:<\/p>\n<p><code>&nbsp;1&nbsp;2&nbsp;3&nbsp;4&nbsp;5&nbsp;6&nbsp;7&nbsp;8&nbsp;9&nbsp;10&nbsp;11<br \/>\n&nbsp;11&nbsp;10&nbsp;9&nbsp;8&nbsp;7&nbsp;6&nbsp;5&nbsp;4&nbsp;3&nbsp;2&nbsp;1<\/code><\/p>\n<p>Because the <code>size\/2<\/code> expression works with integers, the &#8220;half&#8221; value is rounded down, which means the middle element &mdash; no matter what the array size &mdash; isn&#8217;t swapped. It doesn&#8217;t need to be!<\/p>\n<p>This type of manipulation is demonstrated with an integer array. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6859\">next week&#8217;s Lesson<\/a> I cover messing with an array of strings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rather than just shuffle the elements, this time the code flips first for last all values from element zero through element <em>n<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6833\">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-6833","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\/6833","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=6833"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6833\/revisions"}],"predecessor-version":[{"id":6876,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6833\/revisions\/6876"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}