Computing.Net > Forums > Programming > C++ array comparison

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ array comparison

Reply to Message Icon

Name: spoonman184
Date: May 20, 2008 at 04:26:17 Pacific
OS: Windows Vista
CPU/Ram: AMD X2 4400+ 2 GB DD
Product: ACER
Comment:

I am having some problem comparing two arrays in C++. I would like to be able to compare the two to see if they have any elements that match.
Example:
block[7] has elements ajk87e3g
guess[7] has random elements
I would like to compare them to see how many elements from guess[] are the same as the elements from block[]

crappy OEM comptuers...



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: May 22, 2008 at 14:16:35 Pacific
Reply:

Well, this typically done in one of two ways.

1) Just iterate though one array, looking for the contents in another.

for (int i = 0; i < blockMax; ++i) {
for (int j = 0; j < guessMax; ++j) {
if (block[i] == guess[j]) {
//Match found
}
}
}
2) Sort one array first, then iterate through the other array. You would be looking for either a match (hit!), or the sorted array's elements > comparing array's elements (miss!). For the sorting, I'd recommend quick sort. C++ has a sort() function, but you would need to use vectors, not arrays.

0
Reply to Message Icon

Related Posts

See More


Auto Join Monitoring email traffic



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ array comparison

ISO C++ forbids comparison.... www.computing.net/answers/programming/iso-c-forbids-comparison/14016.html

C++ Array Pointer Parameter www.computing.net/answers/programming/c-array-pointer-parameter/6482.html

C++ Arrays (Simple Q) www.computing.net/answers/programming/c-arrays-simple-q/4274.html