LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Initialize array

Hi,

 

I am making an M&M color generator. The code is supposed to generate a color and keep count of the number of each color.

I want to intialize the array in the picture with 0's, because when I initially run the code, the very first generated color (number) is not counted towards the total amount of that color. Initializing with 0's also makes the counter reset everytime the code is run, which would be nice.

It seems like the first number generated initializes the array so it goes from nothing -> 0 -> 1-> etc. instead of 0 -> 1-> etc.

 

I think I can solve the problem by initializing the array with 0's, but I haven't been able to do so. I have also tried using a for loop with shift registers, but I couldn't make it work.

Download All
0 Kudos
Message 1 of 5
(2,587 Views)

I think I solved part of the problem now with a for loop. Now the very first generated color is counted, but the array still doesn't reset everytime I run the code.

Download All
0 Kudos
Message 2 of 5
(2,559 Views)

I ended up using a flat sequence to initialize all the color local variables to 0. It works now.

0 Kudos
Message 3 of 5
(2,550 Views)

The problem isn't that you needed to use a flat sequence.  The problem is that you are using local variables and don't need to.  That, and you have a race condition between the code that generates the random numbers and the code that builds the array.  You actually have a lot of duplicate code in there and the whole VI can be simplified.

 

See the attached for a simpler way to do this without the risk of race condition problems.

0 Kudos
Message 4 of 5
(2,538 Views)

Thanks, I'll take a look at it. I'm new to LabVIEW, so I'm basically going by trial and error.

0 Kudos
Message 5 of 5
(2,517 Views)