11-18-2021 12:18 PM - edited 11-18-2021 12:35 PM
how do you find the sum of all odd numbers from an array. The array in question is coming out of a loop. Help please.
This is what I have so far. when i run it I do not get an output for the sum of the odd numbers.
11-18-2021 12:28 PM - edited 11-18-2021 12:29 PM
First, you have to determine how you are going to tell an even number from an odd one? Have any ideas? To help you on your voyage of discovery, here is the definition of an even number.
11-18-2021 12:29 PM
It probably does not matter where the array comes from, right?
I assume you have integers (blue datatype), so to check odd/even, all you need to inspect is the LSB (e.g. doing a logical AND with 1 and see if the result is zero or not).
So, all you need is another autoindexing FOR loop where you process element by element and conditionally add each to a initialized shift register if it is odd. If you have control over the previous loop, you could even do the processing right there... This is quite simple, and if your code is larger than a postage stamp, you are doing something wrong 😄
See how far you get and attach your code if you get stuck and ask specific questions.
11-18-2021 12:31 PM
@altenbach wrote:
It probably does not matter where the array comes from, right?
I assume you have integers (blue datatype), so to check odd/even, all you need to inspect is the LSB (e.g. doing a logical AND with 1 and see if the result is zero or not).
So, all you need is another autoindexing FOR loop where you process element by element and conditionally add each to a initialized shift register if it is odd. If you have control over the previous loop, you could even do the processing right there... This is quite simple, and if your code is larger than a postage stamp, you are doing something wrong 😄
See how far you get and attach your code if you get stuck and ask specific questions.
You ruined all the fun. I was suggesting starting from the very beginning. 😄
11-18-2021 12:37 PM
I have edited my post for you to see my code. could you please tell me what i am doing wrong . Thanks!
11-18-2021 12:42 PM
@Tayra! wrote:
I have edited my post for you to see my code. could you please tell me what i am doing wrong . Thanks!
The first thing you're doing wrong is not attaching your VI.
11-18-2021 12:43 PM
@billko wrote:
You ruined all the fun. I was suggesting starting from the very beginning.
Sorry, your reply was not there when I composed my reply.
Either way they'll learn something. Even translating my words into graphical code could be quite a challenge for a newbie. Blindly basing odd/even determination on division as you suggested will probably be quite messy, because the typical division outputs orange and then we need to separate the fractional and integer parts and do comparison on floating point numbers, which is another can of worms (Probably not here, but in general). Yes, there are better types of "divisions" (e.g. one that starts with the letter Q), but they are not obvious for the new student. 😄
11-18-2021 12:47 PM - edited 11-18-2021 12:51 PM
@Tayra! wrote:
I have edited my post for you to see my code. could you please tell me what i am doing wrong . Thanks!
All we see is a photo of a blurry diagram, partially covered by the front panel.
Place indicators (or probes) after the AND and after the multiplication to see what's happening. Simple troubleshooting!
If you want real help, you need to attach your actual VI. Please use "save for previous" if you use LabVIEW 2021.
11-18-2021 01:04 PM - edited 11-18-2021 01:10 PM
Obviously, your code is sound, because it works for me, so there must be something else going on.
Some simple things to check: