Know how many elements I have in an array in Arduino

Asked

Viewed 396 times

0

I have an ARRAY of at most 100 row by 2 columns, which will be filled by a user.

My program will play user activity after.

What I want to know is how to know how many lines were filled with data. There is some C command for this?

  • 1

    It doesn’t exist, you have to create an algorithm.

  • 3

    If you post a [mcve] of the part that already works, it’s easier for the community to help. Without seeing how the code is implemented, everything is "kick".

1 answer

0


The ideal is to start by clearing any kind of value within the array.

int x; // limpa os valores do array
for(x=0;x<100;x++){
    array[x][0] = 0;
    array[x][1] = 0;
}

Then the method for insertion is done.

void array_insert(int *data, int **array){
    if(array[99][0] && array[99][1])
        return; //encerra caso o array esteja cheio

    int pos = 0;
    while(array[pos][0] && array[pos][1]) // procura uma posição vazia
        pos++;

    array[pos][0] = data[0]; // insere valor na posição livre do array
    array[pos][1] = data[1];
}

And the count:

int array_len(int **array){
    int x=0;
    while(array[x][0] && array[x][1])
        x++;

    return x;
}

Note: How you said you were working with an array 100*2, values are double pointers so you can access the positions without error.

  • Brumazzi DB, thank you very much! I think this will help me. As said I’m new, and these code fragments will be very useful!

  • 1

    @Andrewahlbuhlganzarolli If an answer solves your problem, you can click on v next to the answer to mark your question as answered.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.