7

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.

#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.

4
  • What did you enter, what did you see, what did you expect to see? Commented Nov 19, 2015 at 0:17
  • 2
    memcmp is an option. Commented Nov 19, 2015 at 0:19
  • Also, you are writing to the same array2 index each time you get input. Commented Nov 19, 2015 at 0:20
  • You are accessing out of bounds - the arrays have size 10 so the loop condition should be i < 10, not i < 11. Also you meant &array2[i] in the scanf line. Commented Nov 19, 2015 at 0:55

5 Answers 5

8
scanf("%d", &array2);

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

Try

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

As for comparing, you can also use memcmp to compare memory:

memcmp(array1, array2, sizeof(array1));
Sign up to request clarification or add additional context in comments.

3 Comments

Care to add some specifics? Sure, bit padding could be a concern I suppose, but I've rarely seen it and not in a scenario like this.
OP is using C, C allows padding bits, which can cause an incorrect memcmp result. Since OP didn't specify that he is using a platform without padding bits, but posted the question in relation to the C Programming language. See title and tag. (Please use @ or I won't be reading your responses. )
3

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

    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.

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.

Comments

1
#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");
                }
        }
}

Comments

1

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

1 Comment

This does not work if there are duplicate elements in either array: for example if on array has n times the first element of the other, the diagnostic All The Elements of array1 is present in array2 will be incorrect.
0

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

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

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.

Now array1 has the pre-defined values write which will be compared to your input values. Enter and store your values into 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");
        }
    }
}

1 Comment

It may actually be more useful to do for (i=0;i<(sizeof(array1)/sizeof(int));i++) instead of for (i=0;i<10;i++)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.