Testing to see if some value is an element of an Array

My goal is to write a piece of code using the array:

primes [] = {2,3,5,7};

If I press a button (n) times, how can I check if primes[n] exists?? (in other words, is "n" an element of the array)

my best guess...

if (n == primes[n])

I'm not interested in using arrays unless I can use figure this out, my first try at using them with arduino.

Thanks

You can use a loop to check each element of the array for a match.

Here is a link to some examples of testing for a prime so you can leave out the array of hard coded values: http://www.cplusplus.com/forum/general/1125/

Nick gammon has a prime number generator which may have an algorithm better suited for an Arduino ( search forum ).

primes = {2,3,5,7};

if (n == primes[n])

This only looks at the n’th element in the array. Push the button 3 times then look at the 3rd element.

You need to cycle through the array to check all elements.

For count = 0; count =< number of elements; increment count
If n = primes[count]
Found = true
Exit for loop

Weedpharma

Is this correct?

for (buttonCount = 1; count =< int elements; count++)
{
if (elements = primeSet[buttonCount])
{
digitalWrite(primeLED, HIGH);
digitalWrite(evenLED, LOW);
digitalWrite(oddLED, LOW);
Serial.println(“Prime”);
}

Is this correct?

for (buttonCount = 1; count =< int elements; count++)

No. All three clauses of the for loop should involve the same variable. Declaring a new, uninitialized variable in the termination clause doesn’t make sense, either.

And, the operator is <=, not =<.

My fault with =<!!

But the count must start at 0 or you miss the first element of the array.

Weedpharma

Ok, this is my code. It is not doing what I want. My goal is to be able to press the button some number of times. After I’ve pressed it say 3 times, the number 3 should light up the odd LED and prime LED. Another press of the button will make the buttonCount 4, this code should check to see if “4” is in the primeSet and if it is


int primeSet = {2,3,5,7};

int elements;
for (buttonCount = 0; (buttonCount <= elements); buttonCount++)
{
if (buttonCount = primeSet[buttonCount])
{


I want to use python language to do this easier.

for __________ in ___________:
{
}

is there a similar way?

You can write a function to do what you want like this.

int primeSet[] = {2,3,5,7};

void setup() 
{
  Serial.begin(115200);

  for (int buttonCount = 0; buttonCount < 10; buttonCount++)
  {
    Serial.print("Testing ");
    Serial.println(buttonCount);
    if (testPrimes(buttonCount))
    {
      Serial.println("In primeSet"); 
    }
    else
    {
      Serial.println("Not in primeSet"); 
    }
    Serial.println();
  }
}

void loop() 
{
}

boolean testPrimes(int test)
{
  for (int x = 0; x < 4; x++)
  {
    if (primeSet[x] == test)
    {
      return true;
    }    
  } 
  return false;
}

Obviously your buttonCount will not come from a for loop.

I think instead of trying to hack your way through, you might want to check out some tutorials on C++ and get a little familiarity with the form and the syntax. You keep trying to use the thing you’re comparing to also as the index to the array. Those should be different numbers.

int primeSet[] = {2,3,5,7,11};

int numberOfElements = 5;

int numberOfPushes;

boolean itWasPrime = false;


//  Code here to get the number of button pushes and populate that variable.



for(int i = 0; i < numberOfElements; i++){
   
   if (numberOfPushes == primeSet[i]){
          itWasPrime = true;
          break;   // can stop searching now that we've found it
   }

My goal is to be able to press the button some number of times.

Where are you storing that information?

for (buttonCount = 0; (buttonCount <= elements); buttonCount++)
  {

You like typing? Typically, for loop variables are one letter names.

if (buttonCount = primeSet[buttonCount])

You need to think about this. Why would need an array if the value in the array matched the index position? This code makes NO sense. Especially the part about ASSIGNING the value in the array to the loop index.

Try describing what you want to do in English, not in code, since you clearly don’t know how to describe it in code.

for (buttonCount = 0; (buttonCount <= elements); buttonCount++)
{
if (buttonCount == primeSet[buttonCount])
{

I assume you have defined elements as the number of elements in the array, as a constant.

Also a comparison requires double ==. Otherwise a is assigned the value b.

If (a ==b). Is a equal to b?

If (a=b). If a takes the value of b and it is non zero, ie, true

{
if (buttonCount = primeSet[buttonCount])
{

Does nothing even if you have the comparison right.

{
if (buttonCount == primeSet[buttonCount]);
Do something now;
{

Weedpharma

Does nothing even if you have the comparison right.

Sure it does. It assigns a value to buttonCount. The result of the assignment statement is then what the if statement is evaluating. The result of the assignment is the value assigned, so the statement is equivalent to:

buttonCount = primeSet[buttonCount];
if (buttonCount)

Which, clearly, does not make sense. But, it does something.

int arraySize = sizeof (primes) / sizeof(int);

for( int i=0; i< arraySize; i++)
  if ( primes[i] == n)
    {//found it
    }

//didn't find it

Clearly I should have said "does nothing of use"

Weedpharma

int elements;
  for (buttonCount = 0; (buttonCount <= elements); buttonCount++)
  {

You didn’t initialize “elements” anywhere. Other than that, the loop looks basically OK. Traditionally, it would look like:

for (index = 0; index < size; index++)

Note the < rather than <=; that because 0…n-1 is n elements.

Basic C does not have “for element in array:” type constructs. (but python can’t do magic; behind the scenes, it’ll be doing the same sort of loop as you need to construct in C. C is just more primitive.)

If I press a button (n) times, how can I check if primes[n] exists?? (in other words, is "n" an element of the array)

You can't easily do this. For example, if I asked you is the phone number 555 12345 in the phone book there isn't a quick answer. You have to look through the whole book to find out.

You can make a "reverse lookup" which you may or may not have enough memory for.

there isn't a quick answer. You have to look through the whole book to find out.

Well, there are a bunch of ways that are generally faster than looking through the whole array, that are useful for very large arrays. As a trivial example, if you have an array of primes, you can stop searching as soon as array[n] is larger than your target value, and you don't need to search at all if the low bit of the target is set. There are entire books on the subject. http://www.amazon.com/Art-Computer-Programming-Volume-Searching/dp/0201896850

Yes, but, to find a number in the phone book, the names might be in order but the numbers are not. However, obviously you stop searching once you found it.

At least in this case there is a finite number of presses before he is crippled by RSI!

If you know the number of elements in the array and they are sorted, maybe a binary search would be quicker.

This assumes that there are far more than 5 element!

Google prime numbers and you will find the 1000th prime is only 7919. Still a lot of button presses.

Weedpharma

westfw: Basic C does not have "for element in array:" type constructs. (but python can't do magic; behind the scenes, it'll be doing the same sort of loop as you need to construct in C. C is just more primitive.)

If you enable C++11 in the IDE, you can use a for each loop:

for( int i : primes ){
  //compare i with number.
}

Doesn't help if you need the index though.