Implemented in C: Swaps the contents of array A and the contents of Group B (as large as an array).

Source: Internet
Author: User

We have already completed the process of exchanging two variables, and the two topics are the same, but the two numbers become two arrays.

Perhaps we would like to think: do we need a third array to swap as an intermediate variable?

The answer is no, we only need to pass a loop body, each time we take out an element in the array and the middle variable t for the Exchange operation can be done.

The code is very simple, we use the array 1{1,2,3}, array 2{4,5,6} For example, to complete the program.

The code is as follows:

#include <stdio.h>intMain () {intArr1[] = {1,2,3}, arr2[] = {4,5,6},i,t; printf ("arr1=");  for(i =0; I <3; i++) {printf ("%d", Arr1[i]); } printf ("\ n"); printf ("arr2=");  for(i =0; I <3; i++) {printf ("%d", Arr2[i]); } printf ("\ n");  for(i =0; I <3; i++) {T=Arr1[i]; Arr1[i]=Arr2[i]; Arr2[i]=T; } printf ("New arr1=");  for(i =0; I <3; i++) {printf ("%d", Arr1[i]); } printf ("\ n"); printf ("New arr2=");  for(i =0; I <3; i++) {printf ("%d", Arr2[i]); } printf ("\ n"); System ("Pause"); return 0;}

In the program, we shilling its output of the original array, and then output the array after the exchange, more intuitive to reflect the changes occurred in the group.
Finally, attach the result of the program running:

Implemented in C: Swaps the contents of array A and the contents of Group B (as large as an array).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.