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
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[]
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.
Summary: HELLO!! my problem is: ISO C++ forbids comparison between pointer and integer is the name of the problem.. here is my code- #include <iostream> #include <fstream> #include <string> using name...
Summary: Well. The first thing you must have understood is the way that C implements the arrays. Basically in C a pointer and an array is the same thing. A pointer is a 4bytes (or 2 sometimes) integer variable...
Summary: im at a bit of a loss here. I never had to work with strings, so forgive me if it is trivial: i want to have an array of strings in C++, for example: string array[10]; array[0] = "I dont"; array[1] =...