6

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Hassen Fatima
  • 401
  • 3
  • 7
  • 12

5 Answers5

4
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));
Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • 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. – Inisheer Nov 19 '15 at 01:25
  • http://stackoverflow.com/questions/9120065/best-way-to-compare-two-int-arrays-of-the-same-length – Inisheer Nov 19 '15 at 01:25
  • 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. ) – this Nov 19 '15 at 16:20
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.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
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");
                }
        }
}
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
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;
}
Om Choudhary
  • 492
  • 1
  • 7
  • 27
  • 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. – chqrlie Aug 18 '20 at 18:08
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");
        }
    }
}
Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
  • 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++)` – Tibrogargan Jul 09 '16 at 07:33