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).