{"id":218284,"date":"2025-05-23T14:18:23","date_gmt":"2025-05-23T14:18:23","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=218284"},"modified":"2025-05-23T14:18:25","modified_gmt":"2025-05-23T14:18:25","slug":"define-a-function-named-swapvalues-that-takes-four-integers-as-parameters-and-swaps-the-first-with-the-second-and-the-third-with-the-fourth-values","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/05\/23\/define-a-function-named-swapvalues-that-takes-four-integers-as-parameters-and-swaps-the-first-with-the-second-and-the-third-with-the-fourth-values\/","title":{"rendered":"Define a function named SwapValues that takes four integers as parameters and swaps the first with the second, and the third with the fourth values."},"content":{"rendered":"\n<p>Define a function named SwapValues that takes four integers as parameters and swaps the first with the second, and the third with the fourth values. Then write a main program that reads four integers from input, calls function SwapValues() to swap the values, and prints the swapped values on a single line separated with spaces. programming c please<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-0-color\">The Correct Answer and Explanation is:<\/mark><\/strong><sup data-fn=\"02f70d68-6fd8-43bb-929c-5f036a82bc8d\" class=\"fn\"><a id=\"02f70d68-6fd8-43bb-929c-5f036a82bc8d-link\" href=\"#02f70d68-6fd8-43bb-929c-5f036a82bc8d\">1<\/a><\/sup><\/p>\n\n\n\n\n\n<p>Here is the correct C program that defines a function <code>SwapValues<\/code> to swap values as described, along with an explanation:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>C Program<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\n\/\/ Function to swap two values using pointers\nvoid SwapValues(int *a, int *b, int *c, int *d) {\n    int temp;\n\n    \/\/ Swap first and second\n    temp = *a;\n    *a = *b;\n    *b = temp;\n\n    \/\/ Swap third and fourth\n    temp = *c;\n    *c = *d;\n    *d = temp;\n}\n\nint main() {\n    int num1, num2, num3, num4;\n\n    \/\/ Read four integers\n    scanf(\"%d %d %d %d\", &amp;num1, &amp;num2, &amp;num3, &amp;num4);\n\n    \/\/ Call function to swap values\n    SwapValues(&amp;num1, &amp;num2, &amp;num3, &amp;num4);\n\n    \/\/ Print swapped values\n    printf(\"%d %d %d %d\\n\", num1, num2, num3, num4);\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<p>This C program demonstrates how to swap pairs of integers using a function and pointers.<\/p>\n\n\n\n<p>The function <code>SwapValues<\/code> is designed to take <strong>four integer pointers<\/strong> as its parameters. These pointers allow the function to directly access and modify the original variables passed from <code>main<\/code>. Inside the function, a temporary variable <code>temp<\/code> is used to perform the swapping:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, it swaps the values pointed to by <code>a<\/code> and <code>b<\/code>, which correspond to the first and second integers.<\/li>\n\n\n\n<li>Next, it swaps the values pointed to by <code>c<\/code> and <code>d<\/code>, which are the third and fourth integers.<\/li>\n<\/ol>\n\n\n\n<p>The <code>main<\/code> function starts by declaring four integers (<code>num1<\/code>, <code>num2<\/code>, <code>num3<\/code>, <code>num4<\/code>). It reads their values using <code>scanf<\/code>, which takes input from the user.<\/p>\n\n\n\n<p>The <code>SwapValues<\/code> function is then called with the <strong>addresses<\/strong> of these four integers using the address-of operator (<code>&amp;<\/code>). This allows the function to modify the actual values in memory rather than working on local copies.<\/p>\n\n\n\n<p>Finally, after the swapping, the new values are printed out in a single line using <code>printf<\/code>.<\/p>\n\n\n\n<p>This approach uses <strong>pointers<\/strong> to enable pass-by-reference in C, allowing changes made in the function to reflect in the calling environment. It\u2019s a fundamental concept in C programming, often used for manipulating values in memory, modifying arrays, or managing dynamic data structures.<\/p>\n\n\n\n<p>This program is efficient and demonstrates core C concepts like functions, pointers, and standard input\/output.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/05\/image-199.png\" alt=\"\" class=\"wp-image-218285\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Define a function named SwapValues that takes four integers as parameters and swaps the first with the second, and the third with the fourth values. Then write a main program that reads four integers from input, calls function SwapValues() to swap the values, and prints the swapped values on a single line separated with spaces. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-218284","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/218284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=218284"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/218284\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=218284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=218284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=218284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}