473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Comparing Arrays

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
12 1801
AdrianH
1,251 Expert 1GB
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
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
1,251 Expert 1GB
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
scruggsy
147 100+
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
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
1,251 Expert 1GB
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
scruggsy
147 100+
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
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
1,251 Expert 1GB
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
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
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
1,251 Expert 1GB
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

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
1
by: Donald Grove | last post by:
If I have two arrays, what is a good paradigm for comparing what is in them, to determine what elements they share, or don't share? Specifically, each array could potentially contain the integers...
4
by: eoghan.kenny | last post by:
Hi, I need to compare two timestamp columns in sql server and see which one is greater. (i can tell if they are not equal buts not enough for this requirement). A timestamp value is unique in...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
11
by: Sheldon | last post by:
Hi, I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.