LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sum of numbers using array

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?

0 Kudos
Message 1 of 9
(2,072 Views)

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?

_______________________________________________________________

-Patrik
CLA || CTA
If it helps - Kudo it, if it answers - Mark As Solution
Message 2 of 9
(2,069 Views)

@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.

 

AddNumbers.png

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 3 of 9
(2,017 Views)

@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?

0 Kudos
Message 4 of 9
(2,000 Views)

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

 

 

0 Kudos
Message 5 of 9
(1,994 Views)

@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.

 

Spoiler

Some functions redacted to let the OP actually try to figure this out for themselves.

 

 


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
Message 6 of 9
(1,952 Views)

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.

 

altenbach_1-1683206547474.png

 

 

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!

 

altenbach_0-1683209968389.png

 

 

Message 7 of 9
(1,936 Views)

@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?

Spoiler
He didn't get arrays!
Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 8 of 9
(1,902 Views)

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:

  1. Start with a 64-bit Integer Control ("Input Number") and a 32-bit integer Indicator ("Digital Sum").
  2. Form the sum of the digits of "Input Number".  For example, if the number was 12345, the sum of the digits would be 15 (1+2+3+4+5)..
  3. If the sum is a single digit, stop and display it as "Digital Sum".  Otherwise, repeat Step 2 with the number you just computed.

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

Message 9 of 9
(1,787 Views)