Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,089 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




Comparing arrays

 
Reply to this topicStart new topic

Comparing arrays

mattman059
14 Oct, 2007 - 02:06 PM
Post #1

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 340


Dream Kudos: 175
My Contributions
Okay so I have an array with 3 "answers" in it:

CODE

answers[3] = {'y','n',y'};


and I also have an array that has the answers corresponding to certain items:

CODE

Chooser[3][3] = {{'y','y','y'}, {'n','y','y'}, {'y','n',y'}};


And what I want to do is compare the answers array to the chooser array, and when I get three matches in a row (like Chooser[0][1], Chooser[0][2], Chooser[0][3] , not [0][2],[0][3],[1][0]) I will have a function or statement that says "You have chosen: " and I'll assign a name to it.

This is kinda like an expert system so thats how its going to work, my problem comes in comparing the two arrays because i keep getting array indicies that are not in a row.

here is my code for comparing:
CODE

for(int i =0; i<3; i++)
        for(int j=0; j<3;j++)
       {
           if(Chooser[i][j] == answers[i])
              {
                    cout << "They match at: Chooser[" << i << "][" << j << "] and answers[" << i << "]" <<endl;
                    continue;
              }
           else
                     break;
        }



and as output I get
CODE

Chooser[0][0] and answers [0]
Chooser[0][1] and answers [0]
Chooser[0][2] and answers [0]

Chooser[1][0] and answers [1]

Chooser[2][0] and answers [2]



Any ideas why?
User is offlineProfile CardPM
+Quote Post

barnwillyb
RE: Comparing Arrays
14 Oct, 2007 - 02:28 PM
Post #2

D.I.C Head
**

Joined: 22 May, 2007
Posts: 55


My Contributions
QUOTE(mattman059 @ 14 Oct, 2007 - 03:06 PM) *

Okay so I have an array with 3 "answers" in it:

CODE

answers[3] = {'y','n',y'};


and I also have an array that has the answers corresponding to certain items:

CODE

Chooser[3][3] = {{'y','y','y'}, {'n','y','y'}, {'y','n',y'}};


And what I want to do is compare the answers array to the chooser array, and when I get three matches in a row (like Chooser[0][1], Chooser[0][2], Chooser[0][3] , not [0][2],[0][3],[1][0]) I will have a function or statement that says "You have chosen: " and I'll assign a name to it.

This is kinda like an expert system so thats how its going to work, my problem comes in comparing the two arrays because i keep getting array indicies that are not in a row.

here is my code for comparing:
CODE

for(int i =0; i<3; i++)
        for(int j=0; j<3;j++)
       {
           if(Chooser[i][j] == answers[i])
              {
                    cout << "They match at: Chooser[" << i << "][" << j << "] and answers[" << i << "]" <<endl;
                    continue;
              }
           else
                     break;
        }



and as output I get
CODE

Chooser[0][0] and answers [0]
Chooser[0][1] and answers [0]
Chooser[0][2] and answers [0]

Chooser[1][0] and answers [1]

Chooser[2][0] and answers [2]



Any ideas why?


Is this what you are after:

CODE
int main (int argc, char * const argv[]) {
    char answers[3] = {'y','n','y'};
    char Chooser[3][3] = {{'y','y','y'}, {'n','y','y'}, {'y','n','y'}};
    
    for(int i =0; i<3; i++)
        for(int j=0; j<3;j++)
        {
            if(Chooser[i][j] = answers[i])
            {
                cout << "They match at: Chooser[" << i << "][" << j << "] and answers[" << i << "]" <<endl;
                continue;
            }
            else
                break;
        }

    return 0;
}

User is offlineProfile CardPM
+Quote Post

mattman059
RE: Comparing Arrays
15 Oct, 2007 - 05:00 AM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 340


Dream Kudos: 175
My Contributions
no, i already have that much (Except for the int main(int arg ...) stuff..which i dont use) the arrays are not being compared properly i was asking if anyone knew what was wrong
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Comparing Arrays
15 Oct, 2007 - 10:38 AM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You're using the incorrect index into your answers array - you should be using the index variable from the inner loop, not the outer one (subbed r and c for i and j in the following):
if (Chooser[i][j] == answers[j])

The program will still produce an output for every individual match, but at least the matches will be correctly defined now. If you just want to output when there are three matches in a row, declare a counter variable and set it to zero between the control statements of your inner and outer loops, increment it each time there is a match, and produce the output AFTER the inner loop is completed, and only if the counter variable marks 3 successes:
CODE
for (int r =0; r<3; r++) {
    int counter=0
    for (int c=0; c<3; c++) {
        if (Chooser[r][c] == answers[c]) {
            counter++
            continue;
        } else
        break;
    }
    if (counter==3) {
        cout << "They match at row "<< r << " of Chooser"<< endl;
    }
}


Hope that helps,

-jjh

This post has been edited by jjhaag: 15 Oct, 2007 - 10:39 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:18PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month