C language - check if two arrays have the same element

I am a little nervous when writing a blog for the first time~


But so happy! (The little rookie rolls happily in his own territory = =)

The following is an exercise problem I encountered while doing the IF statement exercise, and I want to organize it on the blog : Determine whether two arrays have the same element.

Ideas:

First create two arrays, respectively a[ ] and b[ ], first compare the first element in the a array with all the elements in the b array, and then compare the second element in the a array with all the elements in the b array comparison, and so on. Use two for loops to complete, use the i loop to generate the subscripts of the a array, use the j loop to generate the b array subscripts in the loop body, and judge whether a[ i ] is equal to b[ j ] in the j loop, if the condition is true Immediately the same element . The flag is used to mark the state of the program running to a certain moment to judge whether the statement in if is executed.

The role of the system function is to run the command passed to it in the form of a string parameter, and wait for the completion of the command, in the form: #include <stdlib.h>  int system(const char * string);

code show as below:

#include <stdio.h>
#include <stdlib.h>
intmain()
{
	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 is 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("The two arrays have no identical elements\n");
	else
		printf("The two arrays have the same elements\n");
	system("pause");
	return 0;
}

In order to achieve code versatility, the following optimization is carried out, and it is extended to two arrays with any number of elements to achieve this function.

code show as below:

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

intmain()
{
	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++)//此时sizeof(a) / sizeof(a[0])=5
	{
		for (j = 0; j < sizeof(b) / sizeof(b[0]); j++)//此时sizeof(b) / sizeof(b[0])=6
		{
			if (a[i] == b[j])
			{
				printf("The two arrays have the same elements\n");
				system("pause");
				return 0;//Return if the same element exists, reducing the number of cycles of the loop structure
			}
		}
	}
	if (i == sizeof(a) / sizeof(a[0]))
	printf("The two arrays have no identical elements\n");

	system("pause");
	return 0;
}

sizeof( ) is a capacity measurement function 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 array a. Integer sizeof(a[0])=4. Because there are five elements in the array a in the above code, then sizeof(a)=20, sizeof(a[0])=4, so sizeof(a) / sizeof(a[0])=5. In this way, no matter the number of elements in the array, no matter what data type, this part of the code does not need to be changed.

——————————————————————

On the one hand, I started blogging to record my learning process, so that I can accumulate and gain slowly; it is also to exercise my logical thinking and expression skills; of course, it would be great if I could bring some help to others~Fine ~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325818348&siteId=291194637