简体   繁体   中英

Is it possible in C++ (for arduino) to fill in blank cells of int array with -1?

Or, if not, how can I make the arduino to print an array with changeable size? I need to enter additional rows and columns into an array and program should print all these over serial port.

Explanation of my case:

Array should be hard code defined , because the will not be any input devices . Example data is below:

list1 = { { 99, 75 },{ 40, 20 },{ 800, 30 },{ 10, 50 },{ 100, 5 } };
list2 = { { 50, 40 }, { 50, 0 }};
list3 = { { 50, 30 }, { 20, 4 }};

I decided to use 3 d arrays. Everything was going well until I started to print them. It's impossible to have array size as variable, and that's why I declared it as maximum possible size : array [20] [20] [20] . Arduino prints entered values and zeroes where there is blank cell without entered value. Is there any solution for that?

PS: I wanted the array to be filled with "minus ones", not my topic :))

Just initialize your array right after your declaration:

int myarray[128];//128 would be the max
for (i=0; i<128; i++)
    myarray[i] = -1;

This would fill the whole array with -1 and then you could start filling your array with your custom data, having blank cell filled with -1.

A better solution would be creating functions to handle a dynamic array, for instance:

int test_array[128];
int curr_size = 0;
void pushData(int* myarray, int val){
    myarray[curr_size] =  val;
    curr_size++;
}

int popData(int* myarray){
    curr_size--;
    return myarray[curr_size];
}

//and then you could call these functions like this:

pushData(test_array, 3);

int somevar = popData(test_array);

Of course this is just an example and you shouldn't use it as is, since this would only work for handling 1 array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM