c语言程序相关的问题

编写一个程序 程序要求 将数组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;
}
}

//两层循环保证遍历和比较到两个数组的每个元素
//两个数组中都有的元素放入目标数组中,用flag标记防止重复
//return应该放在输出循环的外面

#include<stdio.h>
#include<string.h>
int common_elements(int length, int source1[length], int source2[length], int destination[length])
{
    int i,j,h,k=0,flag;
    //两层循环保证遍历和比较到两个数组的每个元素
    for(i=0; i<length; i++)
    {
        flag=0;
        for(j=0; j<length; j++)
        {
            if(source1[i]==source2[j]&&flag==0)
            {
                flag=1;
                destination[k++]=source1[i];
                //两个数组中都有的元素放入目标数组中,用flag标记防止重复
            }
        }
    }
    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[15];
    a=common_elements(6,source1,source2,destination);
    for(n=0; n<a; n++)
    {
        printf("%d ", destination[n]);
    }
    return 0;//return应该放在输出循环的外面
}

img

朋友,你的整体逻辑错了
你只比较了source2,没比较source,应该把两个都遍历,两个数组都有的元素,才存到destination里面

#include<stdio.h>
#include<string.h>
int common_elements(int length, int source1[], int source2[], int destination[]) 
{
    int i, j, k = 0;
    for(i=0; i<length; i++) {
        bool flag = false;
        for(j=0; j<length; j++) {
            if(source1[i] == source2[j]) {   //两个数组都有的元素
                flag = true;
                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;
}

#include<stdio.h>
#include<string.h>

int common_elements(int length, int source1[], int source2[], int destination[]) {

    int i,j,k=0;
    for(i=0; i<length; i++) {
        bool flag = false;
        for(j=0; j<length; j++) {
            if(source1[i] == source2[j]) {
                flag = true;
                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;
}

有帮助希望采纳哟

前面几位答主没有考虑如果两个数组里相同元素数目不一样的情况,比如一个数组有3个1,而另一个数组有2个1,那么destination应该只含2个1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int common_elements(int length, int source1[], int source2[], int destination[])
{
    int i, j, k = 0;
    int *flags = (int *)malloc(length * sizeof(int));
    memset(flags, 0, length * sizeof(int));
    for (i = 0; i < length; i++)
    {
        int found = 0;
        for (j = 0; j < length; j++)
        {
            // 两个数组的元素相等并且都没有被提取
            if (source1[i] == source2[j] && flags[j] == 0)
            {
                found = 1;
                break;
            }
        }
        if (found)
        {
            // 提取该元素并设置已提取的标记
            destination[k++] = source2[j];
            flags[j] = 1;
        }
    }
    free(flags);
    return k;
}

int main()
{
    int i, n;
    // int source1[6] = {1, 4, 1, 5, 9, 2};
    int source1[6] = {1, 1, 1, 5, 9, 2};
    int source2[6] = {1, 1, 8, 2, 5, 3};
    int destination[6];
    n = common_elements(6, source1, source2, destination);
    for (i = 0; i < n; i++)
        printf("%d ", destination[i]);
    return 0;
}

实在没看懂你的代码,第11行的时候,k是0,循环中又不能改变k,直接不会执行循环体。

你在判断时,如果不相等就直接跳出二层循环了,剩下的怎么判断