Hi
Ihave an array (array1) with 7 numbers (0 or 1) and I have 10 other arrays that are constant and I need to find witch of the 10 arrays match array1 . Is ther any other methhod than scanning each and every number to compare?
EDIT: the title is like that because the minimum character limit is 15
If you could figure out or have the ten arrays in some kind of order, you could terminate your search early after seeing that the remaining arrays would always be "greater than", and therefore not match.
If the arrays are pretty much random you on your hands and knees comparing.
Don't worry, that's what computers are good at, there'll be no complaint.
@paulpaulson has good idea there, I wouldn't have thought of it because it's kinda like cheating.
Comparison would be much easier if you used the bits of an int variable instead to hold what is now in each constant array. Put those ints in an array and you could then iterate through that array and compare each of them with your test int
If you have 7 bits of data you can put them together into one byte. That would make the comparison 7 times as fast. If you can arrange for the bit patterns to be in numeric order you can do a binary search to speed up the search.
My guess is, your teacher phrased this test question this way to see which students would realize that the item being compared could be a byte, and therefore the items it's being compared to could also be bytes...
Final version:
convert the ten arrays (const) to decimal then convert the changing array to decimal then use a switch() to get what the array now converted to decimal matches and add a deafult case if none.
How did you convert the arrays to decimal? I ask because the "obvious" way (converting 7 '1's to decimal 1111111) requires at least 21 bits, or a long value. The more optimal way is to use binary so it can fit in a byte, as johnwasser suggested, e.g., 0b1111111, since each value can be one of only two possible values (a nice use for bits).
int 7segdec_lib_n0 = 63;
int 7segdec_lib_n1 = 48;
int 7segdec_lib_n2 = 109;
int 7segdec_lib_n3 = 121;
int 7segdec_lib_n4 = 51;
int 7segdec_lib_n5 = 91;
int 7segdec_lib_n6 = 95;
int 7segdec_lib_n7 = 112;
int 7segdec_lib_n8 = 127;
int 7segdec_lib_n9 = 123;