The C language determines whether two arrays have the same element

  • 2020-06-19 11:24:40
  • OfStack

Ideas:

First create two arrays, a[] and b[]. Compare the first element in a to all the elements in b, then compare the second element in a to all the elements in b, and so on. for loop is used twice to complete the task. i loop is used to generate each subscript of a array. j loop is used to generate the subscript of b array in the body of the loop. flag is used to mark the state of the program up to a certain moment to determine whether the statements in if are executed.

The system function runs the command passed to it as a string argument and waits for it to complete, in form #include < stdlib.h > int system(const char * string);

The code is as follows:


#include <stdio.h>
#include <stdlib.h>
int main()
{
	int a[5] = {5,3,2,65,8};
	int b[8] = {78,8,9,56,3,6,0,7};
	int i = 0;
	int j = 0;
	int flag = 0;  //flag Used to mark 
	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 8; j++)
		{
			if (a[i] == b[j])
			  flag++;	
		}
	}
	if (flag == 0)
	    printf(" Two arrays do not have the same elements \n");
	else
		printf(" Both arrays have the same elements \n");
	system("pause");
	return 0;
}

In order to achieve code universality, the following optimization, generalized to two arrays have any number of elements, to achieve this function.

The code is as follows:


#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	int a[] = {34,24,78,5,3};
	int b[] = {23,7,98,5,23,3};
	int i = 0;
	int j = 0;
	for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)// At this time sizeof(a) / sizeof(a[0])=5
	{
		for (j = 0; j < sizeof(b) / sizeof(b[0]); j++)// At this time sizeof(b) / sizeof(b[0])=6
		{
			if (a[i] == b[j])
			{
				printf(" Both arrays have the same elements \n");
				system("pause");
				return 0;// Returns the same element, reducing the number of runs of the loop structure 
			}
		}
	}
	if (i == sizeof(a) / sizeof(a[0]))
	printf(" Two arrays do not have the same elements \n");
 
	system("pause");
	return 0;
}

sizeof() is a capacity metric that returns the size of a variable or type in bytes.

Usage: sizeof(type specifier, array name or expression) or sizeof(variable name).

sizeof(a) is the total size of the array a. sizeof(a[0])=4 for integers. sizeof(a)=20, sizeof(a[0])=4, so sizeof(a)/sizeof(a[0])=5. This code does not need to be changed regardless of the number of elements in the array or the data type.

Here's what others have added

We all know that it only takes two layers of the for loop to determine if two arrays have the same element, but inside the for loop, it's important to pay attention to when to break out of the loop.
Here's the third way:


#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int arr1[4] = {0};
	int arr2[5] = {0};
	int i = 0,j=0,k=0;
	printf(" Please enter an array 1(4 An element ) : \n");
	for(i = 0;i<sizeof(arr1)/sizeof(arr1[0]);i++)
	{
		scanf("%d",&arr1[i]);
	}
	printf(" Please enter an array 2(5 An element ) : \n");
	for(i=0;i<sizeof(arr2)/sizeof(arr2[0]);i++)
	{
		scanf("%d",&arr2[i]);
	}
 
	for(j = 0; j<sizeof(arr1)/sizeof(arr1[0]); j++)
	{
		for(k = 0;k<sizeof(arr2)/sizeof(arr2[0]);k++)
		{
			if(arr1[j] == arr2[k])
			{
				printf(" It has the same element! \n");
			}
			if(k<sizeof(arr2)/sizeof(arr2[0]))	
			{
				break;
			}
		}
		if(j>=sizeof(arr1)/sizeof(arr1[0]))	
		printf(" No identical elements! \n");
	}
	return 0;
}

The above method needs to be judged more, and the judgment should also be accurate, the following method will be easier 1:


#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int arr1[4] = {0};
	int arr2[5] = {0};
	int i = 0,j=0,k=0;
	printf(" Please enter an array 1(4 An element ) : \n");
	for(i = 0;i<sizeof(arr1)/sizeof(arr1[0]);i++)
	{
		scanf("%d",&arr1[i]);
	}
	printf(" Please enter an array 2(5 An element ) : \n");
	for(i=0;i<sizeof(arr2)/sizeof(arr2[0]);i++)
	{
		scanf("%d",&arr2[i]);
	}
 
	for(j = 0; j<sizeof(arr1)/sizeof(arr1[0]); j++)
	{
		for(k = 0;k<sizeof(arr2)/sizeof(arr2[0]);k++)
		{
			if(arr1[j] == arr2[k])
			{
				printf(" It has the same element! \n");
				return 0;
			}
		}
	}
	printf(" No identical elements! \n");
	return 0;
}

Related articles: