简体   繁体   English

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

[英]How to compare two arrays in C programming language?

I want to compare two different arrays which are both int .我想比较两个不同的数组,它们都是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.第一个数组是静态的,包含 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.即使我输入的数字等于存储在第一个数组中的数字,程序也总是说不相等。

Arrays use zero based indices for a start.数组使用基于零的索引作为开始。 You are incorrectly populating array2 so 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 array2 as argument to scanf .因为您当前的代码只是将array2的地址作为参数传递给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 array1 so you have undefined behaviour with your current code.目前,您的第二个循环正在访问索引 0 到 10 处的项目 - 但array1中只有 10 个项目,因此您当前的代码存在未定义的行为。

scanf("%d", &array2);

You are never updating the index of array2 when getting a value from input.从输入中获取值时,您永远不会更新array2的索引。

Try尝试

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

As for comparing, you can also use memcmp to compare memory:至于比较,也可以使用memcmp来比较内存:

memcmp(array1, array2, sizeof(array1));
#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");
                }
        }
}

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;
}

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.根据您上面编写的程序,您正在向具有 11 个元素的int array2[10]输入和保存值。

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 array1 has the pre-defined values write which will be compared to your input values.现在array1有预定义的值 write 将与您的输入值进行比较。 Enter and store your values into array2[] .输入您的值并将其存储到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");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM