简体   繁体   中英

How do i complete this question with 2-dimensional array in java?

Hey guys, im working through the Introduction to Programming in Java book and one of the exercises is this:

Empirical shuffle check . Run computational experiments to check that our shuffling code works as advertised. Write a program ShuffleTest that takes command-line arguments M and N, does N shuffles of an array of size M that is initialized with a[i] = i before each shuffle, and prints an M-by-M table such that row i gives the number of times i wound up in position j for all j. All entries in the array should be close to N/M.

Now, this code just outputs a block of zeros...

public class ShuffleTest2 {
  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    for (int i = 0; i < M; ++i)
      deck [i] = i;

    int [][] a = new int [M][M];

    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ;

        for(int n = 0; n < N; n++) {
          int r = i + (int)(Math.random() * (M-i));
          int t = deck[r];
          deck[r] = deck[i];
          deck[i] = t;

          for (int b = 0; b < N; b++)
          {
            for (int c = 0; c < M; c++)
              System.out.print(" " + a[b][c]);
            System.out.println();
          }
        }
      }
    }
  }
}

What am i doing wrong? :(

Thanks

So a is like a history? As you are now it is always filled with zeroes just like you initialized, you never assign to it! After the "shuffling" for loop you need to set

A[i][POSITION] = CARD_VALUE

Meaning that after i-th shuffle, card CARD_VALUE is in position POSITION. I don't want to give you all the specifics, but it will take another for loop, and the nested for-loop for printing needs to be independent of any other loop, occuring when everything else is done.

Looks like you have a few things concerning the for-loops that you need to look over carefully. Trace the program flow manually or with a debugger and you'll notice that some of those braces and code blocks need to be moved.

--TRY THIS--

public class ShuffleTest2 {

  public static void main(String[] args) {
    int M = Integer.parseInt(args[0]);
    int N = Integer.parseInt(args[1]); 
    int [] deck = new int [M];

    int [][] a = new int [M][M]; 

    for (int i = 0; i < M; i++) {  //initialize a to all zeroes
      for (int j = 0; j < M; j++) {
        a[i][j] = 0 ; 
      }
    }

    for(int i = 0; i < N; i++)   //puts the deck in order, shuffles it, and records. N times
    {
        for (int j = 0; j < M; j++)  //order the deck
          deck[j] = j;

        for(int j = 0; j < M; j++) {       //shuffle the deck (same as yours except counter name)
          int r = j + (int)(Math.random() * (M-j));
          int t = deck[r];
          deck[r] = deck[j];
          deck[j] = t;
        }

       for(int j = 0; j < M; j++)   //record status of this deck as described
       {
           int card_at_j = deck[j];  //value of card in position j
           a[card_at_j][j]++;        //tally that card_at_j occured in position j
       }
    }  //big loop ended

    for (int b = 0; b < M; b++)  //print loop.  a is MxM, so limit of N was wrong.
    {
        for (int c = 0; c < M; c++)
        {
           System.out.print(" " + a[b][c]);
           System.out.println();
        }
    }  //print loop ended
  }  //main() ended
 } //class ended

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM