题目描述
实现两个长度相等数组内容的交换
解题思路
函数调用,借助中间变量,利用for循环实现
代码
//将数组A的内容和数组B的内容进行交换,数组A和数组B长度一致
#include<stdio.h>
#include<windows.h>
void change(int a[],int b[],int length)
{
int tmp;
for (int i = 0; i < length; i++)
{
tmp = a[i];
a[i] = b[i];
b[i] = tmp;
}
for (int l = 0; l < length; l++)
{
printf("%d ", a[l]);
}
printf("\n"); //换行显示
for (int j = 0; j < length; j++)
{
printf("%d ", b[j]);
}
}
int main()
{
int arr1[] = { 15, 64, 8, 54, 13 }; //数组大小以及元素可合理设定
int arr2[] = { 98, 43, 25, 84,48 };
int len = sizeof(arr1) / sizeof(arr1[0]);
change(arr1,arr2,len);
system(“pause”);
return 0;
}