Comparing arrays

Reply

Join Date: Sep 2004
Posts: 40
Reputation: coolmel55 is an unknown quantity at this point 
Solved Threads: 1
coolmel55 coolmel55 is offline Offline
Light Poster

Comparing arrays

 
0
  #1
Oct 4th, 2004
How do I compare what is in one array with what is in another array?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Comparing arrays

 
0
  #2
Oct 4th, 2004
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Comparing arrays

 
0
  #3
Oct 4th, 2004
>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'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 228
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Comparing arrays

 
0
  #4
Oct 4th, 2004
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Comparing arrays

 
0
  #5
Oct 5th, 2004
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();
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 11
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

Re: Comparing arrays

 
0
  #6
Oct 5th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Comparing arrays

 
1
  #7
Oct 5th, 2004
>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:
  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...
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 11
Reputation: lara_ is an unknown quantity at this point 
Solved Threads: 0
lara_'s Avatar
lara_ lara_ is offline Offline
Newbie Poster

Re: Comparing arrays

 
0
  #8
Oct 5th, 2004
  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...!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Comparing arrays

 
0
  #9
Oct 5th, 2004
>i just learn c++ programming. so i only can get this...
Then don't accept poor code. Try this for the comparison instead:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 11
Reputation: djextazy is an unknown quantity at this point 
Solved Threads: 0
djextazy djextazy is offline Offline
Newbie Poster

Re: Comparing arrays

 
0
  #10
Oct 5th, 2004
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();
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC