Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在不允许重复的情况下查找两个阵列的交点_C_Arrays - Fatal编程技术网

C 在不允许重复的情况下查找两个阵列的交点

C 在不允许重复的情况下查找两个阵列的交点,c,arrays,C,Arrays,我试图在不允许重复的情况下找到两个数组的交集和并集。我有交叉口的代码,但我的输出远远不正确。我怀疑问题在于我的嵌套for循环: 这是我的密码: #include <stdio.h> int main() { int i,j,n,k; printf("Enter size of the arrays: "); scanf("%d", &j); int first_array[j]; int second_array[j]; printf(

我试图在不允许重复的情况下找到两个数组的交集和并集。我有交叉口的代码,但我的输出远远不正确。我怀疑问题在于我的嵌套for循环:

这是我的密码:

#include <stdio.h>

int main() 
{   
  int i,j,n,k;

  printf("Enter size of the arrays: ");
  scanf("%d", &j);

  int first_array[j];
  int second_array[j];

  printf("Enter elements of first array: ");
  for(i=0; i<j; i++)
  {
      scanf("%d", &first_array[i]);
  }

  printf("Enter elements of second array: ");
  for(i=0; i<j; i++)
  {
      scanf("%d", &second_array[i]);
  }

  /*INTERSECTION & UNION*/
  int ii=-1,ui=-1;
  int intersect_array[2*n];
  int union_array[n];

  for(i=0; i<j; i++)
  {
      for(n=0; n<j; n++)
      {
          if(first_array[i] == second_array[n])
          {
              for(ii=0; ii<j; ii++)
              {
                  if(first_array[i] != intersect_array[ii])
                  {
                      intersect_array[ii] = first_array[i];
                  }
              }
          }
      }
  }

  printf("Intersection: ");
  for(i=0; i<=ii; i++)
      printf("%d",intersect_array[i]);
  printf("\n");

  return(0);
}
当我的嵌套for循环为此时,输出是正确的,只允许重复数字:

  for(i=0; i<j; i++)
  {
      for(n=0; n<j; n++)
      {
          if(first_array[i] == second_array[n])
          {
              intersect_array[ii] = first_array[i];
          }
      }
  }

for(i=0;i您的重复值检查错误。这是工作版本
一旦找到重复值,就应该中断循环

#include <stdio.h>

int main() 
{   
int i,j = 3,n,k;

int first_array[3] = {1, 2, 3};
int second_array[3] = {2, 3, 4};



/*INTERSECTION & UNION*/
int ii=-1,ui=-1;
int intersect_array[3] = {0};
int union_array[3*2];
int intersectCount = 0;

for(i=0; i<j; i++)
{
    for(n=0; n<j; n++)
    {
        if(first_array[i] == second_array[n])
        {
            for(ii=0; ii<intersectCount; ii++)
            {
                if(first_array[i] == intersect_array[ii])
                    break;
            }
            // if it complete loop without break repeat value not found
            if (ii == intersectCount) 
               intersect_array[intersectCount++] = first_array[i];
        }
    }
}

printf("Intersection: ");
for(i=0; i<intersectCount; i++)
    printf("%d",intersect_array[i]);
printf("\n");

return(0);
  }
#包括
int main()
{   
int i,j=3,n,k;
int-first_数组[3]={1,2,3};
int-second_数组[3]={2,3,4};
/*交并*/
INTII=-1,ui=-1;
int intersect_数组[3]={0};
int-union_数组[3*2];
int int intersectCount=0;

对于(i=0;
intersect\u array
的最大大小是
n
,而不是
2*n
union\u array
的最大大小是
2*n
,而不是
n
。我怀疑你跑过了后者的末尾,导致了未定义的行为。我肯定会对数组进行排序,然后使用更有效的代码,如()什么是“允许重复”?重复项,即每个数组都是一个数学集,或者什么?@Lundin重复项意味着在交集输出中,不能有重复的数字,因此如果数组1输入为“2 2 3 4”,数组2输入为“2 3 5 6”,则交集输出为“2 3”,而不是“2 2 2 3”。在我添加for循环和if语句之前,在交叉点输出中有重复的数字。对于没有更清楚地说明这是什么,我深表歉意meant@chux-谢谢编辑。我没有注意与问题无关的部分。当我尝试您的更改时,它会给我一个分段错误。我必须能够输入ar数组中的光线大小和元素,它们不可能是硬的-coded@Requiem_7只需从代码中获取所需的部分。我刚刚制作了一个示例工作版本。
#include <stdio.h>

int main() 
{   
int i,j = 3,n,k;

int first_array[3] = {1, 2, 3};
int second_array[3] = {2, 3, 4};



/*INTERSECTION & UNION*/
int ii=-1,ui=-1;
int intersect_array[3] = {0};
int union_array[3*2];
int intersectCount = 0;

for(i=0; i<j; i++)
{
    for(n=0; n<j; n++)
    {
        if(first_array[i] == second_array[n])
        {
            for(ii=0; ii<intersectCount; ii++)
            {
                if(first_array[i] == intersect_array[ii])
                    break;
            }
            // if it complete loop without break repeat value not found
            if (ii == intersectCount) 
               intersect_array[intersectCount++] = first_array[i];
        }
    }
}

printf("Intersection: ");
for(i=0; i<intersectCount; i++)
    printf("%d",intersect_array[i]);
printf("\n");

return(0);
  }