C语言 如何在C编程语言中比较两个数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33793052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:13:10  来源:igfitidea点击:

How to compare two arrays in C programming language?

carrays

提问by Hassen Fatima

I want to compare two different arrays which are both int. One array is static and contains numbers from 1 to 10 and second arrays asks user to enter ten different numbers and the program checks which elements from both arrays are equal.

我想比较两个不同的数组,它们都是int. 第一个数组是静态的,包含 1 到 10 的数字,第二个数组要求用户输入十个不同的数字,程序会检查两个数组中的哪些元素相等。

#include <stdio.h>
int main(void) {

        int array1[] = {1,2,3,4,5,6,7,8,9,10};
        int array2[10];
        int i;

        for (i=0;i<11;i++) {

                printf("Enter numbers: ");
                scanf("%d", &array2);
        }

        for (i=0;i<11;i++) {

                if (array1[i] != array2[i]) {

                        printf("Not equal \n");
                }
                else {
                        printf("They are equal. \n");
                }
        }
}

The program always say not equal even if I enter a number that is equal to a number stored in first array.

即使我输入的数字等于存储在第一个数组中的数字,程序也总是说不相等。

采纳答案by Inisheer

scanf("%d", &array2);

You are never updating the index of array2when getting a value from input.

array2从输入中获取值时,您永远不会更新索引。

Try

尝试

scanf("%d", &array2[i]);

As for comparing, you can also use memcmpto compare memory:

至于比较,也可以使用memcmp比较内存:

memcmp(array1, array2, sizeof(array1));

回答by mathematician1975

Arrays use zero based indices for a start. You are incorrectly populating array2so you probably want to change your first loop to the following

数组使用基于零的索引作为开始。您的填充不正确,array2因此您可能希望将第一个循环更改为以下内容

    for (i=0;i<10;i++) 
    {
            printf("Enter numbers: ");
            scanf("%d", &array2[i]);
    }

as your current code is simply passing the address of array2as argument to scanf.

因为您当前的代码只是将array2as 参数的地址传递给scanf.

Then change the second loop conditional to

然后将第二个循环条件改为

 for (i=0;i<10;i++) 

in your comparison loop so that you do not access items beyond your array boundaries.

在您的比较循环中,以便您不会访问超出数组边界的项目。

Currently your second loop is accessing items at indices 0 to 10 - but there are only 10 items present in array1so you have undefined behaviour with your current code.

目前,您的第二个循环正在访问索引 0 到 10 处的项目 - 但其中只有 10 个项目,array1因此您当前的代码存在未定义的行为。

回答by Enamul Hassan

#include <stdio.h>
int main(void) {

        int array1[] = {1,2,3,4,5,6,7,8,9,10};
        int array2[10];
        int i;

        for (i=0;i<10;i++) { //fixed the range here

                printf("Enter numbers: ");
                scanf("%d", &array2[i]); //fixed the indexing
        }

        for (i=0;i<10;i++) { //fixed the range here

                if (array1[i] != array2[i]) {

                        printf("Not equal \n");
                }
                else {
                        printf("They are equal. \n");
                }
        }
}

回答by raju_singha

I am a beginner and I have this idea about comparing two arrays.Hope It might help someone like me.

我是初学者,我有比较两个数组的想法。希望它可以帮助像我这样的人。

/***compare two array: all elements are same or not(if not sorted 
 )***/
#include<stdio.h>

int main()
{
int n;
scanf("%d", &n);

int array1[n], array2[n];
int i, j;

for(i=0; i<n; i++)
{
    scanf("%d", &array1[i]);
}

for(i=0; i<n; i++)
{
    scanf("%d", &array2[i]);
}

int flg=0;
for(i = 0; i < n; i++)
{
    for(j=0; j<n; j++)
    {
        if(array1[i] == array2[j])
        {
            flg += 1;
            break;
        }
    }
}
if(flg == n)
{
    printf("All The Elements of array1 is present in array2... :)");
}
else
{
    printf("All THe Elements of array1 is not present in array2 :(");
}
return 0;

}

}

回答by Goma Joaquim

I am trying to respond to the answer, even though I am a beginner to C program.

尽管我是 C 程序的初学者,但我正在尝试回应答案。

According to your program written above, you are inputting and holding values to int array2[10]which has 11 elements.

根据您上面编写的程序,您正在输入和保存int array2[10]具有 11 个元素的值。

Remember that the first element of this array is indexed by zero. Ex: array2[0], until it reaches the last element which is array2[10], you have counted 11.

请记住,该数组的第一个元素的索引为零。例如:array2[0],直到它到达最后一个元素,即array2[10],您已经数了 11。

Now array1has the pre-defined values write which will be compared to your input values. Enter and store your values into array2[].

现在array1有预定义的值写入,将与您的输入值进行比较。输入您的值并将其存储到array2[].

#include <stdio.h>

int main(void) {
    int array1[] = {1,2,3,4,5,6,7,8,9,10};
    int array2[10];
    int i;

    for (i=0;i<10;i++) { //fixed the range here
        printf("Enter numbers: ");
        scanf("%d", &array2[i]); //fixed the indexing

        if (array1[i] != array2[i]) {
            printf("Not equal \n");
        }
        else {
            printf("They are equal. \n");
        }
    }
}