Compare arrays.,.,.,.,.,

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

looping through array is fast in c++ why do you not want to do it this way? Abort loop on first mismatch, you will go through all arrays in no time

Take a view here:

https://cplusplus.com/reference/cstring/memcmp/

No.

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. :expressionless:

a7

Do the arrays have to hold values of 0 and 1 ?

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...

No teacher I'm a hobbyist, but thanks for the idea

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.

Thank you all for the help

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).

I used binary and the 8th bit is just allways zero when converting
For converting there are two methods that i know of the nice way and the easy way

The easy way is to just multiply every bit with the corresponding multiple of 2

Could you show the code plz?

Sure it's part of a library I'm working on for decoding numbers from the signal sent to 7 segment displays

#include "7SegDec.h"
bool 7segdec_lib_invert;
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;
int 7segdec_lib_pins[7];
bool 7segdec_lib_segments[7];
int 7segdec_lib_result;
void (int segmentPins[7])
{
	for(int i = 0; i < 7; i++)
	{
		7segdec_lid_pins[i] = segmentPins[i];
	}
}
int readDigit()
{
	for(int i = 0; i < 7; i++)
	{
		if(7segdec_lib_invert)
		{
			7segdec_lib_segments[i] = !digitalRead(7segdec_lib_pins[i]);
		}
		else
		{
			7segdec_lib_segments[i] = digitalRead(7segdec_lib_pins[i]);
		}
		
	}
	int 7segdec_lib_decseg = 7segdec_lib_segments[6]*1 + 7segdec_lib_segments[5]*2 + 7segdec_lib_segments[4]*4 + 7segdec_lib_segments[3]*8 + 7segdec_lib_segments[2]*16 + 7segdec_lib_segments[1]*32 + 7segdec_lib_segments[0]*64;
	switch(7segdec_lib_decseg)
	{
		case 7segdec_lib_n0:
		{
			7segdec_lib_result = 0;
			break;
		}
		case 7segdec_lib_n1:
		{
			7segdec_lib_result = 1;
			break;
		}
		case 7segdec_lib_n2:
		{
			7segdec_lib_result = 2;
			break;
		}
		case 7segdec_lib_n3:
		{
			7segdec_lib_result = 3;
			break;
		}
		case 7segdec_lib_n4:
		{
			7segdec_lib_result = 4;
			break;
		}
		case 7segdec_lib_n5:
		{
			7segdec_lib_result = 5;
			break;
		}
		case 7segdec_lib_n6:
		{
			7segdec_lib_result = 6;
			break;
		}
		case 7segdec_lib_n7:
		{
			7segdec_lib_result = 7;
			break;
		}
		case 7segdec_lib_n8:
		{
			7segdec_lib_result = 8;
			break;
		}
		case 7segdec_lib_n9:
		{
			7segdec_lib_result = 9;
			break;
		}
		default:
		{
			result = 100;
		}
	}
	return result;
}
void begin()
{
	for(int i = 0; i < 7; i++)
	{
		pinMode(7segdec_lib_pins[i], INPUT);
	}
}
bool activeHigh(bool activeHigh)
{
	7segdec_lib_invert = activeHigh;
}

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;

Looks like this is crying out to be an array

Yeah, this code is an earlier revision
You know when you just try to get it working and don't care about preformance

Names like 7segdec_lib_n0 also don't work in C++ since an identifier cannot begin with a digit.

Thank you for pointing out the problem I havent yet uploaded the code so I didn't know

(why does this sound like customer support?)

Is undefined in all cases but default, would be surprised if compiled

it was meant to be 7segdec_lib_result, but that still wouldn't compile because it starts with a number

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.