#include<stdio.h> #define N 20 void inputarr (int *p, int *n); void outputarr (int *p,int n); void swap(int *a, int *b); void swaparr(int *a, int n, int *b,int m); int main() { int i,n=0,m=0; int a[N],b[N]; //printf("请输入数组a,以-1结束:\n"); inputarr(a,&n); //printf("请输入数组b,以-1结束:\n"); inputarr(b,&m); swaparr(a, n, b, m); //printf("数组a中所有元素:\n"); outputarr (a,m); //printf("数组b中所有元素:\n"); outputarr (b,n); return 0; } /********** Begin **********/ /********** End **********/编程要求 根据提示,在右侧编辑器 Begin-End 区间补充代码,完成三个子函数的定义,交换数组 a 和数组 b 中的对应元素,具体要求如下: 函数 inputarr()实现数组的输入,当遇到数字-1时,结束输入; 函数 swaparr()实现数组元素的交换; 函数 outputarr()实现数组元素的输出。 测试说明 平台会对你编写的代码进行测试: 测试输入: 91 1 42 32 9 57 6 33 2 88 -1 41 13 25 32 29 74 68 30 5 48 57 6 33 62 81 -1 预期输出: 41 13 25 32 29 74 68 30 5 48 57 6 33 62 81 91 1 42 32 9 57 6 33 2 88
时间: 2023-07-23 12:27:14 浏览: 126
#include<stdio.h>
#define N 20
void inputarr (int *p, int *n) {
int i;
for(i=0; i<N; i++) {
scanf("%d", &p[i]);
if(p[i] == -1) {
break;
}
(*n)++;
}
}
void outputarr (int *p,int n) {
int i;
for(i=0; i<n; i++) {
printf("%d ", p[i]);
}
printf("\n");
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void swaparr(int *a, int n, int *b,int m) {
int i;
for(i=0; i<n && i<m; i++) {
swap(&a[i], &b[i]);
}
}
int main() {
int i,n=0,m=0;
int a[N],b[N];
inputarr(a,&n);
inputarr(b,&m);
swaparr(a, n, b, m);
outputarr(a,m);
outputarr(b,n);
return 0;
}
阅读全文
相关推荐


















