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

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.

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

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

It'll be something like this:

#include <iostream.h>
#include <conio.h>
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["<<i<<"]= "; cin>>a; } /* give the elements of the first vector */
for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>b; } /* 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==b) 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();
}

It'll be something like this:

#include <iostream.h>
#include <conio.h>
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["<<i<<"]= "; cin>>a; } /* give the elements of the first vector */
for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>b; } /* 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==b) 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?

>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 <iostream.h>
Not C++.

>#include <conio.h>
No portable.

>void main()
Undefined.

>clrscr();
VERY nonportable.

>cin>>n;
Not robust.

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

>cin>>a;
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...

commented: It's good to know there's an uncompromising critic out there-- Asif_NSU +1
#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...!

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

have you test running your program?

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

#include <iostream>
#include <conio.h>
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["<<i<<"]= "; cin>>a; }
for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>b; }
i=0;
while(i<n) for(j=1;j<=n;j++) if(a[j]==b[j]) i++;
if(i==n) cout<<"The arrays are equal!";
else cout<<"The arrays aren't equal!";
getch();
}

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.