Question about variables signed value of group

Hi all.
If I have:

int varA =0; int varB =0; int varC =0; int varD =0; int varE =0; int varF =0; int varG =0;
some one of them will be set =1 under certain conditions.
and used as:

if( varA == 1 ) {....}
or
if( varB == 1 ) {....}

........

What I am looking for is how to automatically reset all of them =0 except the one on using.
for example:
if (varE ==1) {....} right here I need to reset all others =0, how to do please?

Thanks
Adam

Why not use an array?

1 Like

array is OK, I am not sure how to use it here.

Based on your description so far, neither am I, but memset is a useful function.

1 Like

Thanks.
I'll try it.

consider

output

disp: 1 49 73 58 30 72 44 78 23 9
disp: 1 0 0 0 0 0 0 0 0 0
#define N  10
int arr [N] = {};

void
disp ()
{
    Serial.print ("disp:");
    for (int n = 0; n < N; n++)  {
        Serial.print (" ");
        Serial.print (arr [n]);
    }
    Serial.println ();
}

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

    // populate
    for (int n = 0; n < N; n++)
        arr [n] = random (0, 100);
    arr [random (0, N)] = 1;
    disp();

    // check/reset
    for (int n = 0; n < N; n++)  {
        if (1 == arr [n])  {
            for (int i = 0; i < N; i++)  {
                if (i != n)
                    arr [i] = 0;
            }
            break;
        }
    }
    disp();
}

void
loop (void)
{
}
1 Like

If it's important to separate the function's return type like this, why not also

    for (int 
         n = 0; n < N; n++)  {

?
I'm curious - I've never seen this coding standard before.

1 Like

to allow comment

int                      // largest value
func {                 // return value of largest array element
     int *p,           // array
     int size )        // number of array elements
{
1 Like

So, why no comments?
Normally a header file, prototype thing, no?
And why not the for loop?

1 Like

maybe some organizations have a style that requires a comment to explain the the for loop variable. my organization didn't (and i see no need for one)

but my organizations style did require the return type and arguments on separate lines, even when a comment was not provided (often obvious) and i find it easier to read the code with each on a separate line

1 Like

What do you mean? Automatically? You mean when something else happens? You have to set them to 0. it doesn't matter how you organize them, array or not, the only way to set them to zero is to set them to zero.

1 Like

Thanks.
I mean if I can sign value 1 to a specific variable like:
k=3; and I like to sign 1 to Varray[3] = 1, and all others =0.

for( i =0; i < k; i > k; i<=10; i ++ )
{
Varray[i] = 0;
 }

seems not ok, how to do this.

Cleanest? With a loop? Fill the whole thing with zero, then afterwards, set the specific item you want to be different.

1 Like

Thanks.

If you have an array called Array and you want to change the third value just use

Array[3]=0

Also, instead of using 10 different boolean variables, why not just use one variable and set it to 1-10? If you always want to reset the others so only 1 is ever true, just use an int variable and set it to one value. No need to reset. And checking which is true is SOOO much easier.

1 Like

sp. "fourth value"

If the values will only ever be 0 or 1, then consider using a single unsigned byte to hold all of them. Use bit manipulation to set/clear the bit(s) you want.

Well, since there are 10 boolean items to track, I suspect he'd have to used a 16 bit entity, either int or unsigned int.
For most people, bit manipulation falls into the category of 'advanced voodoo magic', so I'm afraid that unless the OP is intrigued by puzzles, or extremely pushed for memory in his Arduino, he'd be better off keeping these as boolean variables, whether numerically ordered, or in an array. Unless, of course, he's into learning, in which case, 'have at it'!

Ah, I missed that the count went from the original 7 up to 10 in post #12!

My wizard spell level has just increased ... :grinning:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.