06-26-2015 05:49 PM
I need to create a program that will take a six element array, and using shift registers have it output an array that keeps the first element the same, then adds the second and third elements together, then adds the second through 6th element. So if the input array is 2, 12, 10, 5, 20 and 25, the output array will be 2, 22 and 72. I am thinking that using shift registers and a for loop would work, but I cannot get it to work.
Thanks for any help
Solved! Go to Solution.
06-26-2015 06:00 PM
I am not sure whay you would want a loop or a shift register. Index the array, add the appropriate elements, and Build Array.
Lynn
06-26-2015 06:28 PM
@mike28440 wrote:
I need to create a program that will take a six element array, and using shift registers have it output an array that keeps the first element the same, then adds the second and third elements together, then adds the second through 6th element. So if the input array is 2, 12, 10, 5, 20 and 25, the output array will be 2, 22 and 72. I am thinking that using shift registers and a for loop would work, but I cannot get it to work.
First you say that using shift registers is a requirement ("need!"), but later you say that you "think" you need a shift register? Which one is it?
Is this for school as an exercise to learn about shift registers?
If you are just looking for a simple non-scalable solution, use Lynns code above.
If you need a scalable solution that works on any size input array and does the operation according to some defined rules, tell us what the rules are.
If you cannot get it to work (as you say), feel free to attach your broken code and maybe you learn something from our advice. 😄
06-26-2015 06:29 PM
Sorry I was not entirely clear. While yours does it, It needs to calculate the 22, and then add 22 to the rest of the elements in the array. So it leaves the first element of the array alone, then calculates 22 then adds 22 to the rest of the elements. I think that is why shift registers are necessary.
06-26-2015 06:34 PM - edited 06-26-2015 06:35 PM
Well, originally you said that the desired output is [2,22,72] and that's exactly what Lynns code does.
If you want something else, please tell us what the output array should look like! How many elements? What values? Or should the output be a scalar?
06-26-2015 06:39 PM
What you appear to be describing now still does not require a shift register.
As altenbach has asked, please give a complete and precise description of what the rules are.
Lynn
06-26-2015 06:46 PM
Yes, it is for a class, but we can use any resources available, and I have not seen anything in the forums close to this kind of thing. So it should look exactly like Lynn's output, but a shift register is required. So far all I have been able to do is create the input array and wire it to the register. If know if I wire it directly and run it through a for loop with N=3 the output array will just display 2, 10 and 12. I have been trying different ways of it trying to do the sum and that is the part I am having trouble with. I agree without the shift register it would be much easier and Lynn's code would be exactly what is needed. I have attached the vi with the progress of getting the array sizes.
06-26-2015 06:52 PM
There is no restriction on what kind of loop is used. I have been trying it with a for loop, but that is not required. The requirements are:
Use shift register
take the elements of a 6 element array, and the output will be a 3 element array. The first element will be the first element of the orignal array. The second element is elements 2 and 3 added together, and the third element is the result of element 2 added to the rest of the elements in the original array. So exactly what your solution without the shift regiister was doing.
06-26-2015 07:11 PM
OK. Since it is for a class I will not do any more code.
Comments.
1. A shift register is a form of local memory. So, ask yourself what about this problem needs to be remembered?
2. The input and output arrays are different sizes. It does not make sense to wire the array through the shift register because the shift register will "remember" how big the input array is.
3. Thinking about comment 1, how will you get the information from the array to put it into the shift register?
4. What if you applied the same rules but allowed the input array to have any size greater than or equal to 6? This is what is meant by a scalable approach to a problem. The code I posted only works for length 6. For any other length the program needs to be modified. A scalable program works without changes for any size input.
It is often very useful to start with a clear set of requirements, which you have now told us. Then plan how you would do the problem with pencil and paper. Then implement that plan in software.
Lynn
06-26-2015 07:18 PM
So what needs to be remembered are certain elements of the original array. That I get.
Thanks for the tip about not wiring the array to the register. So I guess wiring the array to the loop or just putting the array in the loop would work? Point 4 does not really apply to our problem since it only has to work for any 6 element array. I appreciate that would be a good modification however.