LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

find pair of given sum

Solved!
Go to solution

I want to find array pair of given sum.

In my code i found for only for first index.

what mistake i have done in my program please correct it asap

0 Kudos
Message 1 of 8
(2,727 Views)

I want to find array pair of given sum.

In my code i found for only for first index.

what mistake i have done in my program please correct it asap

0 Kudos
Message 2 of 8
(2,707 Views)
Solution
Accepted by topic author certified

Some hints:

  • your random number is guarnteed to be positive, so taking the absolute value makes little difference. Delete it.
  • To get equal distribution, you should round up or down, not to the nearest.
  • In the inner loop, you are only getting the first element ever using "index array". There are others to look at.
  • With every iteration of the outer loop you can reduce the number of iterations of the inner loop by one. (unless you also want to list duplicate result where the same elements are swapped)
  • You are only keeping the last found element index in the inner loop, again the shift register does nothing here.
  • "Built array" is a better choice than "insert into array" when appending an element.
  • All you need is a single formatting statement to form each string from the two numbers.
  • You are dealing with integers (except in the very first loop), so keep it blue!
  • Since the array never changes later, there is no need for a shift register.
  • With larger array sizes, you'll get a lot of duplicate elements, and thus many duplicate solutions. It would be more interesting to have only unique elements in the input array
  • See how far you get

(and please don't maximize the diagram to the screen, that's annoying!)

Message 3 of 8
(2,717 Views)

please give me the solution how can i remove duplicte pairs that are comming in pair array.

 

 

thank you

0 Kudos
Message 4 of 8
(2,712 Views)

I gave you a lot of hints already. Is this homework or a real-life professional problem?

 

We don't do homework, but we usually help to get on the right track.

0 Kudos
Message 5 of 8
(2,699 Views)

From your description, I don't really understand your (homework?) assignment, but presumably, you do.  So here is a suggestion:

  • Get a pencil and a piece of paper.
  • Write down 10 numbers in a list.
  • Write down a sum.
  • Try to solve your problem with pencil and paper.  Pay attention to the steps you are taking.  Think about situations where there is no solution, or where there is more than one solution.
  • Once you know what to do, translate your steps into LabVIEW code.

Some suggestions and "facts you should know" --

  • It looks like you are trying to generate random integers.  You are doing this using Floats, which have a problem when asking "are two Floats equal?".  it probably won't be an issue in this particular problem (since integers as Floats usually compare properly), but your code will be more robust (and the data wire will better reflect the data) if you convert the random Float to a random I32.
  • As Altenbach points out, "Round to nearest Integer" will give you (on average) 5% 0's, 5% 10's, and 10% 1, 2, 3, ...9's.  Maybe that's OK, but it seems like an "unintended Consequence".

Bob Schor

 

0 Kudos
Message 6 of 8
(2,638 Views)

@certified wrote:

please give me the solution how can i remove duplicte pairs that are comming in pair array.

 


You get duplicates for two different reasons:

  1. If the input array contains duplicate elements. If you create 100 random integers with only 10 possibilities, each number will occur about 10 times and many solutions will look the same, even if you compare different elements. There are way to eliminate duplicate elements, but maybe you should only create unique elements from the beginning. OTOH, maybe each value could occur up to twice so you also potentially get a sum of two identical numbers?
  2. If you blindly compare all possible combinations, you get duplicate solutions because addition is commutative. A+B = B+A. You can easily change the FOR loops so any two element pairs are only checked once.

From the problem description it is also not clear if the same element can be used twice. For example if the desired sum is 4 and only one element is 2, is 2+2 = 4 an allowed solution?

 

0 Kudos
Message 7 of 8
(2,623 Views)

Since you probably learned something by now, here's a simpler, more interesting, but similar problem:

 

Given an positive integer A, find all possible pairs of positive integers that add up to A.

 

See how far you get... 😄

0 Kudos
Message 8 of 8
(2,589 Views)