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.
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])
{
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.
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 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.
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: