将数组A中的内容和数组B中的内容进行交换。(数组一样大)
我把数组内容交换写在swap函数里面,再用两个循环分别输出整形变量数组,而字符型可以直接输出。
sizeof(arr1) / sizeof(arr1[0])是用来判断数组长度的
#include <stdio.h>
#include <stdlib.h>
void swap(int arr1[], int arr2[], int leng)
{
int i = 0;
for (i = 0; i < leng; ++i)
{
int t = arr1[i];
arr1[i] = arr2[i];
arr2[i] = t;
}
}
int main() {
int i = 0;
int arr1[5] = { 0,1,2,3,4 };
int arr2[5] = { 5,6,7,8,9 };
swap(arr1, arr2, sizeof(arr1) / sizeof(arr1[0]));
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");
system ("pause");
return 0;
}
自己给出两个数组然后交换他们之间的元素
```#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void swap(int arr1[], int arr2[], int leng)
{
int i = 0;
for (i = 0; i < leng; ++i)
{
int t = arr1[i];
arr1[i] = arr2[i];
arr2[i] = t;
}
}
int main() {
int i = 0;
int arr1[5];
int arr2[5];
printf("请输入数组1:");
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); ++i) {
scanf("%d", &arr1[i]);
}
printf("请输入数组2:");
for (i = 0; i < sizeof(arr1) / sizeof(arr1[0]); ++i) {
scanf("%d", &arr2[i]);
}
swap(arr1, arr2, sizeof(arr1) / sizeof(arr1[0]));
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");
system("pause");
return 0;
}