05-03-2023 01:38 PM
Hello I want to create an VI to read the integer number and find out the sum of all the digits until it comes to a single digit using an array for example n=4728, sum=4+7+2+8=21,
Sum=2+1=3 any ideas how I can go about it?
05-03-2023 01:44 PM - edited 05-03-2023 01:45 PM
Maybe there are easier ways, but you can convert the numeric to string and then scan from string 1 character at the time, convert them back to number and add them.
Sounds like a school homework - maybe try things first out and come back when you stumble upon some problem?
05-03-2023 06:13 PM
@JoeBurban wrote:
Hello I want to create an VI to read the integer number and find out the sum of all the digits until it comes to a single digit using an array for example n=4728, sum=4+7+2+8=21,
Sum=2+1=3 any ideas how I can go about it?
check this out!
Looks Complicated expecting simple solutions from other LabVIEW Folks.
05-03-2023 07:24 PM
@JoeBurban wrote:
Hello I want to create an VI to read the integer number and find out the sum of all the digits until it comes to a single digit using an array for example n=4728, sum=4+7+2+8=21,
Sum=2+1=3 any ideas how I can go about it?
The term "read" is very vague. It looks like you are entering a scalar multidigit integer into a control and want to sum all decimal digits over and over until you reach a single decimal digits, which is the answer you want.
What is the datatype of the input? (or in other words, is there a limit on the number of digits? For example if the input is a string and we enter 5000 decimal digits, should it still work? Should it only work for integers that fit into e.g. I32?
No matter what the answer to these questions is, the code will be simple and probably fit on a postage stamp. What have you tried?
05-03-2023 07:35 PM
I like this problem! You can "get the answer to the question" by using a single LabVIEW function with two inputs, one of which is your 4-digit (or even 12-digit) number, no loops, no Case statements, just the answer of the sum-of-the-sum-of-the-sum-of-the-...-digits (it stops when it gets to a single digit).
Of course this doesn't teach you anything about LabVIEW and understanding how to develop algorithms, so forget about my claim, above.
Bob Schor
05-04-2023 07:04 AM - edited 05-04-2023 07:06 AM
@PalanivelThiruvenkadam wrote:
@JoeBurban wrote:
Hello I want to create an VI to read the integer number and find out the sum of all the digits until it comes to a single digit using an array for example n=4728, sum=4+7+2+8=21,
Sum=2+1=3 any ideas how I can go about it?
check this out!
Looks Complicated expecting simple solutions from other LabVIEW Folks.
You can simplify that code quite a bit if you use Logrithm Base 10 and Round Toward -Inf to figure out how many times to loop on the number. This was a fun little exercise.
05-04-2023 08:28 AM - edited 05-04-2023 09:20 AM
You are looking for the digital root here. A decimal string can have up to ~2^31 characters, so the first sum (if these are all "9s") can exceed the range of U32. (... and you might also need 64 bit LabVIEW for very (very!!!) long inputs!)
Here's what I might do to implement the "literal" solution. No, I am no giving a full solution because more code is needed to verify that the original string is clean, i.e. does not contain anything except lexical class=3 and extra code is needed to validate that and return an error in case the string is dirty.
This code has enough landmines so it cannot be turned in as solution without excessive learning and study, so even though it might work, the student needs to be prepared to answer any and all follow-up questions. so a full understanding of every single detail is important.
Also note that (according to the post subject), we need be to "using array"(sic), so some of the reported solutions don't quite qualify, depending on how rigid these requirements are.
And yes, as Bob already mentioned, you can eliminate the loop entirely, do some trivial math and it will still work for inputs up to 2147483647 decimal digits, so read the link above! you figure it out!
05-04-2023 11:13 AM
@altenbach wrote:
You are looking for the digital root here.
The non-array solution blew my mind!
Also, why did the confused programmer quit his job?
05-07-2023 02:13 PM
Actually, the Original Problem is not a bad "Learning Simple Concepts in LabVIEW" problem, but it should be more clearly stated so Wise Guys (I'm looking in a mirror) don't "show off" by saying you can "get the answer" with a very simple LabVIEW computation, when the Goal should be "Learn about Loops, Shift Registers, and Doing Integer Arithmetic". Here's how I'd propose the problem: Write a LabVIEW program that will do the following:
As a "discussion point" in class, I would point out that this exercise is an example of a recursive problem, as Step 3 basically involves running the same routine with Digital Sum serving as Input Number. Recursion can be a powerful (if bewildering!) technique that LabVIEW can handle quite nicely.
Bob Schor