By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,907 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
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
  1. int matches (int draw[], int entry[])
  2. {
  3.      int noofmatches = 0;
  4.      for(int i=0;i<MAXSIZE;i++)
  5.      {
  6.          for(int j=0;j<MAXSIZE;j++)
  7.          {
  8.              if(draw[i]==entry[j])
  9.               noofmatches++;   
  10.          }
  11.  
  12.      }
  13.      return noofmatches;
  14. }
Any idea's about what is wrong with it?
Thanks
May 29 '07 #1
Share this Question
Share on Google+
12 Replies


AdrianH
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
  1. int matches (int draw[], int entry[])
  2. {
  3.      int noofmatches = 0;
  4.      for(int i=0;i<MAXSIZE;i++)
  5.      {
  6.          for(int j=0;j<MAXSIZE;j++)
  7.          {
  8.              if(draw[i]==entry[j])
  9.               noofmatches++;   
  10.          }
  11.  
  12.      }
  13.      return noofmatches;
  14. }
Any idea's about what is wrong with it?
Thanks
Nothing, it work fine. It counts the number of elements in draw that are also in entry. What were you expecting?

BTW, this is not exactly safe to do. You have to be absolutely sure that you are passing an arrays that has only MAXSIZE or more elements in it.

If you are happy with just MAXSIZE elements in them, try using the following signature:

Expand|Select|Wrap|Line Numbers
  1. int matches (int (&draw)[MAXSIZE], int (&entry)[MAXSIZE])
Otherwise, I'd use something like a vector.

Adrian
May 29 '07 #2

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
  1. cout << "Please enter the six numbers of the Draw: " << endl;
  2.     for (int i=0; i<MAXSIZE; i++)
  3.     {
  4.        cin >> draw[MAXSIZE];
  5.     }
  6.     cout << "Please enter the six numbers of your entry: " << endl;
  7.     for (int i=0; i<MAXSIZE; i++)
  8.     {
  9.        cin >> entry[MAXSIZE];
  10.     }
  11.  
  12.       cout << matches(draw, entry) << endl;                         
  13.  
Is there something wrong with this?
May 29 '07 #3

AdrianH
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
  1. cout << "Please enter the six numbers of the Draw: " << endl;
  2.     for (int i=0; i<MAXSIZE; i++)
  3.     {
  4.        cin >> draw[MAXSIZE];
  5.     }
  6.     cout << "Please enter the six numbers of your entry: " << endl;
  7.     for (int i=0; i<MAXSIZE; i++)
  8.     {
  9.        cin >> entry[MAXSIZE];
  10.     }
  11.  
  12.       cout << matches(draw, entry) << endl;                         
  13.  
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

AdrianH
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
  1. for (int i=0; i<MAXSIZE; i++)
  2.     {
  3.        cin >> draw[MAXSIZE];
  4.     }
  5.  
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
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAXSIZE = 6;
  5. int matches (int draw[], int entry[]);
  6.  
  7. int main()
  8. {
  9.     int draw[MAXSIZE];
  10.     int entry[MAXSIZE];
  11.  
  12.     cout << "Please enter the six numbers of the Draw: " << endl;
  13.     for (int i=0; i<MAXSIZE; i++)
  14.     {
  15.        cin >> draw[i];
  16.     }
  17.     cout << "Please enter the six numbers of your entry: " << endl;
  18.     for (int i=0; i<MAXSIZE; i++)
  19.     {
  20.        cin >> entry[i];
  21.     }
  22.  
  23.       cout << matches(draw, entry) << endl;                         
  24.  
  25.  
  26.     system("pause");
  27.     return 0;
  28. }// end of main
  29.  
  30.  
  31.  
  32. int matches (int draw[], int entry[])
  33. {
  34.      int noofmatches = 0;
  35.      for(int i=0;i<MAXSIZE;i++)
  36.      {
  37.          for(int j=i+1;j<MAXSIZE;j++)
  38.          {
  39.              if(draw[i]==entry[j]&& i!=j)
  40.              noofmatches++;
  41.  
  42.          }
  43.  
  44.      }
  45.      return noofmatches;
  46. }
Can you see anything that is wrong?
Thanks again
May 29 '07 #9

AdrianH
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
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAXSIZE = 6;
  5. int matches (int draw[], int entry[]);
  6.  
  7. int main()
  8. {
  9.     int draw[MAXSIZE];
  10.     int entry[MAXSIZE];
  11.  
  12.     cout << "Please enter the six numbers of the Draw: " << endl;
  13.     for (int i=0; i<MAXSIZE; i++)
  14.     {
  15.        cin >> draw[i];
  16.     }
  17.     cout << "Please enter the six numbers of your entry: " << endl;
  18.     for (int i=0; i<MAXSIZE; i++)
  19.     {
  20.        cin >> entry[i];
  21.     }
  22.  
  23.       cout << matches(draw, entry) << endl;                         
  24.  
  25.  
  26.     system("pause");
  27.     return 0;
  28. }// end of main
  29.  
  30.  
  31.  
  32. int matches (int draw[], int entry[])
  33. {
  34.      int noofmatches = 0;
  35.      for(int i=0;i<MAXSIZE;i++)
  36.      {
  37.          for(int j=i+1;j<MAXSIZE;j++)
  38.          {
  39.              if(draw[i]==entry[j]&& i!=j)
  40.              noofmatches++;
  41.  
  42.          }
  43.  
  44.      }
  45.      return noofmatches;
  46. }
Can you see anything that is wrong?
Thanks again
Try using your original matches() function.

EDIT: Also don't use system("pause"), it is not standard, use cin.ignore(). It will wait for you to press enter.


Adrian
May 29 '07 #10

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
  1. int numbers (int draw[], int entry[])
  2. {
  3.     int samenumbers = 0;
  4.     for(int i=0; i<MAXSIZE; i++)
  5.     {
  6.         for(int j=0; j<MAXSIZE; j++)
  7.         {
  8.             if(draw[i]==entry[j])
  9.             samenumbers = draw[i];
  10.             break;
  11.  
  12.         }
  13.  
  14.     }
  15.     return samenumbers;
  16.  
  17. }
Any ideas?
Thanks
May 29 '07 #12

AdrianH
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
  1. int numbers (int draw[], int entry[])
  2. {
  3.     int samenumbers = 0;
  4.     for(int i=0; i<MAXSIZE; i++)
  5.     {
  6.         for(int j=0; j<MAXSIZE; j++)
  7.         {
  8.             if(draw[i]==entry[j])
  9.             samenumbers = draw[i];
  10.             break;
  11.  
  12.         }
  13.  
  14.     }
  15.     return samenumbers;
  16.  
  17. }
Any ideas?
Thanks
Please restate, your question is too vague.


Adrian
May 29 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.