2016-04-04 13 views
0

このページをお寄せいただきありがとうございます。私はC#の初心者です。なぜこのコードが値をインクリメントするのかを知りたいと思います。基本的には、システム内の個々の粒子の速度を測定しようとしていますが、テキストファイルの結果は、各速度値が次の値に加算されることを示しています。私が必要とするのは、速度の各値をビンに入れることです。例えば、0と0.5の間の速度が1の場合は1、ビン​​2の場合は0.5〜1.0のようになります。 binをテキストファイルに書き込むことができます。このファイルをヒストグラムに変換して、特定のビン内の各速度がどのくらいの頻度で発生するかを確認することができます。私のコードは以下の通りです。私は与えられた助けに非常に感謝しています。そのページを見ていただきありがとうございます。このコードはなぜ増加するのですか? C#

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); 
     } 
     } 
+0

Math.Roundの構文が原因です。 –

+0

何が問題なのですか?タイトルにはインクリメントがあり、説明には各ビンの頻度をカウントする方法が記述されています。ここで問題の定義はありません。 – Ian

答えて

0
if(velocityArray[i] <= counter) 

これはカウンターの下で、代わりのカウンター下やカウンター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); 
     } 
     } 
関連する問題