943,524 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 35946
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 4th, 2004
0

Comparing arrays

Expand Post »
How do I compare what is in one array with what is in another array?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
coolmel55 is offline Offline
40 posts
since Sep 2004
Oct 4th, 2004
0

Re: Comparing arrays

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 4th, 2004
0

Re: Comparing arrays

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 4th, 2004
0

Re: Comparing arrays

Quote originally posted by Narue ...
>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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 5th, 2004
0

Re: Comparing arrays

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[i]; } /* give the elements of the first vector */
for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004
Oct 5th, 2004
0

Re: Comparing arrays

Quote originally posted by djextazy ...
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[i]; } /* give the elements of the first vector */
for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lara_ is offline Offline
17 posts
since Jul 2004
Oct 5th, 2004
1

Re: Comparing arrays

>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:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ( void )
  5. {
  6. static int a = 10;
  7. static int b = 20;
  8. static int c = 10;
  9. static int d = 20;
  10. int *aa[2] = { &a, &b };
  11. int *ab[2] = { &c, &d };
  12.  
  13. if ( memcmp ( aa, ab, 2 * sizeof ( int ) ) == 0 )
  14. puts ( "Equal" );
  15. else
  16. puts ( "Not equal" );
  17.  
  18. return 0;
  19. }
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[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...
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 5th, 2004
0

Re: Comparing arrays

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2.  
  3. void main()
  4. { int a[50],b[50],i,n,g;
  5.  
  6. cout<<"Give the number of elements of both vectors: "; cin>>n;
  7.  
  8. for(i=1;i<=n;i++) { cout<<"a["<<i<<"]= "; cin>>a[i]; }
  9.  
  10. for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>b[i]; }
  11.  
  12. g=1;
  13.  
  14. while(g==1)
  15. {
  16. for(i=1;i<=n;i++)
  17.  
  18. if(a[i]==b[i])
  19. g=1;
  20.  
  21. else {
  22. g=0;
  23. break;
  24. }
  25.  
  26.  
  27. if(g==0)
  28. break;
  29. g++;
  30. }
  31.  
  32. if(g==0)
  33. cout<<"The arrays aren't equal!";
  34.  
  35. else
  36. cout<<"The arrays are equal!";
  37. }

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

i never use memcmp before, thanks for the example you gave...!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lara_ is offline Offline
17 posts
since Jul 2004
Oct 5th, 2004
0

Re: Comparing arrays

>i just learn c++ programming. so i only can get this...
Then don't accept poor code. Try this for the comparison instead:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. bool is_equal ( int a[], int b[], int size )
  4. {
  5. for ( int i = 0; i < size; i++ ) {
  6. if ( a[i] != b[i] )
  7. return false;
  8. }
  9.  
  10. return true;
  11. }
  12.  
  13. int main()
  14. {
  15. int a[] = {1,2,3,4,5,6,7,8,9};
  16. int b[] = {1,2,3,4,5,6,7,8,9};
  17.  
  18. if ( is_equal ( a, b, 9 ) )
  19. std::cout<<"They're equal"<<std::endl;
  20. else
  21. std::cout<<"They're not equal"<<std::endl;
  22. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 5th, 2004
0

Re: Comparing arrays

Quote originally posted by lara_ ...
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[i]; }
for(i=1;i<=n;i++) { cout<<"b["<<i<<"]= "; cin>>b[i]; }
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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djextazy is offline Offline
11 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Solving the greatest common divisor using C++ and the Euclid method
Next Thread in C++ Forum Timeline: Quick Dynamic Memory Allocation Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC