代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
//定义一个交换数组值的函数
void swap(int arr1[], int arr2[],int length){
int i,temp;
for (i = 0; i < length; i++){
temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
}
int main(){
int arr1[5];
int arr2[5];
//输入两个数组
printf("请输入数组1:\n");
for (int i = 0; i < 5; i++){
scanf("%d", &arr1[i]);
}
printf("请输入数组2:\n");
for (int i = 0; i < 5; i++){
scanf("%d", &arr2[i]);
}
//调用函数
swap(arr1, arr2, 5);
//输出交换后的数组
printf("交换后\n数组1: ");
for(int i=0; i < 5; i++){
printf("%d ", arr1[i]);
}
printf("\n数组2: ");
for (int i=0; i < 5; i++){
printf("%d ", arr2[i]);
}
system("pause");
return 0;
}
运行结果: