| | |
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.
"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
•
•
•
•
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.
"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
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
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();
}
#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();
}
•
•
•
•
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();
}
>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:
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...
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)
#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; }
>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.
C++ Syntax (Toggle Plain Text)
#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:
Then don't accept poor code. Try this for the comparison instead:
C++ Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by lara_
have you test running your program?
#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();
}
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: C++ Compiler???
- Next Thread: State abbreviations
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delayload delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






