Please members of the forum, I need help please, I do not know codes to use to be able to create a set of elements and then decide if an element X, belongs or not to the set of elements. Thanks!
It is unclear to me what you want.
Here are the functions to search and compare memory contents.
Like...?
uint8_t
A[] = { 3, 7, 9, 1, 4 };
void setup()
{
Serial.begin(115200);
Check( 3 );
Check( 10 );
}//setup
void Check( uint8_t candidate )
{
Serial.print( candidate );
for( uint8_t i=0; i<(sizeof(A)/sizeof(A[0])); i++ )
{
if( candidate == A[i] )
{
Serial.println( "∈A" );
return;
}//if
}//for
Serial.println( "∉A" );
}//Check
void loop()
{
}//loop
Thank you very much, it helps me a lot.
I got confused something "uint8_t" but then I investigated and I could see that it is equal to "unsigned int"
uint8_t is equivalent to the byte data type. The unsigned int is uint16_t.
u is for unsigned
int for integer
8 for the number of bits used in the representation
_t is a notation to say it’s a data type (don’t use _t as a suffix for your custom types as POSIX standard says it’s reserved for possible future use. I often use it as a prefix)
This is a way to tell the compiler how many bytes you exactly need for your data regardless of the platform. For example an unsigned int would be 2 bytes on a UNO but 4 bytes on an ESP32.
The c++ spec lists all those here: Fixed width integer types (since C++11) - cppreference.com
Another way:
uint32_t A = bit(3) | bit(7) | bit(9) | bit(1) | bit(4);
void setup()
{
Serial.begin(115200);
Check( 3 );
Check( 10 );
}//setup
void Check( uint8_t candidate )
{
Serial.print( candidate );
if( bitRead(A, candidate) )
Serial.println( "∈A" );
else
Serial.println( "∉A" );
}//Check
void loop()
{
}//loop
smart, quick and memory efficient but may be need to clarify that this works only if your numbers are in [0, 31]
going for a uint64_t would work for numbers in [0, 63]
Thanks @J-M-L I forgot to mention that!
Unfortunately, bitRead() does not work for uint64_t. So instead:
if(A & (1ULL << candidate) )
but it is less readable/digestible for a beginner.
good point it won't work with a uint64_t
but not because of bitRead
It actually would work as it's written that way:
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
but what does not work is bit()
(and the other variants of bit operations) because they use 1UL
This one deserves a grade A+ because it shows that the student has rejected the inefficient linear search solutions in favour of a more efficient direct lookup.
but also that the student made an assumption on the values that are acceptable in the set.
Calling Check( 259 );
will result in the cumbersome message 3∈A
!!
Also with 3, 7, 9 , 1 and 4 we could even go for a uint16_t saving 2 precious bytes !
Indeed. Also, if the sample data is generally sparse, but has large ranges in it, then efficiency of data encoding becomes more important relative to efficiency of searching.
Say :
const char* A[] = { "3", "7, "9", "1099-86999", "765905", "4" }; // application specific range representation
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.