为什么这段代码会增加? C#

时间:2016-04-04 23:28:07

标签: c# arrays increment recording bin

感谢您查看此页面。我是C#的新手,我想知道为什么这段代码会增加值。基本上,我希望测量系统中单个粒子的速度,但是,文本文件的结果显示速度的每个值都会增加到下一个。我需要的是将每个速度值的计数放入一个bin中,例如,对于0到0.5之间的速度计数为1,进入bin 1,0.5到1.0进入bin 2,依此类推,然后每个值bin可以写入一个文本文件,然后我可以将其转换为直方图,以查看某个bin中每个速度发生的频率。我的代码如下。我非常感谢我给予的任何帮助。感谢您查看该页面。

private void computeVelocity()
    {
        //calc velocity of each particle for printing
        double binSize = 0.5;
        double velocityOfParticle = 0.0;
        double averageSquareVelocity = 0.0;
        int maxArrayValue = 0;
        int binArraySize = 0;
        int z;
        double[] velocityArray = new double[Particle.allParticles.Count];

        for (int counter = 0; counter < Particle.allParticles.Count; counter++)
        {
            averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
            velocityOfParticle = Math.Sqrt(averageSquareVelocity);
            velocityArray[counter] = velocityOfParticle; 
        }
        maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
        binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
        //numberOfBins = Math.Round(maxArrayValue / binSize);
        int [] binCountArray = new int[numberOfParticles];
        int incrementTerm = 0;
        Array.Clear(binCountArray, 0, binCountArray.Length);
        for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
        {
            if(counter > binSize)
            {
                incrementTerm = incrementTerm + 1;
            }
            z = 0;
            for(int i = 0; i < numberOfParticles; i++)
            {
                if(velocityArray[i] <= counter)
                {
                    z++;
                }                  
            }     
            binCountArray[incrementTerm] = z;
        }
        foreach (var item in binCountArray)
        {
            VelocityValues.WriteLine(item);
        }
      }

1 个答案:

答案 0 :(得分:0)

if(velocityArray[i] <= counter) 

这将在计数器下添加所有内容,而不是在counter和over-binSize下。更正(可能):

private void computeVelocity()
    {
        //calc velocity of each particle for printing
        double binSize = 0.5;
        double velocityOfParticle = 0.0;
        double averageSquareVelocity = 0.0;
        int maxArrayValue = 0;
        int binArraySize = 0;
        int z;
        double[] velocityArray = new double[Particle.allParticles.Count];

        for (int counter = 0; counter < Particle.allParticles.Count; counter++)
        {
            averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
            velocityOfParticle = Math.Sqrt(averageSquareVelocity);
            velocityArray[counter] = velocityOfParticle; 
        }
        maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
        binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
        //numberOfBins = Math.Round(maxArrayValue / binSize);
        int [] binCountArray = new int[numberOfParticles];
        int incrementTerm = 0;
        Array.Clear(binCountArray, 0, binCountArray.Length);
        for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
        {
            if(counter > binSize)
            {
                incrementTerm = incrementTerm + 1;
            }
            z = 0;
            for(int i = 0; i < numberOfParticles; i++)
            {
                if((velocityArray[i] <= counter) && (velocityArray[i] > counter - binSize))
                {
                    z++;
                }                  
            }     
            binCountArray[incrementTerm] = z;
        }
        foreach (var item in binCountArray)
        {
            VelocityValues.WriteLine(item);
        }
      }