LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you get the sum of even numbers from a 1D array

Hi, I was wondering how to get even numbers from a 1D array and find the sum of these even numbers? Any help would be kindly appreciated

0 Kudos
Message 1 of 19
(5,657 Views)

You can put your array into a for loop, and check if it is even or not (quotient remainder comes to mind). Then you can either create a conditional indexing output tunnel (newer versions) or keep your even number array in a shift register and only add to it if your check is true. After the for loop, you will have an array of the even numbers, which you can put into the Add Array Elements function.

0 Kudos
Message 2 of 19
(5,648 Views)
0 Kudos
Message 3 of 19
(5,640 Views)

School assignment? What have you tried?

 

Ben64

0 Kudos
Message 4 of 19
(5,628 Views)

Ask one of your classmates, or your teacher, for help.

 

BS

0 Kudos
Message 5 of 19
(5,615 Views)

@Gregory wrote:

check if it is even or not (quotient remainder comes to mind).


There is also the trick of ANDing with 1 and seeing if the result is 0.  Might be a little more efficient.  There's a big long thread somewhere around here about finding odd numbers that I learned that trick from.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 19
(5,595 Views)

You should also combine the operations. No need to get an array of all odd numbers first. You can keep a running sum while you are finding them. This wil save you potentially a lot of memory.

 

There is also a solution that does not even need a loop. try it. 😄

0 Kudos
Message 7 of 19
(5,559 Views)

I wonder what this code does? Yes, it sums the odd numbers if an integer array.

See if you can modify it to sum the even numbers. 😄

 

 

0 Kudos
Message 8 of 19
(5,544 Views)

@altenbach wrote:

I wonder what this code does?

 

(Please try to understand every single element of the code.)


Shouldn't you add 1 to the array before anding it with 1 and then multiply the original array with the AND result?

 

Ben64

 

EDIT: OUPS, miss part of your message 😞

0 Kudos
Message 9 of 19
(5,526 Views)

Yes, if you want the sum of the even numbers, you need to add two more primitives. as I said, it currently sums the odd numbers. 😄 (Code will still be all blue 😉


@ben64 wrote:
Shouldn't you add 1 to the array before anding it with 1 and then multiply the original array with the AND result?

Yes, I added more words later...

 

If you add one to the array, the resulting sum would be too big, so my tweaks for the even sum are slighly different.

0 Kudos
Message 10 of 19
(5,514 Views)