编写一个程序 程序要求 将数组1和数组2相同的数字复制到目标数组
数组1: 1 4 1 5 9 2
数组2: 1 1 8 2 5 3
将 1 1 5 2复制到目标群组
这是我的代码 能帮我看看问题在哪里吗
我运行下来的结果只有 1
#include<stdio.h>
#include<string.h>
int common_elements(int length, int source1[length], int source2[length], int destination[length]) {
int i,j,k=0;
for(i=0;i<length;i++) {
int flag=1;
for(j=0;j<k;j++){
if(source2[i]!=destination[j]) {
flag=0;
break;
}
}
if(flag)
destination[k++]=source1[i];
}
return k;
}
int main(){
int n,a;
int source1[6]={1,4,1,5,9,2};
int source2[6]={1,1,8,2,5,3};
int destination[6];
a=common_elements(6,source1,source2,destination);
for(n=0;n<a;n++){
printf("%d", destination[n]);
return 0;
}
}