0

ヒストグラム

--------------------------------------------------------
  1 ****(4)
  2 ******(6)
  3 ***********(11)
  4 *****************(17)
  5 **************************(26)
  6 *************************(25)
  7 *******(7)
  8 ***(3)
  9 (0)
 10 *(1)
--------------------------------------------------------

基本的に上記は私のプログラムが行う必要があるものです..どこかで何かが足りないのですが、何か助けていただければ幸いです:)

import java.util.Random; 
public class Histogram
{

    /*This is a program to generate random number histogram between
    1 and 100 and generate a table */

    public static void main(String args[])
    {

        int [] randarray = new int [80];
        Random random = new Random();
        System.out.println("Histogram");
        System.out.println("---------");

        int i ;
        for ( i = 0; i<randarray.length;i++)
        {   
            int temp = random.nextInt(100); //random numbers up to number value 100
            randarray[i] = temp;

        }

        int [] histo = new int [10];
        for ( i = 0; i<10; i++)
        {
            /* %03d\t, this generates the random numbers to
            three decimal places so the numbers are generated
            with a full number or number with 00's or one 0*/


            if (randarray[i] <= 10) {
                histo[i] = histo[i] + 1;
            //System.out.println("*");
            }
            else if ( randarray[i] <= 20){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 30){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <= 40){
            histo[i] = histo[i] + 1;
            }
            else if (randarray[i] <= 50){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=60){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=70){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=80){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=90){
            histo[i] = histo[i] + 1;
            }
            else if ( randarray[i] <=100){
            histo[i] = histo[i] + 1;
            }

            switch (randarray[i])
            {
            case 1: System.out.print("0-10 | "); break;
            case 2: System.out.print("11-20 | "); break;
            case 3: System.out.print("21-30 | "); break;
            case 4: System.out.print("31-40 | "); break;
            case 5: System.out.print("41-50 | "); break;
            case 6: System.out.print("51-60 | "); break;
            case 7: System.out.print("61-70 | "); break;
            case 8: System.out.print("71-80 | "); break;
            case 9: System.out.print("81-90 | "); break;
            case 10: System.out.print("91-100 | "); 
            }
                for (int i = 0; i < 80; i++)
            {
              randomNumber = random.nextInt(100)
              index = (randomNumber - 1) / 2;
              histo[index]++;
            }
    }
   }
 }
4

5 に答える 5

1

ランダム データには 80 個の値が含まれていますが、最初の 10 個だけを反復処理しています。80 個すべてを反復処理する必要があります。histo[i] の代わりに histo[1]、histo[2] などを使用します。

また、大きなスイッチ ブロック全体を次のように単純化できます。

histo[randarray[i] / 10]++;

randarray を作成してからループする代わりに、次のように簡単に実行できます。

for(int i = 0; i < 80; i++)
{
    histo[random.nextInt(100) / 10]++;
}
于 2009-11-18T21:03:56.940 に答える
0

考慮すべきもう1つのポイントは、ヒストグラムは、質問で示すような正規分布ではなく、平坦で均一な分布になるということです。

于 2009-11-18T21:21:22.687 に答える
0

正しく読めば、あなたのswitch発言はめちゃくちゃだと思います。

あなたのrandarray値は からのものですが、あなた0 < randarray[i] < 100は までの値しか与えていません。それはおそらく何かを捨てるでしょう。10switch

私の推測です。

于 2009-11-18T20:59:14.300 に答える
0

配列には 80 個の乱数がありますが、ループは 10 回しかありません。

この行にも欠陥があります。配列を調べて、どのヒストグラム ビンをインクリメントするかを決定するために、同じカウンター変数を使用したくありません。

 if (randarray[i] <= 10) {
                        histo[i] = histo[i] + 1;

アップデート:

ソリューションはスケーラブルではなく、プログラミングの悪い習慣に慣れてしまうため、ランダムな値をビンに変換するアルゴリズムを考え出す必要があります。

于 2009-11-18T21:02:49.260 に答える
0

最後の for ループがコンパイルされません。多くの構文エラーがあります。

履歴に乱数を入力してから、それらを印刷する必要があります。

同様に中断する前に、各 case ステートメントで hist 配列のカウントを出力する必要があります。

プログラミングを学び続けるなら、頑張ってください!

于 2009-11-18T21:05:14.270 に答える