"Exercise" exchange the contents of array A with the contents of array B (the array is the same size)

The prototype that comes to mind when seeing this question is to exchange the values ​​of two variables. The conventional idea should be to create a temporary variable, then exchange three times, and finally exchange the values ​​of the two variables. Then this question uses the same idea, using loops to achieve multiple exchanges of variables in the array.

#include <stdio.h>
int main()
{
	int arr1[] = { 1, 3, 5, 7, 9 };//随便创建的数组和变量
	int arr2[] = { 3, 4, 5, 6 ,7 };
	int tmp;
	int i = 0;
	for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
	{
		tmp = arr1[i];
		arr1[i] = arr2[i];
		arr2[i] = tmp;
	}
	for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
	{
		printf("%d ", arr1[i]);
	}
	printf("\n");
	for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); i++)
	{
		printf("%d ", arr2[i]);
	}
	printf("\n");
	return 0;
}

Guess you like

Origin blog.csdn.net/NanlinW/article/details/89787355