LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Help adding elements in an array

Solved!
Go to solution

I must create a program that generates 10 random numbers between 195 and 205, then add the 5 that together approach 1000 the closest. I already made a program that generates the numbers in an array, but I have no idea how to evaluate the numbers. Can anyone please help me?

0 Kudos
Message 1 of 5
(1,395 Views)
Solution
Accepted by topic author Yerry21

First, get rid of that local variable. It will get read before the indicator terminal receives data and the sum is thus meaningless. Just wire to the array coming from the FOR loop.

 

To pick five that that have a sum closest to 1000, you need quite a bit more code. The brute force method would be to generate all possible ways to pick five out of 10 and pick the combination with the closest sum (there might be duplicate solutions, of course).

 

(You also have a mathematical problem that the edge values in the selected range only have half the probability of the other values. Try to find a solution for that too)

0 Kudos
Message 2 of 5
(1,365 Views)

@Yerry21 wrote:

I must create a program that generates 10 random numbers between 195 and 205, then add the 5 that together approach 1000 the closest. I already made a program that generates the numbers in an array, but I have no idea how to evaluate the numbers. Can anyone please help me?


Well, I can't open your VI because I don't have a (working) PC with LabVIEW 2017 installed (I had two with LabVIEW 2017 installed, including this one, but they became "no-longer-working" and had to be rebuilt).  So here are some things:

  • I assume you know how to generate 10 random numbers between 195 and 205.
  • Let's assume the array contains [200.1, 196.2, 203.2, 199.4, 197.3, 204.1, 198.3, 196.3, 198.5, 201.3].
  • Can you think of a quick way to answer the question "Which number is closest to 1000?"?  Hint -- don't think about programming, think about answering the question.
  • Did answering the preceding question generate the "Aha! Moment" for you?  Is the algorithm to answer your question now both simple and obvious?

Bob Schor

0 Kudos
Message 3 of 5
(1,359 Views)

@Bob_Schor wrote:
  • Can you think of a quick way to answer the question "Which number is closest to 1000?"?  

The question is which combination of 5 of the 10 numbers form a sum that is closest to 1000.

0 Kudos
Message 4 of 5
(1,356 Views)

Hmm.  I just saw the earlier response, and my understanding of the Problem may be flawed.  I thought the task was to add together the five numbers that were closest to 1000, but (as I was hinting) this is so simple that it is probably the "wrong question", namely find the five numbers in the set whose sum was closest to 1000.  That's an interesting (and much more challenging) problem.  

 

Do you know about Permutations and Combinations?  There are "10 Choose 5" or (by my back-of-the-envelope) 252 combinations to check.  But the number doesn't really matter.  What you need to do is something like this:

  • Figure out how to enumerate each of the combinations of 5 from the 10 numbers.
  • Associate with each combination their sum.
  • Find the 5 sums closest to 1000 (my erroneous interpretation of the original problem may suggest a way to do this).

Bob Schor

0 Kudos
Message 5 of 5
(1,349 Views)