hi,
I'm totally new to this so can anybody help me in creating a code to serial print the 10th number stored in a array of 100 numbers. The numbers can be anything received from the tx pin from arduino.
Thanks in advance
hi,
I'm totally new to this so can anybody help me in creating a code to serial print the 10th number stored in a array of 100 numbers. The numbers can be anything received from the tx pin from arduino.
Thanks in advance
The modulo operator (%) is what you need
Take ideas from this
//original array
//byte anArray[] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 0, 123, 234, 345, 456, 789, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
//array amended following comments about the values used
int anArray[] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 0, 123, 234, 345, 456, 789, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
byte numberOfElements = sizeof(anArray);
void setup()
{
Serial.begin(115200);
Serial.println(numberOfElements);
for (int x = 0 ; x < numberOfElements; x++)
{
if ((x % 5) == 0)
{
Serial.print(x);
Serial.print(" : ");
Serial.println(anArray[x]);
}
}
}
void loop()
{
}
Bob:
Your byte array has values greater than 255, which I don't think will work. Also, to see each 10th element of the array, shouldn't the test be:
if ((x % 10 == 0 && x != 0) {
9%10 == 0 ?
If you just want to print an element of your array. This would work. Arrays start at zero so that is why one is subtracted from arrayElement
int arrayElement=9;
Serial.println(yourArrayName[arrayElement-1];
Or you could, of course, simply do
for (int x = 0; x < arraySize; x += 10)
Actually, looking closer at the thread title it seems that the OP may only want to print one element anyway !
UKHeliBob:
Or you could, of course, simply dofor (int x = 0; x < arraySize; x += 10)
Actually, looking closer at the thread title it seems that the OP may only want to print one element anyway !
Wouldn't that print the 1st, 11th, 21st,...91st elements?
evanmars:
Wouldn't that print the 1st, 11th, 21st,...91st elements?
Yes, but as I said in reply #5 I only just realised that the OP seems only to want to print one element anyway.
Sorry to have caused confusion but I am not having the best of days !
venkatesh223:
I'm totally new to this so can anybody help me in creating a code to serial print the 10th number stored in a array of 100 numbers.
int Array[100];
Serial.print(Array[9]); // Indexes start at 0 so index 9 is the 10th number.