954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comparing arrays

How do I compare what is in one array with what is in another array?

coolmel55
Light Poster
40 posts since Sep 2004
Reputation Points: 10
Solved Threads: 1
 

Loop through all array elements and compare each element; if all elements are the same, consider the arrays equal. Or use memcmp , which essentially does this.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>Or use memcmp, which essentially does this.
memcmp uses a shallow byte-by-byte comparison, so depending on the type of items in the array this might not be a good idea.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>Or use memcmp, which essentially does this. memcmp uses a shallow byte-by-byte comparison, so depending on the type of items in the array this might not be a good idea.

I know what you are saying, but would you mind listing examples.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

It'll be something like this:

#include
#include
void main()
{ clrscr(); int a[50],b[50],i,n,g;
cout<<"Give the number of elements of both vectors: "; cin>>n;
for(i=1;i<=n;i++) { cout<<"a["<>a[i]; } /* give the elements of the first vector */
for(i=1;i<=n;i++) { cout<<"b["<>b[i]; } /* give the elements of the second vector */
g=1; // we presume that the vectors are equal
while(g==1)
{ g=0;
for(i=1;i<=n;i++) if(a[i]==b[i]) g=1; } /* if the number of the component is the same (i) and the value of the component is the same , then g=1 and the while { } repeats until it ends or finds unequal components */
if(g==1) cout<<"The arrays are equal!";
else cout<<"The arrays aren't equal!";
getch();
}

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

It'll be something like this:

#include #include void main() { clrscr(); int a[50],b[50],i,n,g; cout<<"Give the number of elements of both vectors: "; cin>>n; for(i=1;i<=n;i++) { cout<<"a["<>a[i]; } /* give the elements of the first vector */ for(i=1;i<=n;i++) { cout<<"b["<>b[i]; } /* give the elements of the second vector */ g=1; // we presume that the vectors are equal while(g==1) { g=0; for(i=1;i<=n;i++) if(a[i]==b[i]) g=1; } /* if the number of the component is the same (i) and the value of the component is the same , then g=1 and the while { } repeats until it ends or finds unequal components */ if(g==1) cout<<"The arrays are equal!"; else cout<<"The arrays aren't equal!"; getch(); }

have you test running your program?

lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

>I know what you are saying, but would you mind listing examples.
The simplest example is an array of pointers. Do you want to compare the value of the pointers or the value of the addresses pointed to:

#include <stdio.h>
#include <string.h>

int main ( void )
{
  static int a = 10;
  static int b = 20;
  static int c = 10;
  static int d = 20;
  int *aa[2] = { &a, &b };
  int *ab[2] = { &c, &d };

  if ( memcmp ( aa, ab, 2 * sizeof ( int ) ) == 0 )
    puts ( "Equal" );
  else
    puts ( "Not equal" );

  return 0;
}

Most likely it's the former, so memcmp would give the wrong answer unless by some coincidence both arrays all have pointers that reference the same addresses.

>It'll be something like this
No it wouldn't, that code is awful. The concept is sound, but the implementation sucks. Here are a few reasons why:

>#include
Not C++.

>#include
No portable.

>void main()
Undefined.

>clrscr();
VERY nonportable.

>cin>>n;
Not robust.

>for(i=1;i<=n;i++)
Poor style, not idiomatic.

>cin>>a[i];
Not robust, difficult to recover from on error.

>g=1;
>while(g==1)
>{ g=0;
Confused and awkward.

>getch();
Not portable and stupid because there's a standard alternative that works just as well.

And on top of that, you didn't use code tags...

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#include <iostream.h>

void main()
{	int a[50],b[50],i,n,g;
	
	cout<<"Give the number of elements of both vectors: "; cin>>n;
		
	for(i=1;i<=n;i++) { cout<<"a["<<i<<"]= "; cin>>a[i]; } 
	
	for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>b[i]; } 

	g=1;
	
	while(g==1)
	{			
		for(i=1;i<=n;i++) 
			
			if(a[i]==b[i]) 
				g=1;
			
			else {
				g=0;
				break;
			}
		

		if(g==0)
			break;
		g++;
	}  
		
	if(g==0)
		cout<<"The arrays aren't equal!";
	
	else 
		cout<<"The arrays are equal!";
}


i just learn c++ programming. so i only can get this...

i never use memcmp before, thanks for the example you gave...!

lara_
Newbie Poster
17 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

>i just learn c++ programming. so i only can get this...
Then don't accept poor code. Try this for the comparison instead:

#include <iostream>

bool is_equal ( int a[], int b[], int size )
{
  for ( int i = 0; i < size; i++ ) {
    if ( a[i] != b[i] )
      return false;
  }

  return true;
}

int main()
{
  int a[] = {1,2,3,4,5,6,7,8,9};
  int b[] = {1,2,3,4,5,6,7,8,9};

  if ( is_equal ( a, b, 9 ) )
    std::cout<<"They're equal"<<std::endl;
  else
    std::cout<<"They're not equal"<<std::endl;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
have you test running your program?


I apologize. That one I didn't test. Here is the correct version , tested :cheesy: :

#include
#include
using namespace std;
int main()
{ int a[50],b[50],i,j,n;
cout<<"Give the number of elements of both vectors: "; cin>>n;
for(i=1;i<=n;i++) { cout<<"a["<>a[i]; }
for(i=1;i<=n;i++) { cout<<"b["<>b[i]; }
i=0;
while(i

djextazy
Newbie Poster
11 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
template < typename T >
int CompareArrays(T* ArrayOne, T* ArrayTwo)
{
    // Create the length variable for both arrays
    int LengthOne = 0, LengthTwo = 0;

    // Do this in in local block so we can destroy both copy pointers after using them
    {
    // Copy both arrays (this way we won't manipulate the addres of both pointers)
    T* CopyOne = ArrayOne;
    T* CopyTwo = ArrayTwo;

    // Get the length of both arrays
    while (*CopyOne++) LengthOne++;
    while (*CopyTwo++) LengthTwo++;
    }

    // Check if the length numbers are equal
    if (LengthTwo != LengthOne)
    return -2;

    // Now Check for unequal elements
    for (int index = 0;index<LengthOne;index++)
    {
        // Test for equivalence
        if (*ArrayOne != *ArrayTwo)
        return -1;

        // Move one step forward
        ArrayOne++;ArrayTwo++;
    }

    // At this point we can make sure that both arrays are equal
    return 0;
}

int main ()
{

    char teststring[]  ="Hallo";
    char teststring2[] ="Hallo";

    int Check = CompareArrays(teststring,teststring2);
    if (Check==0)
    cout << "equal";
    else
    if (Check==-2)
    cout << "nah, one array has more elements than the other one";
    else
    cout << "I bet there is no equivalence";
    return 0;
}
Dman01
Light Poster
25 posts since Aug 2010
Reputation Points: 20
Solved Threads: 3
 

Assuming that the actual question meant to ask whether each element of one array is equal to the corresponding element of the other, the first observation is that in order for them to be equal, they must have the same number of elements, which I'll call n . Then

std::equal(array1, array1+n, array2)

returns true if the two arrays are equal and false otherwise.

If that's not the question that the original poster had in mind, clarification would be appreciated.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You