By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,446 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,446 IT Pros & Developers. It's quick & easy.

Looping with 2 dimensional arrays

P: 35
hi, i need a program that takes an array of 6 numbers and compares these numbers to a two dimensional array of n columns (user decides a number between 1 and 18) and 6 rows. So i need to check if each of the rows in the 2 dimensional array have matching numbers to the original array....
Hope that makes sense.
So far I have a function that kind of does this but it wont switch to the next row without including the first one so each result includes the result of all the rows before it. So I get..
6 12 18 as a result instead of...
6 6 6

Here's my function....


Expand|Select|Wrap|Line Numbers
  1. void results (int draw[], int entry[][maxsize2], int size)
  2. {
  3.     int matches = 0;
  4.     for (int h=0; h<size; h++)
  5.     {
  6.       for (int i=0; i<MAXSIZE; i++)
  7.       {
  8.         for (int j=0; j<MAXSIZE; j++)
  9.         {
  10.            if (draw[i] == entry[j][maxsize2])
  11.            matches++;  
  12.         } 
  13.       }
  14.       cout << matches << " ";
  15.     }
  16.     cout << endl;
  17. }
Any ideas about how to move to the next row?
Thanks
May 30 '07 #1
Share this Question
Share on Google+
4 Replies


100+
P: 147
hi, i need a program that takes an array of 6 numbers and compares these numbers to a two dimensional array of n columns (user decides a number between 1 and 18) and 6 rows. So i need to check if each of the rows in the 2 dimensional array have matching numbers to the original array....
Hope that makes sense.
So far I have a function that kind of does this but it wont switch to the next row without including the first one so each result includes the result of all the rows before it. So I get..
6 12 18 as a result instead of...
6 6 6

Here's my function....


Expand|Select|Wrap|Line Numbers
  1. void results (int draw[], int entry[][maxsize2], int size)
  2. {
  3.     int matches = 0;
  4.     for (int h=0; h<size; h++)
  5.     {
  6.       for (int i=0; i<MAXSIZE; i++)
  7.       {
  8.         for (int j=0; j<MAXSIZE; j++)
  9.         {
  10.            if (draw[i] == entry[j][maxsize2])
  11.            matches++;  
  12.         } 
  13.       }
  14.       cout << matches << " ";
  15.     }
  16.     cout << endl;
  17. }
Any ideas about how to move to the next row?
Thanks
I'm not sure what's up with maxsize2, but it looks like you just need to reset matches to 0 after each run of the innermost loop (following the cout statement).
May 30 '07 #2

P: 35
Thanks, that makes sense but for some reason it works if all the numbers in all the rows are equal (e.g. 1 2 3 4 5 6 in all of them) but otherwise it just gives me 1 as the answer for all the rows.
MAXSIZE2 is the number of column which is determined by the user so I wasn't sure how else to include it in the function.
May 30 '07 #3

100+
P: 147
Thanks, that makes sense but for some reason it works if all the numbers in all the rows are equal (e.g. 1 2 3 4 5 6 in all of them) but otherwise it just gives me 1 as the answer for all the rows.
MAXSIZE2 is the number of column which is determined by the user so I wasn't sure how else to include it in the function.
Okay, but are you sure that you always want to compare against the maxsize2th element, as below?
Expand|Select|Wrap|Line Numbers
  1. #
  2.         for (int j=0; j<MAXSIZE; j++)
  3.         {
  4.            if (draw[i] == entry[j][maxsize2])
  5.            matches++; 
  6.         } 
Or do you really mean to compare against all of entry[j][0] through entry[j][maxsize2 - 1]?

I get the impression that the nesting of your loops is a little different than what you want it to be. But you haven't explained what is contained in the rows and columns of entry[][], or the purpose of the size parameter, so I'm stil not sure what you're trying to accomplish in your function.
May 30 '07 #4

P: 35
Or do you really mean to compare against all of entry[j][0] through entry[j][maxsize2 - 1]?
Oh ok i see how that is wrong now and yes i want to do what you have suggested above but i'm just not sure how to put that in a statement compring the two values.

I get the impression that the nesting of your loops is a little different than what you want it to be. But you haven't explained what is contained in the rows and columns of entry[][], or the purpose of the size parameter, so I'm stil not sure what you're trying to accomplish in your function.
I'm comapring lotto numbers so the first array is the winning numbers and the 2 dimensional one is entries of the user. So they have 6 numbers in each entry which is the row and up to 18 entries which is the number of colums. But I have to make it so the user inputs the amount of columns which is the second part of the array. So its actually entry[6][what user enters]

MAXSIZE2 is up to 18
and size is what they actually enter
May 30 '07 #5

Post your reply

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