交换数组

时间:2022-11-17 17:54:21

问题:交换两个数组的内容(数组的大小相同)

int main()
{
int arr1[] = { 1,3,5,7,9 };
int arr2[] = { 2,4,6,8,0 };
int tmp = 0;
int i = 0;
int sz = sizeof(arr1) / sizeof(arr1[0]);
for (i = 0;i < sz;i++)
{
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
printf("arr1 = ");
for (i = 0;i < sz;i++)
{
printf("%d ", arr1[i]);
}
printf("\n");//换行
printf("arr2 = ");
for (i = 0;i < sz;i++)
{
printf("%d ", arr2[i]);
}
return 0;
}