Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,
How do I bin and zero fill an array? For example:
I have an array:
double[] data = [1.2,2.9,5.9,7] (which is 4 elements long and I want to create an array that is:
new double[10] (10 elements long)

I want to bin and zero fill using integers of 1. The result should look like this:
result = [0,1.2,0,2.9,0,5.9,7,0,0,0]

What I have tried:

I have tried exactly nothing which is why I'm asking the question
Posted
Updated 4-Mar-16 12:32pm

1 solution

What does "bin" have to do with this? It doesn't look like a binning problem.

So, apparently, the result should contain alternating zeros and values from data, starting with a zero, and filling to the end with zero, right?

So, how would you do it if you had two stacks of paper, one with 4 sheets with the values of data and one with 6 (10-4) sheets with zeros, and were going to make a single pile of paper in the same sequence as you want for result?
That's what your code needs to do.

This is really pretty straightforward.
Hint: You don't need actually to deal with the zeros. When you use new double[10] to assign to result, C# has guaranteed to have filled it with zeros.

So ... are there "edge cases" that you haven't specified (or thought about)?
For example:
What if data.Length is greater than half of
result.Length<br />
?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900