Thread: searching array for matches

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002

    searching array for matches

    hey all, i'm having troube searching this array to find identical matches. i though i had the logic figured out, but it's not working. thanks for your help.

    #include <iostream.h>
    #define draw 5
    void f_pair (const char *face_card[]);
    const char *face_card [5]={"1", "2", "3", "1", "5"};
    return (0);
    void f_pair (const char *face_card[])
    {	int j=1;
    	for (int i=0; i<draw-1; i++)
    		for (j+=i; j<draw;++j)
    		{	 if (face_card[i]==face_card[j])
    			{	cout<<"you matched: "<<face_card[i]<<endl<<endl;
    				cout<<"no match"<<endl;

  2. #2

    a couple problems.

    for (j+=i; j<draw;++j)

    Will not work. The first time through the loop, it will work, yes, because j=1, so adding i would give you i+1. But, the second time through the loop, j=draw. So j+=i would give you draw+i.

    Instead, consider:

    for (j=i+1; j<draw;++j)

    Secondly, this will not work as intented:

    if (face_card[i]==face_card[j])

    What you are doing are comparing locations in memory of the strings, instead of the actual strings. For instance:

    int b[3] = {1, 2, 3};
    int a[3] = {1, 2, 3};

    if(a == b) cout << "same" << endl; else cout << "not same" << endl;

    The arrays have the same elements, but they are in different locations in memory.

    Since the strings are all one character long, comparing the first character of each string would work:

    if (*face_card[i]==*face_card[j])

    Or, what would probably be prefered, would be to use strcmp, in case you wanted to extend the strings to hold more than one character:

    if (strcmp(face_card[i], face_card[j])==0)

    You'll need to include <string.h>

    Why are you using strings anyway? Why not an integer array?

    Finally, I'm not quite sure what you are attempting with:

    cout << "no match" << endl;

    Do you want to check to see if there are no matches at all in the array? Or for each number, see if there are any matches to that number or not?

    To do the first one (no matches at all in the array), you would want to keep track of whether or not:

    cout<<"you matched: "<<face_card[i]<<endl<<endl;

    was printed. If it isn't printed at all, then display "no match". Use a variable to do this:

    if (strcmp(face_card[i], face_card[j])==0)
    cout << "you matched: " << face_card[i] << endl;

    You would of course want to initalize matches to 0 in the variable declaration. And then, at the end of your function, you can do:

    if(matches==0) cout << "No match" << endl;

    I hope this helps.

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    hey, thanks a lot, that really hit the spot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. searching thru a c++ class array
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2002, 09:15 PM