455,907 Members | 1,364 Online
Need help? Post your question and get tips & solutions from a community of 455,907 IT Pros & Developers. It's quick & easy.

# Comparing Arrays

 P: 35 Hi, I need to write a code that compares two arrays (draw and entry) and see if there are any matching numbers in them. I have tried a few different things and I don't understand why this doesn't work. Here is my code Expand|Select|Wrap|Line Numbers int matches (int draw[], int entry[]) {      int noofmatches = 0;      for(int i=0;i
12 Replies

 Expert 100+ P: 1,251 Hi, I need to write a code that compares two arrays (draw and entry) and see if there are any matching numbers in them. I have tried a few different things and I don't understand why this doesn't work. Here is my code Expand|Select|Wrap|Line Numbers int matches (int draw[], int entry[]) {      int noofmatches = 0;      for(int i=0;i

 P: 35 Oh really, it isn't working for me. Maybe it's my output and input... maxsize is fine as it is always going to be the same size and i declared it as a constant. This is the in/output.... Expand|Select|Wrap|Line Numbers cout << "Please enter the six numbers of the Draw: " << endl;     for (int i=0; i> draw[MAXSIZE];     }     cout << "Please enter the six numbers of your entry: " << endl;     for (int i=0; i> entry[MAXSIZE];     }         cout << matches(draw, entry) << endl;                            Is there something wrong with this? May 29 '07 #3

 Expert 100+ P: 1,251 Oh really, it isn't working for me. Maybe it's my output and input... maxsize is fine as it is always going to be the same size and i declared it as a constant. This is the in/output.... Expand|Select|Wrap|Line Numbers cout << "Please enter the six numbers of the Draw: " << endl;     for (int i=0; i> draw[MAXSIZE];     }     cout << "Please enter the six numbers of your entry: " << endl;     for (int i=0; i> entry[MAXSIZE];     }         cout << matches(draw, entry) << endl;                            Is there something wrong with this? Yes, what element are you putting your input into? Adrian May 29 '07 #4

 100+ P: 147 Oh really, it isn't working for me. Maybe it's my output and input... It could be your test input. If either the first or second batch of numbers contains duplicate elements, your noOfMatches is going to be higher than you expect. If that doesn't help, try to be define "isn't working" more specifically. EDIT: Oops, Adrian spotted the more serious problem. :) But you still have an issue with non-unique elements. May 29 '07 #5

 P: 35 I'm not quite sure what you mean by element but I am putting 6 numbers into each array. So six into entry[] and six into draw[]. May 29 '07 #6

 Expert 100+ P: 1,251 I'm not quite sure what you mean by element but I am putting 6 numbers into each array. So six into entry[] and six into draw[]. Look at line 4. What index are you using? Same goes for line 9. Adrian May 29 '07 #7

 100+ P: 147 I'm not quite sure what you mean by element but I am putting 6 numbers into each array. So six into entry[] and six into draw[]. The 6 numbers make up the 6 elements of each array. The problem with your input-reading code is here: Expand|Select|Wrap|Line Numbers for (int i=0; i> draw[MAXSIZE];     }   What you want to do is store one number in each element (from 0 to MAXSIZE - 1) of your array. What you're actually doing is storing each input into draw[MAXSIZE], so each successive input overwrites the previous one, and they are all written into memory outside of your array. Replace cin >> draw[MAXSIZE] with cin >> draw[i]. May 29 '07 #8

 P: 35 Thanks for that, that does make sense but it still doesn't seem to be working. I keep getting zero for the number of matches. So here is the code all together.... Expand|Select|Wrap|Line Numbers #include  using namespace std;   const int MAXSIZE = 6; int matches (int draw[], int entry[]);   int main() {     int draw[MAXSIZE];     int entry[MAXSIZE];       cout << "Please enter the six numbers of the Draw: " << endl;     for (int i=0; i> draw[i];     }     cout << "Please enter the six numbers of your entry: " << endl;     for (int i=0; i> entry[i];     }         cout << matches(draw, entry) << endl;                                  system("pause");     return 0; }// end of main       int matches (int draw[], int entry[]) {      int noofmatches = 0;      for(int i=0;i

 Expert 100+ P: 1,251 Thanks for that, that does make sense but it still doesn't seem to be working. I keep getting zero for the number of matches. So here is the code all together.... Expand|Select|Wrap|Line Numbers #include  using namespace std;   const int MAXSIZE = 6; int matches (int draw[], int entry[]);   int main() {     int draw[MAXSIZE];     int entry[MAXSIZE];       cout << "Please enter the six numbers of the Draw: " << endl;     for (int i=0; i> draw[i];     }     cout << "Please enter the six numbers of your entry: " << endl;     for (int i=0; i> entry[i];     }         cout << matches(draw, entry) << endl;                                  system("pause");     return 0; }// end of main       int matches (int draw[], int entry[]) {      int noofmatches = 0;      for(int i=0;i

 P: 35 Ok it finally works!!! Thankyou sooo much. I just changes it that many times I didn't realise I hadn't tried changing it back! Thanks again. May 29 '07 #11

 P: 35 Hi again, Now I need to output the numbers that match and nothing i've tried works. This is what i've got so far... Expand|Select|Wrap|Line Numbers int numbers (int draw[], int entry[]) {     int samenumbers = 0;     for(int i=0; i

 Expert 100+ P: 1,251 Hi again, Now I need to output the numbers that match and nothing i've tried works. This is what i've got so far... Expand|Select|Wrap|Line Numbers int numbers (int draw[], int entry[]) {     int samenumbers = 0;     for(int i=0; i