Comparing Array elements.

+1 Akhilesh Chobey · December 5, 2015
How do I compare two arrays?
For e.g. I have a constant array int f [4]={0,1,1,0};
and I have another input array of four elements.If the input is same as f i.e. 0,1,1,0.
I want to print true. how do i do that?

Thanks :)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Denis Balaj · December 5, 2015
Version 1:
#include <stdio.h>
int main(){

      int f [4]={0,1,1,0};
      int a[4];
      int i;

      printf("Enter 4 elements\n");
      for(i=0; i<4; i++){
            printf("Enter element %d: ",i+1);
            scanf(" %d", &a);

            if(f == a){
                  printf("True\n");
            }else{
                  printf("False\n");
            }

      }

      return 0;
}

Version 2:

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

      int f[4]={0,1,1,0};
      int a[4];
      int i;

      printf("Enter 4 elements\n");
      for(i=0; i<4; i++){
            printf("Enter element %d: ",i+1);
            scanf(" %d", &a);
      }

      if(f[0] == a[0] && f[1] == a[1] && f[2] == a[2] && f[3] == a[3]){
            printf("True\n");
      }else{
            printf("False\n");
      }

      return 0;
}
0 Akhilesh Chobey · December 5, 2015
Yup. V2 is subtle and perfect. Thanks for that. :)
0 Akhilesh Chobey · December 5, 2015
I think the version 1 is wrong. Because don't we have to compare the elements(i th element)of both arrays. Like you have done in version 2? 
0 Denis Balaj · December 5, 2015
What do you think about v2, do you mean to compare, if they are the same print true, or i other is greater than other to print true, check v2...
  • 1

C

107,249 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator