thalsharif
thalsharif

Reputation: 357

How to store values in array inside loop?

I have loop where N=50. inside the loop I have array (vector). my condition is: if i mod 10 == 0, then saving value of summation in a vector. So after finish the loop we expecting to have 5 values stored in a vector. How can I do that without storing all 50 values.

My Example: my vector will save (0 0 0 0 0 0 0 0 0 20 0 0 0 ...). I just want to save only 20 in the first row then repeat it 5 times. I have to use N=50 not 5. is that possible? enter image description here enter image description here

Upvotes: 1

Views: 33196

Answers (2)

CharlesB
CharlesB

Reputation: 90396

It is easy with shift registers: use one to pass the array being built from one iteration to the other, and test the i%10==0 in a case structure. On true append the current value to the array, else don't modify it.

VI snippet

Upvotes: 6

Brendan Cutajar
Brendan Cutajar

Reputation: 727

I am not sure i understand your question. With your current condition, i==10, you will only have one value stored in the vector not 5 (ie only the value 10 will be store in the vector).

If you want to save the number every time i is a multiple of 10 then all you need to do is add a condition inside the loop to check whether i mod 10 == 0. If the result is true then add i to the vector otherwise ignore the value of i . Alternatively you could loop from 1 to 5 times and add i * 10 to your vector. the result will be the same.

Upvotes: 1

Related Questions